mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Range formatting: Fix invalid syntax after parenthesizing expression (#9751)
This commit is contained in:
parent
50bfbcf568
commit
4f7fb566f0
24 changed files with 351 additions and 212 deletions
1
crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/end_of_file.py
vendored
Normal file
1
crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/end_of_file.py
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
a + b<RANGE_START><RANGE_END>
|
13
crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/parentheses.py
vendored
Normal file
13
crates/ruff_python_formatter/resources/test/fixtures/ruff/range_formatting/parentheses.py
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
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>
|
|
@ -67,3 +67,19 @@ 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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue