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

Fix function application named arg on new line #290

Merged
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
25 changes: 12 additions & 13 deletions core/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,6 @@ static Fodder &open_fodder(AST *ast_)
AST *left = left_recursive(ast_);
return left != nullptr ? open_fodder(left) : ast_->openFodder;
}
static const Fodder &open_fodder(const AST *ast_)
{
return open_fodder(const_cast<AST*>(ast_));
}

/** Strip blank lines from the top of the file. */
void remove_initial_newlines(AST *ast)
Expand Down Expand Up @@ -1267,10 +1263,11 @@ class FixIndentation {
return false;
}

/** Will at least one new line precede this AST? */
bool hasNewLines(const AST *expr)
/** Get the first fodder from an ArgParam. */
const Fodder &argParamFirstFodder(const ArgParam &ap)
{
return hasNewLines(open_fodder(expr));
if (ap.id != nullptr) return ap.idFodder;
return open_fodder(ap.expr);
}

/** Reindent an expression.
Expand All @@ -1290,18 +1287,19 @@ class FixIndentation {
expr(ast->target, new_indent, space_before);
fill(ast->fodderL, false, false, new_indent.lineUp);
column++; // (
const Fodder &first_fodder = ast->args.size() == 0
? ast->fodderR
: open_fodder(ast->args[0].expr);
const Fodder &first_fodder = ast->args.size() == 0 ? ast->fodderR : argParamFirstFodder(ast->args[0]);
bool strong_indent = false;
// Need to use strong indent if there are not newlines before any of the sub-expressions
// Need to use strong indent if any of the
// arguments (except the first) are preceded by newlines.
bool first = true;
for (auto &arg : ast->args) {
if (first) {
// Skip first element.
first = false;
continue;
}
if (hasNewLines(arg.expr)) strong_indent = true;
if (hasNewLines(argParamFirstFodder(arg)))
strong_indent = true;
}

Indent arg_indent = strong_indent
Expand Down Expand Up @@ -1352,7 +1350,8 @@ class FixIndentation {
first = false;
continue;
}
if (hasNewLines(el.expr)) strong_indent = true;
if (hasNewLines(open_fodder(el.expr)))
strong_indent = true;
}

Indent new_indent = strong_indent
Expand Down
18 changes: 18 additions & 0 deletions test_suite/formatter.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,22 @@ limitations under the License.
field: 1,

dollarUnary: - $.field,

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

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

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

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

17 changes: 17 additions & 0 deletions test_suite/formatter.jsonnet.fmt.golden
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,21 @@ limitations under the License.
field: 1,

dollarUnary: - $.field,

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

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

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

user4: std.toString(
a='value1',
),
}
3 changes: 2 additions & 1 deletion test_suite/formatter.jsonnet.golden
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
"test_field9d": null,
"test_field9e": null,
"test_field9g": null,
"test_field9h": null
"test_field9h": null,
"user4": "value1"
}