Skip to content

Commit 0613740

Browse files
authored
Merge pull request #294 from sparkprime/fix_formatter_named_arg_newline
Fix formatter behavior around + and ()
2 parents 7dee7f8 + bf3e11a commit 0613740

11 files changed

+257
-113
lines changed

core/formatter.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,23 @@ class FixIndentation {
10811081
}
10821082
}
10831083

1084+
/** Calculate the indentation of sub-expressions.
1085+
*
1086+
* If the first sub-expression is on the same line as the current node, then subsequent
1087+
* ones will be lined up and further indentations in their subexpresssions will be based from
1088+
* this column. Otherwise, subseqeuent ones will be on the next line with no
1089+
* additional indent.
1090+
*/
1091+
Indent alignStrong(const Fodder &first_fodder, const Indent &old, unsigned line_up)
1092+
{
1093+
if (first_fodder.size() == 0 || first_fodder[0].kind == FodderElement::INTERSTITIAL) {
1094+
return Indent(line_up, line_up);
1095+
} else {
1096+
// Reset
1097+
return old;
1098+
}
1099+
}
1100+
10841101
/* Set indentation values within the fodder elements.
10851102
*
10861103
* The last one gets a special indentation value, all the others are set to the same thing.
@@ -1399,8 +1416,21 @@ class FixIndentation {
13991416

14001417
} else if (auto *ast = dynamic_cast<Binary*>(ast_)) {
14011418
const Fodder &first_fodder = open_fodder(ast->left);
1402-
Indent new_indent = align(first_fodder, indent,
1403-
column + (space_before ? 1 : 0));
1419+
1420+
// Need to use strong indent in the case of
1421+
/*
1422+
A
1423+
+ B
1424+
or
1425+
A +
1426+
B
1427+
*/
1428+
bool strong_indent = hasNewLines(ast->opFodder) || hasNewLines(open_fodder(ast->right));
1429+
1430+
unsigned inner_column = column + (space_before ? 1 : 0);
1431+
Indent new_indent = strong_indent
1432+
? alignStrong(first_fodder, indent, inner_column)
1433+
: align(first_fodder, indent, inner_column);
14041434
expr(ast->left, new_indent, space_before);
14051435
fill(ast->opFodder, true, true, new_indent.lineUp);
14061436
column += bop_string(ast->op).length();
@@ -1632,7 +1662,7 @@ class FixIndentation {
16321662

16331663
} else if (auto *ast = dynamic_cast<Parens*>(ast_)) {
16341664
column++; // (
1635-
Indent new_indent = newIndent(open_fodder(ast->expr), indent, column);
1665+
Indent new_indent = newIndentStrong(open_fodder(ast->expr), indent, column);
16361666
expr(ast->expr, new_indent, false);
16371667
fill(ast->closeFodder, false, false, new_indent.lineUp, indent.base);
16381668
column++; // )

stdlib/std.jsonnet

Lines changed: 81 additions & 79 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: Cannot test equality of functions
2-
std.jsonnet:1000:17-41 function <anonymous>
2+
std.jsonnet:1002:17-41 function <anonymous>
33
error.equality_function.jsonnet:17:1-32
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_array.jsonnet:18:18-31 thunk <array_element>
3-
std.jsonnet:980:41-44 thunk <b>
4-
std.jsonnet:980:33-44 function <anonymous>
5-
std.jsonnet:980:33-44 function <aux>
6-
std.jsonnet:983:29-44 function <anonymous>
7-
std.jsonnet:984:21-32
3+
std.jsonnet:982:41-44 thunk <b>
4+
std.jsonnet:982:33-44 function <anonymous>
5+
std.jsonnet:982:33-44 function <aux>
6+
std.jsonnet:985:29-44 function <anonymous>
7+
std.jsonnet:986:21-32
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_object.jsonnet:18:22-35 object <b>
3-
std.jsonnet:994:62-65 thunk <b>
4-
std.jsonnet:994:54-65 function <anonymous>
5-
std.jsonnet:994:54-65 function <aux>
6-
std.jsonnet:997:29-44 function <anonymous>
7-
std.jsonnet:998:21-32
3+
std.jsonnet:996:62-65 thunk <b>
4+
std.jsonnet:996:54-65 function <anonymous>
5+
std.jsonnet:996:54-65 function <aux>
6+
std.jsonnet:999:29-44 function <anonymous>
7+
std.jsonnet:1000:21-32
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.invariant.equality.jsonnet:17:10-14 thunk <object_assert>
3-
std.jsonnet:994:54-57 thunk <a>
4-
std.jsonnet:994:54-65 function <anonymous>
5-
std.jsonnet:994:54-65 function <anonymous>
6-
std.jsonnet:998:21-32
3+
std.jsonnet:996:54-57 thunk <a>
4+
std.jsonnet:996:54-65 function <anonymous>
5+
std.jsonnet:996:54-65 function <anonymous>
6+
std.jsonnet:1000:21-32
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.obj_assert.fail1.jsonnet:20:23-28 thunk <object_assert>
3-
std.jsonnet:994:54-57 thunk <a>
4-
std.jsonnet:994:54-65 function <anonymous>
5-
std.jsonnet:994:54-65 function <anonymous>
6-
std.jsonnet:998:21-32
3+
std.jsonnet:996:54-57 thunk <a>
4+
std.jsonnet:996:54-65 function <anonymous>
5+
std.jsonnet:996:54-65 function <anonymous>
6+
std.jsonnet:1000:21-32
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: foo was not equal to bar
22
error.obj_assert.fail2.jsonnet:20:32-64 thunk <object_assert>
3-
std.jsonnet:994:54-57 thunk <a>
4-
std.jsonnet:994:54-65 function <anonymous>
5-
std.jsonnet:994:54-65 function <anonymous>
6-
std.jsonnet:998:21-32
3+
std.jsonnet:996:54-57 thunk <a>
4+
std.jsonnet:996:54-65 function <anonymous>
5+
std.jsonnet:996:54-65 function <anonymous>
6+
std.jsonnet:1000:21-32
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: Assertion failed. 1 != 2
2-
std.jsonnet:648:13-55 function <anonymous>
2+
std.jsonnet:649:13-55 function <anonymous>
33
error.sanity.jsonnet:17:1-21

test_suite/formatter.jsonnet

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ limitations under the License.
201201
),
202202

203203
test_field11c: ([
204-
"foo",
205-
]),
204+
"foo",
205+
]),
206206

207207
test_field12: [
208208
1,
@@ -298,6 +298,62 @@ limitations under the License.
298298
['something']
299299
else [],
300300

301+
strongIndent:: {
302+
foo: ["foo"]
303+
+ if true then [
304+
"baz"
305+
] else [],
306+
307+
bar: ["foo"]
308+
+ if true then
309+
["baz"]
310+
else [],
311+
312+
fow: ["foo"] +
313+
if true then [
314+
"baz"
315+
] else [],
316+
317+
baw: ["foo"] +
318+
if true then
319+
["baz"]
320+
else [],
321+
322+
fox: std.equals(["foo"],
323+
if true then [
324+
"baz"
325+
] else [],
326+
),
327+
328+
bax: std.equals(["foo"],
329+
if true then
330+
["baz"]
331+
else [],
332+
),
333+
334+
foy: [["foo"],
335+
if true then [
336+
"baz"
337+
] else [],
338+
],
339+
340+
bay: [["foo"],
341+
if true then
342+
["baz"]
343+
else [],
344+
],
345+
346+
foz: (["foo"]
347+
+ if true then [
348+
"baz"
349+
] else []),
350+
351+
baz: (["foo"]
352+
+ if true then
353+
["baz"]
354+
else []),
355+
},
356+
301357
user4: std.toString(
302358
a='value1',
303359
),

test_suite/formatter.jsonnet.fmt.golden

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ limitations under the License.
200200
),
201201

202202
test_field11c: ([
203-
"foo",
204-
]),
203+
"foo",
204+
]),
205205

206206
test_field12: [
207207
1,
@@ -286,17 +286,73 @@ limitations under the License.
286286

287287
objects1():: ['something']
288288
+ if true then ['something']
289-
else [],
289+
else [],
290290

291291
objects2():: ['something']
292292
+ if true then
293-
['something']
294-
else [],
293+
['something']
294+
else [],
295295

296296
objects3():: ['something'] + if true then
297297
['something']
298298
else [],
299299

300+
strongIndent:: {
301+
foo: ["foo"]
302+
+ if true then [
303+
"baz",
304+
] else [],
305+
306+
bar: ["foo"]
307+
+ if true then
308+
["baz"]
309+
else [],
310+
311+
fow: ["foo"] +
312+
if true then [
313+
"baz",
314+
] else [],
315+
316+
baw: ["foo"] +
317+
if true then
318+
["baz"]
319+
else [],
320+
321+
fox: std.equals(["foo"],
322+
if true then [
323+
"baz",
324+
] else [],
325+
),
326+
327+
bax: std.equals(["foo"],
328+
if true then
329+
["baz"]
330+
else [],
331+
),
332+
333+
foy: [["foo"],
334+
if true then [
335+
"baz",
336+
] else [],
337+
],
338+
339+
bay: [["foo"],
340+
if true then
341+
["baz"]
342+
else [],
343+
],
344+
345+
foz: (["foo"]
346+
+ if true then [
347+
"baz",
348+
] else []),
349+
350+
baz: (["foo"]
351+
+ if true then
352+
["baz"]
353+
else []),
354+
},
355+
300356
user4: std.toString(
301357
a='value1',
302358
),

0 commit comments

Comments
 (0)