Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break before slice colon #7332

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,18 @@ def a():
g1 = "g"[(1):(2)]
g2 = "g"[(1):(2):(3)]

# https://github.com/astral-sh/ruff/issues/7316
section_header_data = byte_array[
byte_begin_index
+ byte_step_index * event_index : byte_begin_index
+ byte_step_index * (event_index + 1)
]

section_header_data2 = byte_array[
byte_begin_index
+ byte_step_index * event_index : byte_begin_index
+ byte_step_index : section_size
]



4 changes: 2 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
if !all_simple && lower.is_some() {
space().fmt(f)?;
}
token(":").fmt(f)?;
write!(f, [soft_line_break(), token(":")])?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this into the else branch or we may end up with an unintentional a \n<space>

// No upper node, no need for a space, e.g. `x[a() :]`
if !all_simple && upper.is_some() {
space().fmt(f)?;
Expand Down Expand Up @@ -125,7 +125,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
if !all_simple && (upper.is_some() || step.is_none()) {
space().fmt(f)?;
}
token(":").fmt(f)?;
write!(f, [soft_line_break(), token(":")])?;
// No step node, no need for a space
if !all_simple && step.is_some() {
space().fmt(f)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ f = "f"[:,]
g1 = "g"[(1):(2)]
g2 = "g"[(1):(2):(3)]

# https://github.com/astral-sh/ruff/issues/7316
section_header_data = byte_array[
byte_begin_index
+ byte_step_index * event_index : byte_begin_index
+ byte_step_index * (event_index + 1)
]

section_header_data2 = byte_array[
byte_begin_index
+ byte_step_index * event_index : byte_begin_index
+ byte_step_index : section_size
]



```

## Output
Expand Down Expand Up @@ -132,21 +147,25 @@ b1 = "b"[ # a

# Handle the spacing from the colon correctly with upper leading comments
c1 = "c"[
1: # e
1
: # e
# f
2
]
c2 = "c"[
1: # e
1
: # e
2
]
c3 = "c"[
1:
1
:
# f
2
]
c4 = "c"[
1: # f
1
: # f
2
]

Expand All @@ -155,7 +174,8 @@ d1 = "d"[ # comment
:
]
d2 = "d"[ # comment
1:
1
:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this isn't ideal. Do we need to group the line break with the left side so that it only renders if the left expands (or the : and the right side don't fit on the line)

]
d3 = "d"[
1 # comment
Expand Down Expand Up @@ -191,6 +211,18 @@ f = "f"[:,]
# Regression test for https://github.com/astral-sh/ruff/issues/5733
g1 = "g"[(1):(2)]
g2 = "g"[(1):(2):(3)]

# https://github.com/astral-sh/ruff/issues/7316
section_header_data = byte_array[
byte_begin_index + byte_step_index * event_index
: byte_begin_index + byte_step_index * (event_index + 1)
]

section_header_data2 = byte_array[
byte_begin_index + byte_step_index * event_index
: byte_begin_index + byte_step_index
: section_size
]
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ raise ( # hey 2

# some comment
raise aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:bbbbbbbbbbbbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
:bbbbbbbbbbbbbbbbbbbbbbbbbb
]

raise (
Expand Down