Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subscript comment placement with parenthesized value #10496

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
+ 1
)[0]


# Regression tests for: https://github.com/astral-sh/ruff/issues/10355
repro(
"some long string that takes up some space"
Expand All @@ -14,7 +13,7 @@

repro(
"some long string that takes up some space"
)[0 # some long comment also taking up space
)[0 # some long comment also taking up space
]

repro(
Expand All @@ -23,9 +22,20 @@

repro("some long string that takes up some space")[0] # some long comment also taking up space


repro(
"some long string that takes up some space"
)[ # some long comment also taking up space
0:-1
]

(
repro
)[ # some long comment also taking up space
0
]

(
repro # some long comment also taking up space
)[
0
]
13 changes: 8 additions & 5 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,24 @@ fn handle_enclosed_comment<'a>(
// 0
// ]
// ```
if comment.line_position().is_end_of_line() {
if comment.line_position().is_end_of_line()
&& expr_subscript.value.end() < comment.start()
{
// Ensure that there are no tokens between the open bracket and the comment.
let mut lexer = SimpleTokenizer::new(
locator.contents(),
TextRange::new(expr_subscript.value.end(), comment.start()),
)
.skip_trivia();

// Skip the opening parenthesis.
let Some(paren) = lexer.next() else {
// Skip to after the opening parenthesis (may skip some closing parentheses of value)
if !lexer
.by_ref()
.any(|token| token.kind() == SimpleTokenKind::LBracket)
{
return CommentPlacement::Default(comment);
};

debug_assert_eq!(paren.kind(), SimpleTokenKind::LBracket);

// If there are no additional tokens between the open parenthesis and the comment, then
// it should be attached as a dangling comment on the brackets, rather than a leading
// comment on the first argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ result = (
+ 1
)[0]


# Regression tests for: https://github.com/astral-sh/ruff/issues/10355
repro(
"some long string that takes up some space"
Expand All @@ -20,7 +19,7 @@ repro(

repro(
"some long string that takes up some space"
)[0 # some long comment also taking up space
)[0 # some long comment also taking up space
]

repro(
Expand All @@ -29,12 +28,23 @@ repro(

repro("some long string that takes up some space")[0] # some long comment also taking up space


repro(
"some long string that takes up some space"
)[ # some long comment also taking up space
0:-1
]

(
repro
)[ # some long comment also taking up space
0
]

(
repro # some long comment also taking up space
)[
0
]
```

## Output
Expand All @@ -45,7 +55,6 @@ result = (
+ 1
)[0]


# Regression tests for: https://github.com/astral-sh/ruff/issues/10355
repro(
"some long string that takes up some space"
Expand All @@ -65,10 +74,17 @@ repro("some long string that takes up some space")[
0
] # some long comment also taking up space


repro(
"some long string that takes up some space"
)[ # some long comment also taking up space
0:-1
]

(repro)[ # some long comment also taking up space
0
]

(
repro # some long comment also taking up space
)[0]
```