mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:26:23 +00:00
Preserve trailing semicolons when using fmt: off
(#8275)
This commit is contained in:
parent
2c84f911c4
commit
3ccca332bd
3 changed files with 108 additions and 8 deletions
27
crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/trailing_semicolon.py
vendored
Normal file
27
crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/trailing_semicolon.py
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
def f():
|
||||
# fmt: off
|
||||
a = 10
|
||||
|
||||
if True:
|
||||
with_semicolon = 10 \
|
||||
;
|
||||
|
||||
formatted = true;
|
||||
|
||||
|
||||
def f():
|
||||
# fmt: off
|
||||
|
||||
if True:
|
||||
with_semicolon = 20 \
|
||||
; # comment
|
||||
|
||||
|
||||
# fmt: off
|
||||
statement = 0 \
|
||||
;
|
||||
# fmt: on
|
||||
a = 10
|
||||
|
||||
# fmt: off
|
||||
last_statement_with_semi ;
|
|
@ -395,14 +395,18 @@ fn write_suppressed_statements<'a>(
|
|||
statement = SuiteChildStatement::Other(next_statement);
|
||||
leading_node_comments = comments.leading(next_statement);
|
||||
} else {
|
||||
let mut nodes =
|
||||
std::iter::successors(Some(AnyNodeRef::from(statement.statement())), |statement| {
|
||||
statement.last_child_in_body()
|
||||
});
|
||||
|
||||
let end = nodes
|
||||
.find_map(|statement| comments.trailing(statement).last().map(Ranged::end))
|
||||
.unwrap_or(statement.end());
|
||||
let mut current = AnyNodeRef::from(statement.statement());
|
||||
// Expand the range of the statement to include any trailing comments or semicolons.
|
||||
let end = loop {
|
||||
if let Some(comment) = comments.trailing(current).last() {
|
||||
break comment.end();
|
||||
} else if let Some(child) = current.last_child_in_body() {
|
||||
current = child;
|
||||
} else {
|
||||
break trailing_semicolon(current, source)
|
||||
.map_or(statement.end(), TextRange::end);
|
||||
}
|
||||
};
|
||||
|
||||
FormatVerbatimStatementRange {
|
||||
verbatim_range: TextRange::new(format_off_comment.end(), end),
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/trailing_semicolon.py
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
def f():
|
||||
# fmt: off
|
||||
a = 10
|
||||
|
||||
if True:
|
||||
with_semicolon = 10 \
|
||||
;
|
||||
|
||||
formatted = true;
|
||||
|
||||
|
||||
def f():
|
||||
# fmt: off
|
||||
|
||||
if True:
|
||||
with_semicolon = 20 \
|
||||
; # comment
|
||||
|
||||
|
||||
# fmt: off
|
||||
statement = 0 \
|
||||
;
|
||||
# fmt: on
|
||||
a = 10
|
||||
|
||||
# fmt: off
|
||||
last_statement_with_semi ;
|
||||
```
|
||||
|
||||
## Output
|
||||
```py
|
||||
def f():
|
||||
# fmt: off
|
||||
a = 10
|
||||
|
||||
if True:
|
||||
with_semicolon = 10 \
|
||||
;
|
||||
|
||||
|
||||
formatted = true
|
||||
|
||||
|
||||
def f():
|
||||
# fmt: off
|
||||
|
||||
if True:
|
||||
with_semicolon = 20 \
|
||||
; # comment
|
||||
|
||||
|
||||
# fmt: off
|
||||
statement = 0 \
|
||||
;
|
||||
# fmt: on
|
||||
a = 10
|
||||
|
||||
# fmt: off
|
||||
last_statement_with_semi ;
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue