Range formatting: Fix invalid syntax after parenthesizing expression (#9751)

This commit is contained in:
Micha Reiser 2024-02-02 17:56:25 +01:00 committed by GitHub
parent 50bfbcf568
commit 4f7fb566f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 351 additions and 212 deletions

View file

@ -0,0 +1,14 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/end_of_file.py
---
## Input
```python
a + b<RANGE_START><RANGE_END>```
## Output
```python
a + b```

View file

@ -0,0 +1,41 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/parentheses.py
---
## Input
```python
def needs_parentheses( ) -> bool:
return item.sizing_mode is None and <RANGE_START>item.width_policy == "auto" and item.height_policy == "automatic"<RANGE_END>
def no_longer_needs_parentheses( ) -> bool:
return (
<RANGE_START>item.width_policy == "auto"
and item.height_policy == "automatic"<RANGE_END>
)
def format_range_after_inserted_parens ():
a and item.sizing_mode is None and item.width_policy == "auto" and item.height_policy == "automatic"<RANGE_START>
print("Format this" ) <RANGE_END>
```
## Output
```python
def needs_parentheses( ) -> bool:
return (
item.sizing_mode is None
and item.width_policy == "auto"
and item.height_policy == "automatic"
)
def no_longer_needs_parentheses( ) -> bool:
return item.width_policy == "auto" and item.height_policy == "automatic"
def format_range_after_inserted_parens ():
a and item.sizing_mode is None and item.width_policy == "auto" and item.height_policy == "automatic"
print("Format this")
```

View file

@ -73,6 +73,22 @@ def convert_str(value: str) -> str: # Trailing comment
<RANGE_END>
def test ():
pass
def test_comment_indent():
<RANGE_START># A misaligned comment<RANGE_END>
print("test")
# This demonstrates the use case where a user inserts a new function or class after an existing function.
# In this case, we should avoid formatting the node that directly precedes the new function/class.
# However, the problem is that the preceding node **must** be formatted to determine the whitespace between the two statements.
def test_start ():
print("Ideally this gets not reformatted" )
<RANGE_START>
def new_function_inserted_after_test_start ():
print("This should get formatted" )<RANGE_END>
```
## Output
@ -133,6 +149,22 @@ def convert_str(value: str) -> str: # Trailing comment
def test ():
pass
def test_comment_indent():
# A misaligned comment
print("test")
# This demonstrates the use case where a user inserts a new function or class after an existing function.
# In this case, we should avoid formatting the node that directly precedes the new function/class.
# However, the problem is that the preceding node **must** be formatted to determine the whitespace between the two statements.
def test_start ():
print("Ideally this gets not reformatted" )
def new_function_inserted_after_test_start():
print("This should get formatted")
```