Skip to content

Commit

Permalink
Handle open-parenthesis comments on match case
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 23, 2023
1 parent 94f5f18 commit cbdbb72
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
23 changes: 23 additions & 0 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 14 additions & 16 deletions crates/ruff_python_formatter/src/other/match_case.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand All @@ -33,19 +38,12 @@ 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 !open_parenthesis_comments.is_empty()
|| 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)?;
}
Expand All @@ -57,7 +55,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
Ok(())
}),
),
clause_body(body, dangling_item_comments),
clause_body(body, trailing_colon_comments),
]
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ match ( # d 2
case d2:
pass
match d3:
case (
# d 3
case ( # d 3
x
):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ match pattern_comments:
match pattern_comments:
case (
# leading
case ( # leading
only_leading
):
pass
Expand Down

0 comments on commit cbdbb72

Please sign in to comment.