Skip to content

Commit

Permalink
Preserve trailing semicolons when using fmt: off
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Oct 27, 2023
1 parent 538867c commit eb5ab97
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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 ;
12 changes: 6 additions & 6 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ if True:
#[test]
fn quick_test() {
let source = r#"
def main() -> None:
def f():
# fmt: off
if True:
some_very_long_variable_name_abcdefghijk = Foo()
some_very_long_variable_name_abcdefghijk = some_very_long_variable_name_abcdefghijk[
some_very_long_variable_name_abcdefghijk.some_very_long_attribute_name
== "This is a very long string abcdefghijk"
]
with_semicolon = 20 \
; # comment
"#;
let source_type = PySourceType::Python;
Expand Down
20 changes: 12 additions & 8 deletions crates/ruff_python_formatter/src/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) = AnyNodeRef::from(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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ;
```



0 comments on commit eb5ab97

Please sign in to comment.