mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Format ExpressionStarred
nodes (#5654)
This commit is contained in:
parent
9f486fa841
commit
987111f5fb
16 changed files with 192 additions and 581 deletions
|
@ -87,11 +87,12 @@
|
|||
//!
|
||||
//! It is possible to add an additional optional label to [`SourceComment`] If ever the need arises to distinguish two *dangling comments* in the formatting logic,
|
||||
|
||||
use ruff_text_size::TextRange;
|
||||
use std::cell::Cell;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
use rustpython_parser::ast::Mod;
|
||||
use rustpython_parser::ast::{Mod, Ranged};
|
||||
|
||||
pub(crate) use format::{
|
||||
dangling_comments, dangling_node_comments, leading_alternate_branch_comments, leading_comments,
|
||||
|
@ -114,7 +115,7 @@ mod placement;
|
|||
mod visitor;
|
||||
|
||||
/// A comment in the source document.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub(crate) struct SourceComment {
|
||||
/// The location of the comment in the source document.
|
||||
slice: SourceCodeSlice,
|
||||
|
@ -155,15 +156,20 @@ impl SourceComment {
|
|||
pub(crate) fn is_unformatted(&self) -> bool {
|
||||
!self.is_formatted()
|
||||
}
|
||||
}
|
||||
|
||||
impl SourceComment {
|
||||
/// Returns a nice debug representation that prints the source code for every comment (and not just the range).
|
||||
pub(crate) fn debug<'a>(&'a self, source_code: SourceCode<'a>) -> DebugComment<'a> {
|
||||
DebugComment::new(self, source_code)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for SourceComment {
|
||||
#[inline]
|
||||
fn range(&self) -> TextRange {
|
||||
self.slice.range()
|
||||
}
|
||||
}
|
||||
|
||||
/// The position of a comment in the source text.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub(crate) enum CommentLinePosition {
|
||||
|
|
|
@ -38,6 +38,7 @@ pub(super) fn place_comment<'a>(
|
|||
handle_slice_comments,
|
||||
handle_attribute_comment,
|
||||
handle_expr_if_comment,
|
||||
handle_trailing_expression_starred_star_end_of_line_comment,
|
||||
];
|
||||
for handler in HANDLERS {
|
||||
comment = match handler(comment, locator) {
|
||||
|
@ -1215,6 +1216,21 @@ fn handle_expr_if_comment<'a>(
|
|||
CommentPlacement::Default(comment)
|
||||
}
|
||||
|
||||
fn handle_trailing_expression_starred_star_end_of_line_comment<'a>(
|
||||
comment: DecoratedComment<'a>,
|
||||
_locator: &Locator,
|
||||
) -> CommentPlacement<'a> {
|
||||
if comment.line_position().is_own_line() || comment.following_node().is_none() {
|
||||
return CommentPlacement::Default(comment);
|
||||
}
|
||||
|
||||
let AnyNodeRef::ExprStarred(starred) = comment.enclosing_node() else {
|
||||
return CommentPlacement::Default(comment);
|
||||
};
|
||||
|
||||
CommentPlacement::leading(starred.as_any_node_ref(), comment)
|
||||
}
|
||||
|
||||
/// Looks for a token in the range that contains no other tokens except for parentheses outside
|
||||
/// the expression ranges
|
||||
fn find_only_token_in_range(range: TextRange, locator: &Locator, token_kind: TokenKind) -> Token {
|
||||
|
|
|
@ -454,6 +454,13 @@ impl<'a> DecoratedComment<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Ranged for DecoratedComment<'_> {
|
||||
#[inline]
|
||||
fn range(&self) -> TextRange {
|
||||
self.slice.range()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DecoratedComment<'_>> for SourceComment {
|
||||
fn from(decorated: DecoratedComment) -> Self {
|
||||
Self::new(decorated.slice, decorated.line_position)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue