mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 03:48:29 +00:00
Merge 8b18899f2d into 665f68036c
This commit is contained in:
commit
e880618c5c
9 changed files with 367 additions and 10 deletions
|
|
@ -294,3 +294,39 @@ if parent_body:
|
||||||
# d
|
# d
|
||||||
# e
|
# e
|
||||||
#f
|
#f
|
||||||
|
|
||||||
|
# Compare behavior with `while`/`else` comment placement
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
first;second
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
|
||||||
|
|
@ -28,3 +28,37 @@ while (
|
||||||
and anotherCondition or aThirdCondition # trailing third condition
|
and anotherCondition or aThirdCondition # trailing third condition
|
||||||
): # comment
|
): # comment
|
||||||
print("Do something")
|
print("Do something")
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
first;second
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
|
||||||
|
|
@ -1042,4 +1042,33 @@ else: # trailing comment
|
||||||
|
|
||||||
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_else_indented_comment_between_branches() {
|
||||||
|
let source = r"while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
";
|
||||||
|
let test_case = CommentsTestCase::from_code(source);
|
||||||
|
|
||||||
|
let comments = test_case.to_comments();
|
||||||
|
|
||||||
|
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_else_very_indented_comment_between_branches() {
|
||||||
|
let source = r"while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
";
|
||||||
|
let test_case = CommentsTestCase::from_code(source);
|
||||||
|
|
||||||
|
let comments = test_case.to_comments();
|
||||||
|
|
||||||
|
assert_debug_snapshot!(comments.debug(test_case.source_code));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use ruff_python_trivia::{
|
||||||
find_only_token_in_range, first_non_trivia_token, indentation_at_offset,
|
find_only_token_in_range, first_non_trivia_token, indentation_at_offset,
|
||||||
};
|
};
|
||||||
use ruff_source_file::LineRanges;
|
use ruff_source_file::LineRanges;
|
||||||
use ruff_text_size::{Ranged, TextLen, TextRange};
|
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
|
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
|
||||||
|
|
@ -602,9 +602,35 @@ fn handle_own_line_comment_between_branches<'a>(
|
||||||
// following branch or if it a trailing comment of the previous body's last statement.
|
// following branch or if it a trailing comment of the previous body's last statement.
|
||||||
let comment_indentation = comment_indentation_after(preceding, comment.range(), source);
|
let comment_indentation = comment_indentation_after(preceding, comment.range(), source);
|
||||||
|
|
||||||
let preceding_indentation = indentation(source, &preceding)
|
let preceding_indentation = indentation(source, &preceding).map_or_else(
|
||||||
.unwrap_or_default()
|
// If `indentation` returns `None`, then there is leading
|
||||||
.text_len();
|
// content before the preceding node. In this case, we
|
||||||
|
// always treat the comment as being less-indented than the
|
||||||
|
// preceding. For example:
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True: pass
|
||||||
|
// # leading on `else`
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
// Note we even do this if the comment is very indented
|
||||||
|
// (which matches `black`'s behavior as of 2025.11.11)
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True: pass
|
||||||
|
// # leading on `else`
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
|| {
|
||||||
|
comment_indentation
|
||||||
|
// This can be any positive number - we just
|
||||||
|
// want to hit the `Less` branch below
|
||||||
|
+ TextSize::new(1)
|
||||||
|
},
|
||||||
|
ruff_text_size::TextLen::text_len,
|
||||||
|
);
|
||||||
|
|
||||||
// Compare to the last statement in the body
|
// Compare to the last statement in the body
|
||||||
match comment_indentation.cmp(&preceding_indentation) {
|
match comment_indentation.cmp(&preceding_indentation) {
|
||||||
|
|
@ -678,8 +704,49 @@ fn handle_own_line_comment_after_branch<'a>(
|
||||||
preceding: AnyNodeRef<'a>,
|
preceding: AnyNodeRef<'a>,
|
||||||
source: &str,
|
source: &str,
|
||||||
) -> CommentPlacement<'a> {
|
) -> CommentPlacement<'a> {
|
||||||
let Some(last_child) = preceding.last_child_in_body() else {
|
// If the preceding node has a body, we want the last child - e.g.
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True:
|
||||||
|
// def foo():
|
||||||
|
// something
|
||||||
|
// last_child
|
||||||
|
// # comment
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// Otherwise, the preceding node may be the last statement in the body
|
||||||
|
// of the preceding branch, in which case we can take it as our
|
||||||
|
// `last_child` here - e.g.
|
||||||
|
//
|
||||||
|
// ```python
|
||||||
|
// if True:
|
||||||
|
// something
|
||||||
|
// last_child
|
||||||
|
// # comment
|
||||||
|
// else:
|
||||||
|
// pass
|
||||||
|
// ```
|
||||||
|
let last_child = match preceding.last_child_in_body() {
|
||||||
|
Some(last) => last,
|
||||||
|
None if matches!(
|
||||||
|
comment.enclosing_node(),
|
||||||
|
AnyNodeRef::StmtIf(_)
|
||||||
|
| AnyNodeRef::StmtWhile(_)
|
||||||
|
| AnyNodeRef::StmtFor(_)
|
||||||
|
| AnyNodeRef::StmtMatch(_)
|
||||||
|
| AnyNodeRef::ElifElseClause(_)
|
||||||
|
| AnyNodeRef::StmtTry(_)
|
||||||
|
| AnyNodeRef::MatchCase(_)
|
||||||
|
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||||
|
) =>
|
||||||
|
{
|
||||||
|
preceding
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
return CommentPlacement::Default(comment);
|
return CommentPlacement::Default(comment);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// We only care about the length because indentations with mixed spaces and tabs are only valid if
|
// We only care about the length because indentations with mixed spaces and tabs are only valid if
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_formatter/src/comments/mod.rs
|
||||||
|
expression: comments.debug(test_case.source_code)
|
||||||
|
---
|
||||||
|
{
|
||||||
|
Node {
|
||||||
|
kind: StmtWhile,
|
||||||
|
range: 0..45,
|
||||||
|
source: `while True: pass⏎`,
|
||||||
|
}: {
|
||||||
|
"leading": [],
|
||||||
|
"dangling": [
|
||||||
|
SourceComment {
|
||||||
|
text: "# comment",
|
||||||
|
position: OwnLine,
|
||||||
|
formatted: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"trailing": [],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_python_formatter/src/comments/mod.rs
|
||||||
|
expression: comments.debug(test_case.source_code)
|
||||||
|
---
|
||||||
|
{
|
||||||
|
Node {
|
||||||
|
kind: StmtPass,
|
||||||
|
range: 16..20,
|
||||||
|
source: `pass`,
|
||||||
|
}: {
|
||||||
|
"leading": [],
|
||||||
|
"dangling": [],
|
||||||
|
"trailing": [
|
||||||
|
SourceComment {
|
||||||
|
text: "# comment",
|
||||||
|
position: OwnLine,
|
||||||
|
formatted: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/form_feed.py
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/form_feed.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
## Input
|
## Input
|
||||||
```python
|
```python
|
||||||
|
|
@ -18,7 +17,7 @@ else:
|
||||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7624
|
# Regression test for: https://github.com/astral-sh/ruff/issues/7624
|
||||||
if symbol is not None:
|
if symbol is not None:
|
||||||
request["market"] = market["id"]
|
request["market"] = market["id"]
|
||||||
# "remaining_volume": "0.0",
|
# "remaining_volume": "0.0",
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/if.py
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/if.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
## Input
|
## Input
|
||||||
```python
|
```python
|
||||||
|
|
@ -301,6 +300,42 @@ if parent_body:
|
||||||
# d
|
# d
|
||||||
# e
|
# e
|
||||||
#f
|
#f
|
||||||
|
|
||||||
|
# Compare behavior with `while`/`else` comment placement
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
first;second
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
|
@ -607,6 +642,48 @@ if parent_body:
|
||||||
# d
|
# d
|
||||||
# e
|
# e
|
||||||
# f
|
# f
|
||||||
|
|
||||||
|
# Compare behavior with `while`/`else` comment placement
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
if True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if True:
|
||||||
|
first
|
||||||
|
second
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/while.py
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/while.py
|
||||||
snapshot_kind: text
|
|
||||||
---
|
---
|
||||||
## Input
|
## Input
|
||||||
```python
|
```python
|
||||||
|
|
@ -35,6 +34,40 @@ while (
|
||||||
and anotherCondition or aThirdCondition # trailing third condition
|
and anotherCondition or aThirdCondition # trailing third condition
|
||||||
): # comment
|
): # comment
|
||||||
print("Do something")
|
print("Do something")
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True: pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
first;second
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
|
@ -70,4 +103,44 @@ while (
|
||||||
or aThirdCondition # trailing third condition
|
or aThirdCondition # trailing third condition
|
||||||
): # comment
|
): # comment
|
||||||
print("Do something")
|
print("Do something")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
while True:
|
||||||
|
pass
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
first
|
||||||
|
second
|
||||||
|
# comment
|
||||||
|
else:
|
||||||
|
pass
|
||||||
```
|
```
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue