mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 02:12:22 +00:00
Fix formatter with-statement after-as own line comment instability (#6033)
**Summary** Fix an instability in with statement formatter when there is an own line comment as the `as` ```python with ( a as # bad comment b): ``` **Test Plan** Added the comment to the test cases.
This commit is contained in:
parent
a9f535997d
commit
7f3797185c
4 changed files with 44 additions and 11 deletions
|
@ -25,6 +25,7 @@ with (
|
||||||
with (
|
with (
|
||||||
a # a
|
a # a
|
||||||
as # as
|
as # as
|
||||||
|
# own line
|
||||||
b # b
|
b # b
|
||||||
, # comma
|
, # comma
|
||||||
c # c
|
c # c
|
||||||
|
@ -32,6 +33,13 @@ with (
|
||||||
... # body
|
... # body
|
||||||
# body trailing own
|
# body trailing own
|
||||||
|
|
||||||
|
with (
|
||||||
|
a # a
|
||||||
|
as # as
|
||||||
|
# own line
|
||||||
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb # b
|
||||||
|
): pass
|
||||||
|
|
||||||
|
|
||||||
with (a,): # magic trailing comma
|
with (a,): # magic trailing comma
|
||||||
...
|
...
|
||||||
|
|
|
@ -1370,15 +1370,15 @@ fn handle_with_item_comment<'a>(
|
||||||
SimpleTokenKind::As,
|
SimpleTokenKind::As,
|
||||||
);
|
);
|
||||||
|
|
||||||
// If before the `as` keyword, then it must be a trailing comment of the context expression.
|
|
||||||
if comment.end() < as_token.start() {
|
if comment.end() < as_token.start() {
|
||||||
|
// If before the `as` keyword, then it must be a trailing comment of the context expression.
|
||||||
CommentPlacement::trailing(context_expr, comment)
|
CommentPlacement::trailing(context_expr, comment)
|
||||||
}
|
}
|
||||||
// Trailing end of line comment coming after the `as` keyword`.
|
// Trailing end of line comment coming after the `as` keyword`.
|
||||||
else if comment.line_position().is_end_of_line() {
|
else if comment.line_position().is_end_of_line() {
|
||||||
CommentPlacement::dangling(comment.enclosing_node(), comment)
|
CommentPlacement::dangling(comment.enclosing_node(), comment)
|
||||||
} else {
|
} else {
|
||||||
CommentPlacement::Default(comment)
|
CommentPlacement::leading(optional_vars, comment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ use rustpython_parser::ast::WithItem;
|
||||||
|
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::{write, Buffer, FormatResult};
|
||||||
|
|
||||||
use crate::comments::trailing_comments;
|
use crate::comments::{leading_comments, trailing_comments};
|
||||||
use crate::expression::maybe_parenthesize_expression;
|
use crate::expression::maybe_parenthesize_expression;
|
||||||
use crate::expression::parentheses::Parenthesize;
|
use crate::expression::parentheses::Parenthesize;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
@ -27,14 +27,22 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
|
||||||
if let Some(optional_vars) = optional_vars {
|
if let Some(optional_vars) = optional_vars {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[space(), text("as"), trailing_comments(trailing_as_comments)]
|
||||||
space(),
|
|
||||||
text("as"),
|
|
||||||
trailing_comments(trailing_as_comments),
|
|
||||||
space(),
|
|
||||||
optional_vars.format(),
|
|
||||||
]
|
|
||||||
)?;
|
)?;
|
||||||
|
let leading_var_comments = comments.leading_comments(optional_vars.as_ref());
|
||||||
|
if leading_var_comments.is_empty() {
|
||||||
|
write!(f, [space(), optional_vars.format()])?;
|
||||||
|
} else {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
[
|
||||||
|
// Otherwise the comment would end up on the same line as the `as`
|
||||||
|
hard_line_break(),
|
||||||
|
leading_comments(leading_var_comments),
|
||||||
|
optional_vars.format()
|
||||||
|
]
|
||||||
|
)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ with (
|
||||||
with (
|
with (
|
||||||
a # a
|
a # a
|
||||||
as # as
|
as # as
|
||||||
|
# own line
|
||||||
b # b
|
b # b
|
||||||
, # comma
|
, # comma
|
||||||
c # c
|
c # c
|
||||||
|
@ -38,6 +39,13 @@ with (
|
||||||
... # body
|
... # body
|
||||||
# body trailing own
|
# body trailing own
|
||||||
|
|
||||||
|
with (
|
||||||
|
a # a
|
||||||
|
as # as
|
||||||
|
# own line
|
||||||
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb # b
|
||||||
|
): pass
|
||||||
|
|
||||||
|
|
||||||
with (a,): # magic trailing comma
|
with (a,): # magic trailing comma
|
||||||
...
|
...
|
||||||
|
@ -142,12 +150,21 @@ with (
|
||||||
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
a as b, # a # as # b # comma
|
a as # a # as
|
||||||
|
# own line
|
||||||
|
b, # b # comma
|
||||||
c, # c
|
c, # c
|
||||||
): # colon
|
): # colon
|
||||||
... # body
|
... # body
|
||||||
# body trailing own
|
# body trailing own
|
||||||
|
|
||||||
|
with (
|
||||||
|
a as # a # as
|
||||||
|
# own line
|
||||||
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb # b
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
a,
|
a,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue