mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 18:02:23 +00:00
Rewrite placement logic (#6040)
## Summary This is a rewrite of the main comment placement logic. `place_comment` now has three parts: - place own line comments - between branches - after a branch - place end-of-line comments - after colon - after a branch - place comments for specific nodes (that include module level comments) The rewrite fixed three bugs: `class A: # trailing comment` comments now stay end-of-line, `try: # comment` remains end-of-line and deeply indented try-else-finally comments remain with the right nested statement. It will be much easier to give more alternative branches nodes since this is abstracted away by `is_node_with_body` and the first/last child helpers. Adding new node types can now be done by adding an entry to the `place_comment` match. The code went from 1526 lines before #6033 to 1213 lines now. It thinks it easier to just read the new `placement.rs` rather than reviewing the diff. ## Test Plan The existing fixtures staying the same or improving plus new ones for the bug fixes.
This commit is contained in:
parent
2cf00fee96
commit
13f9a16e33
14 changed files with 541 additions and 673 deletions
|
@ -35,5 +35,11 @@ class Test(aaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccc +
|
|||
class Test(aaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb * cccccccccccccccccccccccc + dddddddddddddddddddddd + eeeeeeeee, ffffffffffffffffff, gggggggggggggggggg):
|
||||
pass
|
||||
|
||||
class Test(Aaaa): # trailing comment
|
||||
class TestTrailingComment1(Aaaa): # trailing comment
|
||||
pass
|
||||
|
||||
|
||||
class TestTrailingComment2: # trailing comment
|
||||
pass
|
||||
|
||||
|
||||
|
|
|
@ -88,13 +88,17 @@ def f():
|
|||
# comment
|
||||
|
||||
if True:
|
||||
def f2():
|
||||
def f():
|
||||
pass
|
||||
# 1
|
||||
else:
|
||||
def f2():
|
||||
elif True:
|
||||
def f():
|
||||
pass
|
||||
# 2
|
||||
else:
|
||||
def f():
|
||||
pass
|
||||
# 3
|
||||
|
||||
if True: print("a") # 1
|
||||
elif True: print("b") # 2
|
||||
|
|
|
@ -106,16 +106,35 @@ except RuntimeError:
|
|||
raise
|
||||
|
||||
try:
|
||||
def f2():
|
||||
def f():
|
||||
pass
|
||||
# a
|
||||
except:
|
||||
def f2():
|
||||
def f():
|
||||
pass
|
||||
# b
|
||||
else:
|
||||
def f():
|
||||
pass
|
||||
# c
|
||||
finally:
|
||||
def f():
|
||||
pass
|
||||
# d
|
||||
|
||||
try: pass # a
|
||||
except ZeroDivisionError: pass # b
|
||||
except: pass # b
|
||||
except: pass # c
|
||||
else: pass # d
|
||||
finally: pass # c
|
||||
finally: pass # e
|
||||
|
||||
try: # 1 preceding: any, following: first in body, enclosing: try
|
||||
print(1) # 2 preceding: last in body, following: fist in alt body, enclosing: try
|
||||
except ZeroDivisionError: # 3 preceding: test, following: fist in alt body, enclosing: try
|
||||
print(2) # 4 preceding: last in body, following: fist in alt body, enclosing: exc
|
||||
except: # 5 preceding: last in body, following: fist in alt body, enclosing: try
|
||||
print(2) # 6 preceding: last in body, following: fist in alt body, enclosing: exc
|
||||
else: # 7 preceding: last in body, following: fist in alt body, enclosing: exc
|
||||
print(3) # 8 preceding: last in body, following: fist in alt body, enclosing: try
|
||||
finally: # 9 preceding: last in body, following: fist in alt body, enclosing: try
|
||||
print(3) # 10 preceding: last in body, following: any, enclosing: try
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue