mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +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)
|
handle_leading_class_with_decorators_comment(comment, class_def)
|
||||||
}
|
}
|
||||||
AnyNodeRef::StmtImportFrom(import_from) => handle_import_from_comment(comment, import_from),
|
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::StmtWith(with_) => handle_with_comment(comment, with_),
|
||||||
AnyNodeRef::ExprConstant(_) => {
|
AnyNodeRef::ExprConstant(_) => {
|
||||||
if let Some(AnyNodeRef::ExprFString(fstring)) = comment.enclosing_parent() {
|
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`].
|
/// Attach an enclosed end-of-line comment to a [`ast::StmtWith`].
|
||||||
///
|
///
|
||||||
/// For example, given:
|
/// 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_ast::{MatchCase, Pattern, Ranged};
|
||||||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
|
|
||||||
use crate::comments::{leading_comments, SourceComment};
|
use crate::comments::SourceComment;
|
||||||
use crate::expression::parentheses::parenthesized;
|
use crate::expression::parentheses::parenthesized;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
|
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
|
||||||
|
@ -21,8 +21,13 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
||||||
body,
|
body,
|
||||||
} = item;
|
} = 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 comments = f.context().comments().clone();
|
||||||
let dangling_item_comments = comments.dangling(item);
|
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!(
|
write!(
|
||||||
f,
|
f,
|
||||||
|
@ -33,19 +38,10 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
||||||
&format_with(|f| {
|
&format_with(|f| {
|
||||||
write!(f, [text("case"), space()])?;
|
write!(f, [text("case"), space()])?;
|
||||||
|
|
||||||
let leading_pattern_comments = comments.leading(pattern);
|
if is_match_case_pattern_parenthesized(item, pattern, f.context())? {
|
||||||
if !leading_pattern_comments.is_empty() {
|
parenthesized("(", &pattern.format(), ")")
|
||||||
parenthesized(
|
.with_dangling_comments(open_parenthesis_comments)
|
||||||
"(",
|
.fmt(f)?;
|
||||||
&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)?;
|
|
||||||
} else {
|
} else {
|
||||||
pattern.format().fmt(f)?;
|
pattern.format().fmt(f)?;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +53,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
||||||
Ok(())
|
Ok(())
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
clause_body(body, dangling_item_comments),
|
clause_body(body, trailing_colon_comments),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,8 +222,7 @@ match ( # d 2
|
||||||
case d2:
|
case d2:
|
||||||
pass
|
pass
|
||||||
match d3:
|
match d3:
|
||||||
case (
|
case ( # d 3
|
||||||
# d 3
|
|
||||||
x
|
x
|
||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -354,8 +354,7 @@ match pattern_comments:
|
||||||
|
|
||||||
|
|
||||||
match pattern_comments:
|
match pattern_comments:
|
||||||
case (
|
case ( # leading
|
||||||
# leading
|
|
||||||
only_leading
|
only_leading
|
||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue