Skip to content

Commit

Permalink
Ignore the open parentheses when measuring BestFitsParenthesize
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 1, 2023
1 parent 7d948b2 commit d013b71
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 54 deletions.
9 changes: 5 additions & 4 deletions crates/ruff_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,11 +1451,12 @@ impl<Context> std::fmt::Debug for Group<'_, Context> {
}
}

/// Content that may get parenthesized if it exceeds the configured line width but only if the parenthesized
/// layout doesn't exceed the line width too, in which case it falls back to the flat layout.
/// Content that may get parenthesized if it exceeds the configured line width but only if the parenthesized content
/// doesn't exceed the line width too, in which case it falls back to the flat layout.
///
/// This IR is identical to the following [`best_fitting`] layout but is implemented as custom IR for
/// best performance.
/// This IR is almost identical to the following [`best_fitting`] layout but is implemented as custom IR for
/// best performance. It differs in that the open parentheses is ignored when testing if the content fits because
/// the goal is to only parenthesize the content if parenthesizing makes the content fit.
///
/// ```rust
/// # use ruff_formatter::prelude::*;
Expand Down
11 changes: 7 additions & 4 deletions crates/ruff_formatter/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ impl<'a> Printer<'a> {
args.with_measure_mode(MeasureMode::AllLines),
);

queue.extend_back(&[OPEN_PAREN, INDENT, HARD_LINE_BREAK]);
// Don't measure the opening parentheses. Only concerned whether parenthesizing
// makes the content fit.
queue.extend_back(&[INDENT, HARD_LINE_BREAK]);
let fits_expanded = self.fits(queue, stack)?;
queue.pop_slice();
stack.pop(TagKind::BestFitParenthesize)?;
Expand Down Expand Up @@ -1299,10 +1301,11 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
self.state.line_width = 0;
self.state.pending_indent = unindented.indention();

return Ok(self.fits_text(Text::Token(")"), unindented));
// Don't measure the `)` similar to the open parentheses but increment the line width and indent level.
self.fits_text(Text::Token(")"), unindented);
} else {
self.stack.pop(TagKind::BestFitParenthesize)?;
}

self.stack.pop(TagKind::BestFitParenthesize)?;
}

FormatElement::Tag(StartConditionalGroup(group)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
```diff
--- Black
+++ Ruff
@@ -49,9 +49,7 @@
element = 0 # type: int
@@ -50,8 +50,8 @@
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
- another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = (
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = (
- 3
- ) # type: int
+ another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = 3 # type: int
+ 3 # type: int
+ )
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
@@ -100,7 +98,13 @@
@@ -100,7 +100,13 @@
)
c = call(
Expand All @@ -156,7 +156,7 @@ aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*ite
)
@@ -108,11 +112,18 @@
@@ -108,11 +114,18 @@
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
Expand Down Expand Up @@ -233,7 +233,9 @@ def f(
element = 0 # type: int
another_element = 1 # type: float
another_element_with_long_name = 2 # type: int
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = 3 # type: int
another_really_really_long_element_with_a_unnecessarily_long_name_to_describe_what_it_does_enterprise_style = (
3 # type: int
)
an_element_with_a_long_value = calls() or more_calls() and more() # type: bool
tup = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def f(
```diff
--- Black
+++ Ruff
@@ -7,26 +7,16 @@
@@ -7,26 +7,18 @@
)
# "AnnAssign"s now also work
Expand Down Expand Up @@ -123,14 +123,16 @@ def f(
-z: Short | Short2 | Short3 | Short4 = 8
-z: int = 2.3
-z: int = foo()
+z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = 7
+z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = (
+ 7
+)
+z: (Short | Short2 | Short3 | Short4) = 8
+z: (int) = 2.3
+z: (int) = foo()
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
@@ -63,7 +53,7 @@
@@ -63,7 +55,7 @@
# remove unnecessary paren
Expand All @@ -139,7 +141,7 @@ def f(
# this is a syntax error in the type annotation according to mypy, but it's not invalid *python* code, so make sure we don't mess with it and make it so.
@@ -72,12 +62,10 @@
@@ -72,12 +64,10 @@
def foo(
i: int,
Expand All @@ -156,7 +158,7 @@ def f(
*,
s: str,
) -> None:
@@ -88,7 +76,7 @@
@@ -88,7 +78,7 @@
async def foo(
q: str | None = Query(
None, title="Some long title", description="Some long description"
Expand Down Expand Up @@ -185,7 +187,9 @@ z: (int)
z: (int)
z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = 7
z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = (
7
)
z: (Short | Short2 | Short3 | Short4) = 8
z: (int) = 2.3
z: (int) = foo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,23 @@ del concatenated_strings, string_variable_name, normal_function_name, normal_nam
```diff
--- Black
+++ Ruff
@@ -1,18 +1,12 @@
# long variable name
-this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
- 0
-)
-this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
- 1 # with a comment
-)
+this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = 0
+this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = 1 # with a comment
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = [
1,
2,
3,
]
-this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
- function()
-)
+this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function()
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
arg1, arg2, arg3
@@ -13,11 +13,11 @@
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
function()
)
@@ -35,10 +29,8 @@
-this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
- arg1, arg2, arg3
+this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
+ function(arg1, arg2, arg3)
)
-this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
- [1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
+this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
+ function([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
# long function name
normal_name = (
@@ -35,10 +35,8 @@
)
# long arguments
normal_name = normal_function_name(
Expand All @@ -92,19 +86,25 @@ del concatenated_strings, string_variable_name, normal_function_name, normal_nam

```python
# long variable name
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = 0
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = 1 # with a comment
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
0
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
1 # with a comment
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = [
1,
2,
3,
]
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function()
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
arg1, arg2, arg3
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
function()
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
[1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
function(arg1, arg2, arg3)
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
function([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
# long function name
normal_name = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ def f(*args, **kwargs):
pass
this_is_a_very_long_argument_asökdhflakjslaslhfdlaffahflahsöfdhasägporejfäalkdsjäfalisjäfdlkasjd = 1
this_is_a_very_long_argument_asökdhflakjslaslhfdlaffahflahsöfdhasägporejfäalkdsjäfalisjäfdlkasjd = (
1
)
session = MagicMock()
models = MagicMock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def foo():
yield (e) # Too many parentheses
# comment
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in l:
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in (
l
):
yield ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
for x in l: # comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ type X[T: int, *Ts, **P] = (T, Ts, P)
type X[T: (int, str), *Ts, **P] = (T, Ts, P)
# long name
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = (
int
)
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[
A
] = int
Expand All @@ -128,7 +130,9 @@ type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
Bbbbbbbbbbbbb,
] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = Tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = (
Tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
)
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = (
Tttttttttttttttttttttttttttttttttttttttttttttttttttttttt # with comment
)
Expand Down

0 comments on commit d013b71

Please sign in to comment.