mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
Handle open-parenthesis comments on match case (#6798)
## Summary Ensures that we retain the open-parenthesis comment in cases like: ```python match pattern_comments: case ( # leading only_leading ): ... ``` Previously, this was treated as a leading comment on `only_leading`. ## Test Plan `cargo test`
This commit is contained in:
parent
5f5de52aba
commit
4bc5eddf91
4 changed files with 37 additions and 20 deletions
|
@ -210,6 +210,7 @@ fn handle_enclosed_comment<'a>(
|
|||
handle_leading_class_with_decorators_comment(comment, class_def)
|
||||
}
|
||||
AnyNodeRef::StmtImportFrom(import_from) => handle_import_from_comment(comment, import_from),
|
||||
AnyNodeRef::MatchCase(match_case) => handle_match_case_comment(comment, match_case),
|
||||
AnyNodeRef::StmtWith(with_) => handle_with_comment(comment, with_),
|
||||
AnyNodeRef::ExprConstant(_) => {
|
||||
if let Some(AnyNodeRef::ExprFString(fstring)) = comment.enclosing_parent() {
|
||||
|
@ -1303,6 +1304,28 @@ fn handle_import_from_comment<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
/// Attach an enclosed end-of-line comment to a [`MatchCase`].
|
||||
///
|
||||
/// For example, given:
|
||||
/// ```python
|
||||
/// case ( # comment
|
||||
/// pattern
|
||||
/// ):
|
||||
/// ...
|
||||
/// ```
|
||||
///
|
||||
/// The comment will be attached to the [`MatchCase`] node as a dangling comment.
|
||||
fn handle_match_case_comment<'a>(
|
||||
comment: DecoratedComment<'a>,
|
||||
match_case: &'a MatchCase,
|
||||
) -> CommentPlacement<'a> {
|
||||
if comment.line_position().is_end_of_line() && comment.start() < match_case.pattern.start() {
|
||||
CommentPlacement::dangling(comment.enclosing_node(), comment)
|
||||
} else {
|
||||
CommentPlacement::Default(comment)
|
||||
}
|
||||
}
|
||||
|
||||
/// Attach an enclosed end-of-line comment to a [`ast::StmtWith`].
|
||||
///
|
||||
/// For example, given:
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::{MatchCase, Pattern, Ranged};
|
||||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
use crate::comments::{leading_comments, SourceComment};
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::parentheses::parenthesized;
|
||||
use crate::prelude::*;
|
||||
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
|
||||
|
@ -21,8 +21,13 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
body,
|
||||
} = item;
|
||||
|
||||
// Distinguish dangling comments that appear on the open parenthesis from those that
|
||||
// appear on the trailing colon.
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling_item_comments = comments.dangling(item);
|
||||
let (open_parenthesis_comments, trailing_colon_comments) = dangling_item_comments.split_at(
|
||||
dangling_item_comments.partition_point(|comment| comment.start() < pattern.start()),
|
||||
);
|
||||
|
||||
write!(
|
||||
f,
|
||||
|
@ -33,19 +38,10 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
&format_with(|f| {
|
||||
write!(f, [text("case"), space()])?;
|
||||
|
||||
let leading_pattern_comments = comments.leading(pattern);
|
||||
if !leading_pattern_comments.is_empty() {
|
||||
parenthesized(
|
||||
"(",
|
||||
&format_args![
|
||||
leading_comments(leading_pattern_comments),
|
||||
pattern.format()
|
||||
],
|
||||
")",
|
||||
)
|
||||
.fmt(f)?;
|
||||
} else if is_match_case_pattern_parenthesized(item, pattern, f.context())? {
|
||||
parenthesized("(", &pattern.format(), ")").fmt(f)?;
|
||||
if is_match_case_pattern_parenthesized(item, pattern, f.context())? {
|
||||
parenthesized("(", &pattern.format(), ")")
|
||||
.with_dangling_comments(open_parenthesis_comments)
|
||||
.fmt(f)?;
|
||||
} else {
|
||||
pattern.format().fmt(f)?;
|
||||
}
|
||||
|
@ -57,7 +53,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
Ok(())
|
||||
}),
|
||||
),
|
||||
clause_body(body, dangling_item_comments),
|
||||
clause_body(body, trailing_colon_comments),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue