-
Notifications
You must be signed in to change notification settings - Fork 453
Add simple newline insertion to formatter #334
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -963,6 +963,280 @@ class PrettyFieldNames : public FmtPass { | |
} | ||
}; | ||
|
||
/// Add newlines inside complex structures (arrays, objects etc.). | ||
/// | ||
/// The main principle is that a structure can either be: | ||
/// * expanded and contain newlines in all the designated places | ||
/// * unexpanded and not contain any newlines in any of designated places. | ||
/// | ||
/// It only looks shallowly at the AST nodes, so there may be some newlines deeper that | ||
/// don't affect expanding. For example: | ||
/// [{ | ||
/// 'a': 'b', | ||
/// 'c': 'd', | ||
/// }] | ||
/// The outer array can be unexpanded, because there are no newlines between | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incomplete sentence |
||
class FixNewlines: public FmtPass { | ||
using FmtPass::visit; | ||
|
||
bool shouldExpand(const Array *array) | ||
{ | ||
for (const auto &elem: array->elements) { | ||
if (countNewlines(open_fodder(elem.expr)) > 0) { | ||
return true; | ||
} | ||
} | ||
if (countNewlines(array->closeFodder) > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void expand(Array *array) | ||
{ | ||
for (auto &elem: array->elements) { | ||
ensureCleanNewline(open_fodder(elem.expr)); | ||
} | ||
ensureCleanNewline(array->closeFodder); | ||
} | ||
|
||
Fodder &objectFieldOpenFodder(ObjectField &field) | ||
{ | ||
if (field.kind == ObjectField::Kind::FIELD_STR) { | ||
return field.expr1->openFodder; | ||
} | ||
return field.fodder1; | ||
} | ||
|
||
bool shouldExpand(Object *object) | ||
{ | ||
for (auto &field: object->fields) { | ||
if (countNewlines(objectFieldOpenFodder(field)) > 0) { | ||
return true; | ||
} | ||
} | ||
if (countNewlines(object->closeFodder) > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void expand(Object *object) | ||
{ | ||
for (auto &field: object->fields) { | ||
ensureCleanNewline(objectFieldOpenFodder(field)); | ||
} | ||
ensureCleanNewline(object->closeFodder); | ||
} | ||
|
||
bool shouldExpand(Local *local) | ||
{ | ||
for (auto &bind: local->binds) { | ||
if (countNewlines(bind.varFodder) > 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void expand(Local *local) | ||
{ | ||
bool first = true; | ||
for (auto &bind: local->binds) { | ||
if (!first) { | ||
ensureCleanNewline(bind.varFodder); | ||
} | ||
first = false; | ||
} | ||
} | ||
|
||
bool shouldExpand(ArrayComprehension *comp) | ||
{ | ||
if (countNewlines(open_fodder(comp->body)) > 0) { | ||
return true; | ||
} | ||
for (auto &spec: comp->specs) { | ||
if (countNewlines(spec.openFodder) > 0) { | ||
return true; | ||
} | ||
} | ||
if (countNewlines(comp->closeFodder) > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void expand(ArrayComprehension *comp) | ||
{ | ||
ensureCleanNewline(open_fodder(comp->body)); | ||
for (auto &spec: comp->specs) { | ||
ensureCleanNewline(spec.openFodder); | ||
} | ||
ensureCleanNewline(comp->closeFodder); | ||
} | ||
|
||
bool shouldExpand(ObjectComprehension *comp) | ||
{ | ||
for (auto &field: comp->fields) { | ||
if (countNewlines(objectFieldOpenFodder(field)) > 0) { | ||
return true; | ||
} | ||
} | ||
for (auto &spec: comp->specs) { | ||
if (countNewlines(spec.openFodder) > 0) { | ||
return true; | ||
} | ||
} | ||
if (countNewlines(comp->closeFodder) > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void expand(ObjectComprehension *comp) | ||
{ | ||
for (auto &field: comp->fields) { | ||
ensureCleanNewline(objectFieldOpenFodder(field)); | ||
} | ||
for (auto &spec: comp->specs) { | ||
ensureCleanNewline(spec.openFodder); | ||
} | ||
ensureCleanNewline(comp->closeFodder); | ||
} | ||
|
||
bool shouldExpand(Parens *parens) | ||
{ | ||
return countNewlines(open_fodder(parens->expr)) > 0 | ||
|| countNewlines(parens->closeFodder) > 0; | ||
} | ||
|
||
void expand(Parens *parens) | ||
{ | ||
ensureCleanNewline(open_fodder(parens->expr)); | ||
ensureCleanNewline(parens->closeFodder); | ||
} | ||
|
||
Fodder &argParamOpenFodder(ArgParam ¶m) | ||
{ | ||
if (param.id != nullptr) { | ||
return param.idFodder; | ||
} else if (param.expr != nullptr) { | ||
return open_fodder(param.expr); | ||
} else { | ||
std::cerr << "Invalid ArgParam" << std::endl; | ||
abort(); | ||
} | ||
} | ||
|
||
// Example: | ||
// f(1, 2, | ||
// 3) | ||
// Should be expanded to: | ||
// f(1, | ||
// 2, | ||
// 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the closing paren live on a new line to match the below style? Or is that handled separately? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of this comment was to explain what this particular function does, expandBetween as opposed to expandNearParens (both operate on ArgParams). But still no, it wouldn't be touched by the other one (it would if there was a newline before 1 or after 3). |
||
bool shouldExpandBetween(ArgParams ¶ms) | ||
{ | ||
bool first = true; | ||
for (auto ¶m: params) { | ||
if (!first && countNewlines(argParamOpenFodder(param)) > 0) { | ||
return true; | ||
} | ||
first = false; | ||
} | ||
return false; | ||
} | ||
|
||
void expandBetween(ArgParams ¶ms) | ||
{ | ||
bool first = true; | ||
for (auto ¶m: params) { | ||
if (!first) { | ||
ensureCleanNewline(argParamOpenFodder(param)); | ||
} | ||
first = false; | ||
} | ||
} | ||
|
||
// Example: | ||
// very_long_function_on_a_long_line_so_arguments_dont_fit_on_the_same_line( | ||
// 1, 2, 3) | ||
// Should be expanded to: | ||
// very_long_function_on_a_long_line_so_arguments_dont_fit_on_the_same_line( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with the other formatting passes, but are any considerations taken for line length? Reading the code here it seems the expansion happens when args are already on another line, not if function is too long. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it doesn't take the length into account. The name was only an example situation when someone would format it this way (as opposed to keeping arguments on the same line). Hmmm... but I can see how it can be confusing. Maybe it's better to change it to something generic. |
||
// 1, 2, 3 | ||
// ) | ||
bool shouldExpandNearParens(ArgParams ¶ms, Fodder &fodder_r) | ||
{ | ||
if (params.empty()) { | ||
return false; | ||
} | ||
auto &argFodder = argParamOpenFodder(params.front()); | ||
return countNewlines(fodder_r) > 0 || countNewlines(argFodder) > 0; | ||
} | ||
|
||
void expandNearParens(ArgParams ¶ms, Fodder &fodder_r) | ||
{ | ||
if (!params.empty()) { | ||
ensureCleanNewline(argParamOpenFodder(params.front())); | ||
} | ||
ensureCleanNewline(fodder_r); | ||
} | ||
|
||
public: | ||
FixNewlines(Allocator &alloc, const FmtOpts &opts) : FmtPass(alloc, opts) { } | ||
|
||
template<class T> | ||
void simpleExpandingVisit(T *expr) { | ||
if (shouldExpand(expr)) { | ||
expand(expr); | ||
} | ||
FmtPass::visit(expr); | ||
} | ||
|
||
void visit(Array *array) | ||
{ | ||
simpleExpandingVisit(array); | ||
} | ||
|
||
void visit(Object *object) | ||
{ | ||
simpleExpandingVisit(object); | ||
} | ||
|
||
void visit(Local *local) | ||
{ | ||
simpleExpandingVisit(local); | ||
} | ||
|
||
void visit(ArrayComprehension *comp) | ||
{ | ||
simpleExpandingVisit(comp); | ||
} | ||
|
||
void visit(ObjectComprehension *comp) | ||
{ | ||
simpleExpandingVisit(comp); | ||
} | ||
|
||
void visit(Parens *parens) | ||
{ | ||
simpleExpandingVisit(parens); | ||
} | ||
|
||
void params(Fodder &fodder_l, ArgParams ¶ms, Fodder &fodder_r) | ||
{ | ||
if (shouldExpandBetween(params)) { | ||
expandBetween(params); | ||
} | ||
|
||
if (shouldExpandNearParens(params, fodder_r)) { | ||
expandNearParens(params, fodder_r); | ||
} | ||
|
||
FmtPass::params(fodder_l, params, fodder_r); | ||
} | ||
}; | ||
|
||
class FixIndentation { | ||
|
||
FmtOpts opts; | ||
|
@@ -977,7 +1251,7 @@ class FixIndentation { | |
* \param separate_token If the last fodder was an interstitial, whether a space should follow | ||
* it. | ||
* \param all_but_last_indent New indentation value for all but final fodder element. | ||
* \param last_indent New indentation value for but final fodder element. | ||
* \param last_indent New indentation value for the final fodder element. | ||
*/ | ||
void fill(Fodder &fodder, bool space_before, bool separate_token, | ||
unsigned all_but_last_indent, unsigned last_indent) | ||
|
@@ -1889,13 +2163,6 @@ class SortImports { | |
return false; | ||
} | ||
|
||
void ensureCleanNewline(Fodder &fodder) | ||
{ | ||
if (!fodder_has_clean_endline(fodder)) { | ||
fodder_push_back(fodder, FodderElement(FodderElement::Kind::LINE_END, 0, 0, {})); | ||
} | ||
} | ||
|
||
AST *toplevelImport(Local *local, ImportElems &imports, const Fodder &groupOpenFodder) | ||
{ | ||
assert(isGoodLocal(local)); | ||
|
@@ -1958,6 +2225,7 @@ std::string jsonnet_fmt(AST *ast, Fodder &final_fodder, const FmtOpts &opts) | |
remove_initial_newlines(ast); | ||
if (opts.maxBlankLines > 0) | ||
EnforceMaximumBlankLines(alloc, opts).file(ast, final_fodder); | ||
FixNewlines(alloc, opts).file(ast, final_fodder); | ||
FixTrailingCommas(alloc, opts).file(ast, final_fodder); | ||
FixParens(alloc, opts).file(ast, final_fodder); | ||
FixPlusObject(alloc, opts).file(ast, final_fodder); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly more concise description:
/// * unexpanded and contain newlines in none of the designated places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done