ruff/crates/ruff_python_formatter/src
konstin 6155fd647d
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.
2023-06-21 15:09:39 +00:00
..
comments Format Slice Expressions (#5047) 2023-06-21 15:09:39 +00:00
expression Format Slice Expressions (#5047) 2023-06-21 15:09:39 +00:00
module Run rustfmt on nightly to clean up erroneous comments (#5106) 2023-06-15 00:19:05 +00:00
other Fix ArgWithDefault comments handling (#5204) 2023-06-20 20:48:07 +00:00
pattern Use dedicated structs in comparable.rs (#5042) 2023-06-13 03:57:34 +00:00
snapshots Format Slice Expressions (#5047) 2023-06-21 15:09:39 +00:00
statement Consistently name comment own line/end-of-line line_position() (#5215) 2023-06-21 11:04:56 +02:00
builders.rs Add basic Constant formatting (#4954) 2023-06-08 11:42:44 +00:00
cli.rs Fix ArgWithDefault comments handling (#5204) 2023-06-20 20:48:07 +00:00
context.rs Format binary expressions (#4862) 2023-06-06 08:34:53 +00:00
generated.rs Upgrade RustPython (#5192) 2023-06-19 21:09:53 +00:00
lib.rs Format Slice Expressions (#5047) 2023-06-21 15:09:39 +00:00
main.rs Add a formatter CLI for debugging (#4809) 2023-06-05 07:33:33 +00:00
prelude.rs Accept any Into<AnyNodeRef> as Comments arguments (#5205) 2023-06-20 16:49:21 +00:00
trivia.rs Format Slice Expressions (#5047) 2023-06-21 15:09:39 +00:00