Skip to content

Commit

Permalink
fmt ExprDict: group k/v pairs
Browse files Browse the repository at this point in the history
group key/value pairs when formatting dict to prefer breaking lines
between entries instead of inside them
  • Loading branch information
davidszotten committed Jun 19, 2023
1 parent fe781f2 commit 4378cfe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 75 deletions.
12 changes: 10 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ struct KvPair<'a> {
impl Format<PyFormatContext<'_>> for KvPair<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
if let Some(key) = self.key {
write!(f, [key.format(), text(":"), space(), self.value.format()])
write!(
f,
[group(&format_args![
key.format(),
text(":"),
space(),
self.value.format()
])]
)
} else {
write!(f, [text("**"), self.value.format()])
write!(f, [group(&format_args![text("**"), self.value.format()])])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ last_call()
Name
None
True
@@ -7,294 +8,268 @@
@@ -7,226 +8,225 @@
1
1.0
1j
Expand Down Expand Up @@ -630,8 +630,7 @@ last_call()
+ "NOT_YET_IMPLEMENTED_STRING": "NOT_YET_IMPLEMENTED_STRING",
+ "NOT_YET_IMPLEMENTED_STRING": "NOT_YET_IMPLEMENTED_STRING",
+ "NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call(),
+ "NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call()
+ + NOT_IMPLEMENTED_call(),
+ "NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call() + NOT_IMPLEMENTED_call(),
+ "NOT_YET_IMPLEMENTED_STRING": 1,
+ "NOT_YET_IMPLEMENTED_STRING": 1,
**kwargs,
Expand Down Expand Up @@ -683,19 +682,9 @@ last_call()
+Ø = NOT_IMPLEMENTED_call()
+NOT_IMPLEMENTED_call()
mapping = {
- A: 0.25 * (10.0 / 12),
- B: 0.1 * (10.0 / 12),
- C: 0.1 * (10.0 / 12),
- D: 0.1 * (10.0 / 12),
+ A: 0.25
+ * (10.0 / 12),
+ B: 0.1
+ * (10.0 / 12),
+ C: 0.1
+ * (10.0 / 12),
+ D: 0.1
+ * (10.0 / 12),
}
A: 0.25 * (10.0 / 12),
B: 0.1 * (10.0 / 12),
@@ -236,65 +236,35 @@
def gen():
Expand Down Expand Up @@ -725,14 +714,6 @@ last_call()
-for (x,) in (1,), (2,), (3,):
- ...
-for y in ():
- ...
-for z in (i for i in (1, 2, 3)):
- ...
-for i in call():
- ...
-for j in 1 + (2 + 3):
- ...
-while this and that:
+NOT_IMPLEMENTED_call()
+NOT_IMPLEMENTED_call()
+NOT_IMPLEMENTED_call()
Expand All @@ -746,6 +727,14 @@ last_call()
+NOT_YET_IMPLEMENTED_StmtFor
+while NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2:
...
-for z in (i for i in (1, 2, 3)):
- ...
-for i in call():
- ...
-for j in 1 + (2 + 3):
- ...
-while this and that:
- ...
-for (
- addr_family,
- addr_type,
Expand Down Expand Up @@ -784,7 +773,7 @@ last_call()
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
@@ -327,24 +302,44 @@
@@ -327,24 +297,44 @@
):
return True
if (
Expand Down Expand Up @@ -841,7 +830,7 @@ last_call()
):
return True
(
@@ -363,8 +358,9 @@
@@ -363,8 +353,9 @@
bbbb >> bbbb * bbbb
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Expand Down Expand Up @@ -1063,8 +1052,7 @@ SomeName
"NOT_YET_IMPLEMENTED_STRING": "NOT_YET_IMPLEMENTED_STRING",
"NOT_YET_IMPLEMENTED_STRING": "NOT_YET_IMPLEMENTED_STRING",
"NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call(),
"NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call()
+ NOT_IMPLEMENTED_call(),
"NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call() + NOT_IMPLEMENTED_call(),
"NOT_YET_IMPLEMENTED_STRING": 1,
"NOT_YET_IMPLEMENTED_STRING": 1,
**kwargs,
Expand All @@ -1089,14 +1077,10 @@ result = NOT_IMPLEMENTED_call()
Ø = NOT_IMPLEMENTED_call()
NOT_IMPLEMENTED_call()
mapping = {
A: 0.25
* (10.0 / 12),
B: 0.1
* (10.0 / 12),
C: 0.1
* (10.0 / 12),
D: 0.1
* (10.0 / 12),
A: 0.25 * (10.0 / 12),
B: 0.1 * (10.0 / 12),
C: 0.1 * (10.0 / 12),
D: 0.1 * (10.0 / 12),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ def __await__(): return (yield)
#!/usr/bin/env python3
-import asyncio
-import sys
-
-from third_party import X, Y, Z
+NOT_YET_IMPLEMENTED_StmtImport
+NOT_YET_IMPLEMENTED_StmtImport

-from library import some_connection, some_decorator
-from third_party import X, Y, Z
+NOT_YET_IMPLEMENTED_StmtImportFrom

-from library import some_connection, some_decorator
-
-f"trigger 3.6 mode"
+NOT_YET_IMPLEMENTED_StmtImportFrom
+NOT_YET_IMPLEMENTED_ExprJoinedStr
Expand Down Expand Up @@ -189,7 +189,7 @@ def __await__(): return (yield)


def spaces_types(
@@ -55,79 +61,39 @@
@@ -55,71 +61,27 @@
c: list = [],
d: dict = {},
e: bool = True,
Expand Down Expand Up @@ -271,23 +271,7 @@ def __await__(): return (yield)


def trailing_comma():
mapping = {
- A: 0.25 * (10.0 / 12),
- B: 0.1 * (10.0 / 12),
- C: 0.1 * (10.0 / 12),
- D: 0.1 * (10.0 / 12),
+ A: 0.25
+ * (10.0 / 12),
+ B: 0.1
+ * (10.0 / 12),
+ C: 0.1
+ * (10.0 / 12),
+ D: 0.1
+ * (10.0 / 12),
}


@@ -135,14 +101,8 @@
@@ -135,14 +97,8 @@
a,
**kwargs,
) -> A:
Expand Down Expand Up @@ -397,14 +381,10 @@ def long_lines():

def trailing_comma():
mapping = {
A: 0.25
* (10.0 / 12),
B: 0.1
* (10.0 / 12),
C: 0.1
* (10.0 / 12),
D: 0.1
* (10.0 / 12),
A: 0.25 * (10.0 / 12),
B: 0.1 * (10.0 / 12),
C: 0.1 * (10.0 / 12),
D: 0.1 * (10.0 / 12),
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ mapping = {
}
mapping = {
A: 0.25
* (10.0 / 12),
B: 0.1
* (10.0 / 12),
C: 0.1
* (10.0 / 12),
D: 0.1
* (10.0 / 12),
A: 0.25 * (10.0 / 12),
B: 0.1 * (10.0 / 12),
C: 0.1 * (10.0 / 12),
D: 0.1 * (10.0 / 12),
}
```

Expand Down

0 comments on commit 4378cfe

Please sign in to comment.