Improve comment handling around PatternMatchAs (#6797)

## Summary

Follows up on
https://github.com/astral-sh/ruff/pull/6652#discussion_r1300871033 with
some modifications to the `PatternMatchAs` comment handling.
Specifically, any comments between the `as` and the end are now
formatted as dangling, and we now insert some newlines in the
appropriate places.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-23 00:48:20 -04:00 committed by GitHub
parent d08f697a04
commit 1e6d1182bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 142 additions and 7 deletions

View file

@ -205,6 +205,7 @@ fn handle_enclosed_comment<'a>(
handle_module_level_own_line_comment_before_class_or_function_comment(comment, locator)
}
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
AnyNodeRef::PatternMatchAs(_) => handle_pattern_match_as_comment(comment, locator),
AnyNodeRef::StmtFunctionDef(_) => handle_leading_function_with_decorators_comment(comment),
AnyNodeRef::StmtClassDef(class_def) => {
handle_leading_class_with_decorators_comment(comment, class_def)
@ -1145,6 +1146,47 @@ fn handle_with_item_comment<'a>(
}
}
/// Handles trailing comments after the `as` keyword of a pattern match item:
///
/// ```python
/// case (
/// pattern
/// as # dangling end of line comment
/// # dangling own line comment
/// name
/// ): ...
/// ```
fn handle_pattern_match_as_comment<'a>(
comment: DecoratedComment<'a>,
locator: &Locator,
) -> CommentPlacement<'a> {
debug_assert!(comment.enclosing_node().is_pattern_match_as());
let Some(pattern) = comment.preceding_node() else {
return CommentPlacement::Default(comment);
};
let mut tokens = SimpleTokenizer::starts_at(pattern.end(), locator.contents())
.skip_trivia()
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
let Some(as_token) = tokens
.next()
.filter(|token| token.kind == SimpleTokenKind::As)
else {
return CommentPlacement::Default(comment);
};
if comment.end() < as_token.start() {
// If before the `as` keyword, then it must be a trailing comment of the pattern.
CommentPlacement::trailing(pattern, comment)
} else {
// Otherwise, must be a dangling comment. (Any comments that follow the name will be
// trailing comments on the pattern match item, rather than enclosed by it.)
CommentPlacement::dangling(comment.enclosing_node(), comment)
}
}
/// Handles comments around the `:=` token in a named expression (walrus operator).
///
/// For example, here, `# 1` and `# 2` will be marked as dangling comments on the named expression,

View file

@ -1,6 +1,7 @@
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::{Pattern, PatternMatchAs};
use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::parenthesized;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
@ -16,6 +17,8 @@ impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs {
name,
} = item;
let comments = f.context().comments().clone();
if let Some(name) = name {
if let Some(pattern) = pattern {
// Parenthesize nested `PatternMatchAs` like `(a as b) as c`.
@ -31,7 +34,24 @@ impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs {
pattern.format().fmt(f)?;
}
write!(f, [space(), text("as"), space()])?;
if comments.has_trailing(pattern.as_ref()) {
write!(f, [hard_line_break()])?;
} else {
write!(f, [space()])?;
}
write!(f, [text("as")])?;
let trailing_as_comments = comments.dangling(item);
if trailing_as_comments.is_empty() {
write!(f, [space()])?;
} else if trailing_as_comments
.iter()
.all(|comment| comment.line_position().is_own_line())
{
write!(f, [hard_line_break()])?;
}
write!(f, [dangling_comments(trailing_as_comments)])?;
}
name.format().fmt(f)
} else {
@ -39,4 +59,12 @@ impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs {
text("_").fmt(f)
}
}
fn fmt_dangling_comments(
&self,
_dangling_comments: &[SourceComment],
_f: &mut PyFormatter,
) -> FormatResult<()> {
Ok(())
}
}