mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-02 04:48:06 +00:00
Format Slice Expressions (#5047)
This formats slice expressions and subscript expressions. Spaces around the colons follows the same rules as black (https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#slices): ```python e00 = "e"[:] e01 = "e"[:1] e02 = "e"[: a()] e10 = "e"[1:] e11 = "e"[1:1] e12 = "e"[1 : a()] e20 = "e"[a() :] e21 = "e"[a() : 1] e22 = "e"[a() : a()] e200 = "e"[a() : :] e201 = "e"[a() :: 1] e202 = "e"[a() :: a()] e210 = "e"[a() : 1 :] ``` Comment placement is different due to our very different infrastructure. If we have explicit bounds (e.g. `x[1:2]`) all comments get assigned as leading or trailing to the bound expression. If a bound is missing `[:]`, comments get marked as dangling and placed in the same section as they were originally in: ```python x = "x"[ # a # b : # c # d ] ``` to ```python x = "x"[ # a # b : # c # d ] ``` Except for the potential trailing end-of-line comments, all comments get formatted on their own line. This can be improved by keeping end-of-line comments after the opening bracket or after a colon as such but the changes were already complex enough. I added tests for comment placement and spaces.
This commit is contained in:
parent
4634560c80
commit
6155fd647d
22 changed files with 1065 additions and 430 deletions
82
crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/slice.py
vendored
Normal file
82
crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/slice.py
vendored
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
# Handle comments both when lower and upper exist and when they don't
|
||||
a1 = "a"[
|
||||
# a
|
||||
1 # b
|
||||
: # c
|
||||
2 # d
|
||||
]
|
||||
a2 = "a"[
|
||||
# a
|
||||
# b
|
||||
: # c
|
||||
# d
|
||||
]
|
||||
|
||||
# Check all places where comments can exist
|
||||
b1 = "b"[ # a
|
||||
# b
|
||||
1 # c
|
||||
# d
|
||||
: # e
|
||||
# f
|
||||
2 # g
|
||||
# h
|
||||
: # i
|
||||
# j
|
||||
3 # k
|
||||
# l
|
||||
]
|
||||
|
||||
# Handle the spacing from the colon correctly with upper leading comments
|
||||
c1 = "c"[
|
||||
1
|
||||
: # e
|
||||
# f
|
||||
2
|
||||
]
|
||||
c2 = "c"[
|
||||
1
|
||||
: # e
|
||||
2
|
||||
]
|
||||
c3 = "c"[
|
||||
1
|
||||
:
|
||||
# f
|
||||
2
|
||||
]
|
||||
c4 = "c"[
|
||||
1
|
||||
: # f
|
||||
2
|
||||
]
|
||||
|
||||
# End of line comments
|
||||
d1 = "d"[ # comment
|
||||
:
|
||||
]
|
||||
d2 = "d"[ # comment
|
||||
1:
|
||||
]
|
||||
d3 = "d"[
|
||||
1 # comment
|
||||
:
|
||||
]
|
||||
|
||||
# Spacing around the colon(s)
|
||||
def a():
|
||||
...
|
||||
|
||||
e00 = "e"[:]
|
||||
e01 = "e"[:1]
|
||||
e02 = "e"[: a()]
|
||||
e10 = "e"[1:]
|
||||
e11 = "e"[1:1]
|
||||
e12 = "e"[1 : a()]
|
||||
e20 = "e"[a() :]
|
||||
e21 = "e"[a() : 1]
|
||||
e22 = "e"[a() : a()]
|
||||
e200 = "e"[a() :: ]
|
||||
e201 = "e"[a() :: 1]
|
||||
e202 = "e"[a() :: a()]
|
||||
e210 = "e"[a() : 1 :]
|
||||
Loading…
Add table
Add a link
Reference in a new issue