Skip to content

Fix formatter behavior around + and () #294

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

Merged
merged 3 commits into from
Jan 29, 2017
Merged
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
36 changes: 33 additions & 3 deletions core/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,23 @@ class FixIndentation {
}
}

/** Calculate the indentation of sub-expressions.
*
* If the first sub-expression is on the same line as the current node, then subsequent
* ones will be lined up and further indentations in their subexpresssions will be based from
* this column. Otherwise, subseqeuent ones will be on the next line with no
* additional indent.
*/
Indent alignStrong(const Fodder &first_fodder, const Indent &old, unsigned line_up)
{
if (first_fodder.size() == 0 || first_fodder[0].kind == FodderElement::INTERSTITIAL) {
return Indent(line_up, line_up);
} else {
// Reset
return old;
}
}

/* Set indentation values within the fodder elements.
*
* The last one gets a special indentation value, all the others are set to the same thing.
Expand Down Expand Up @@ -1399,8 +1416,21 @@ class FixIndentation {

} else if (auto *ast = dynamic_cast<Binary*>(ast_)) {
const Fodder &first_fodder = open_fodder(ast->left);
Indent new_indent = align(first_fodder, indent,
column + (space_before ? 1 : 0));

// Need to use strong indent in the case of
/*
A
+ B
or
A +
B
*/
bool strong_indent = hasNewLines(ast->opFodder) || hasNewLines(open_fodder(ast->right));

unsigned inner_column = column + (space_before ? 1 : 0);
Indent new_indent = strong_indent
? alignStrong(first_fodder, indent, inner_column)
: align(first_fodder, indent, inner_column);
expr(ast->left, new_indent, space_before);
fill(ast->opFodder, true, true, new_indent.lineUp);
column += bop_string(ast->op).length();
Expand Down Expand Up @@ -1632,7 +1662,7 @@ class FixIndentation {

} else if (auto *ast = dynamic_cast<Parens*>(ast_)) {
column++; // (
Indent new_indent = newIndent(open_fodder(ast->expr), indent, column);
Indent new_indent = newIndentStrong(open_fodder(ast->expr), indent, column);
expr(ast->expr, new_indent, false);
fill(ast->closeFodder, false, false, new_indent.lineUp, indent.base);
column++; // )
Expand Down
160 changes: 81 additions & 79 deletions stdlib/std.jsonnet

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test_suite/error.equality_function.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RUNTIME ERROR: Cannot test equality of functions
std.jsonnet:1000:17-41 function <anonymous>
std.jsonnet:1002:17-41 function <anonymous>
error.equality_function.jsonnet:17:1-32
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_array.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_array.jsonnet:18:18-31 thunk <array_element>
std.jsonnet:980:41-44 thunk <b>
std.jsonnet:980:33-44 function <anonymous>
std.jsonnet:980:33-44 function <aux>
std.jsonnet:983:29-44 function <anonymous>
std.jsonnet:984:21-32
std.jsonnet:982:41-44 thunk <b>
std.jsonnet:982:33-44 function <anonymous>
std.jsonnet:982:33-44 function <aux>
std.jsonnet:985:29-44 function <anonymous>
std.jsonnet:986:21-32
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_object.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_object.jsonnet:18:22-35 object <b>
std.jsonnet:994:62-65 thunk <b>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <aux>
std.jsonnet:997:29-44 function <anonymous>
std.jsonnet:998:21-32
std.jsonnet:996:62-65 thunk <b>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:996:54-65 function <aux>
std.jsonnet:999:29-44 function <anonymous>
std.jsonnet:1000:21-32
8 changes: 4 additions & 4 deletions test_suite/error.invariant.equality.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.invariant.equality.jsonnet:17:10-14 thunk <object_assert>
std.jsonnet:994:54-57 thunk <a>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:998:21-32
std.jsonnet:996:54-57 thunk <a>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:1000:21-32
8 changes: 4 additions & 4 deletions test_suite/error.obj_assert.fail1.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.obj_assert.fail1.jsonnet:20:23-28 thunk <object_assert>
std.jsonnet:994:54-57 thunk <a>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:998:21-32
std.jsonnet:996:54-57 thunk <a>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:1000:21-32
8 changes: 4 additions & 4 deletions test_suite/error.obj_assert.fail2.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: foo was not equal to bar
error.obj_assert.fail2.jsonnet:20:32-64 thunk <object_assert>
std.jsonnet:994:54-57 thunk <a>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:998:21-32
std.jsonnet:996:54-57 thunk <a>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:996:54-65 function <anonymous>
std.jsonnet:1000:21-32
2 changes: 1 addition & 1 deletion test_suite/error.sanity.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RUNTIME ERROR: Assertion failed. 1 != 2
std.jsonnet:648:13-55 function <anonymous>
std.jsonnet:649:13-55 function <anonymous>
error.sanity.jsonnet:17:1-21
60 changes: 58 additions & 2 deletions test_suite/formatter.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ limitations under the License.
),

test_field11c: ([
"foo",
]),
"foo",
]),

test_field12: [
1,
Expand Down Expand Up @@ -298,6 +298,62 @@ limitations under the License.
['something']
else [],

strongIndent:: {
foo: ["foo"]
+ if true then [
"baz"
] else [],

bar: ["foo"]
+ if true then
["baz"]
else [],

fow: ["foo"] +
if true then [
"baz"
] else [],

baw: ["foo"] +
if true then
["baz"]
else [],

fox: std.equals(["foo"],
if true then [
"baz"
] else [],
),

bax: std.equals(["foo"],
if true then
["baz"]
else [],
),

foy: [["foo"],
if true then [
"baz"
] else [],
],

bay: [["foo"],
if true then
["baz"]
else [],
],

foz: (["foo"]
+ if true then [
"baz"
] else []),

baz: (["foo"]
+ if true then
["baz"]
else []),
},

user4: std.toString(
a='value1',
),
Expand Down
66 changes: 61 additions & 5 deletions test_suite/formatter.jsonnet.fmt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ limitations under the License.
),

test_field11c: ([
"foo",
]),
"foo",
]),

test_field12: [
1,
Expand Down Expand Up @@ -286,17 +286,73 @@ limitations under the License.

objects1():: ['something']
+ if true then ['something']
else [],
else [],

objects2():: ['something']
+ if true then
['something']
else [],
['something']
else [],

objects3():: ['something'] + if true then
['something']
else [],

strongIndent:: {
foo: ["foo"]
+ if true then [
"baz",
] else [],

bar: ["foo"]
+ if true then
["baz"]
else [],

fow: ["foo"] +
if true then [
"baz",
] else [],

baw: ["foo"] +
if true then
["baz"]
else [],

fox: std.equals(["foo"],
if true then [
"baz",
] else [],
),

bax: std.equals(["foo"],
if true then
["baz"]
else [],
),

foy: [["foo"],
if true then [
"baz",
] else [],
],

bay: [["foo"],
if true then
["baz"]
else [],
],

foz: (["foo"]
+ if true then [
"baz",
] else []),

baz: (["foo"]
+ if true then
["baz"]
else []),
},

user4: std.toString(
a='value1',
),
Expand Down