feat: add `dfmt_space_after_keywords`

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
Prajwal S N 2023-11-23 00:11:00 +05:30
parent 90c9040898
commit 62e47bb5d4
No known key found for this signature in database
GPG Key ID: 60701A603988FAC2
2 changed files with 72 additions and 23 deletions

View File

@ -1297,7 +1297,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitWhile(ASTCodegen.WhileStatement s) void visitWhile(ASTCodegen.WhileStatement s)
{ {
write("while ("); write("while");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (auto p = s.param) if (auto p = s.param)
{ {
// Print condition assignment // Print condition assignment
@ -1324,7 +1327,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
newline(); newline();
if (s._body) if (s._body)
writeStatement(s._body); writeStatement(s._body);
write("while ("); write("while");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
writeExpr(s.condition); writeExpr(s.condition);
write(");"); write(");");
newline(); newline();
@ -1332,7 +1338,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitFor(ASTCodegen.ForStatement s) void visitFor(ASTCodegen.ForStatement s)
{ {
write("for ("); write("for");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (s._init) if (s._init)
{ {
writeStatement(s._init); writeStatement(s._init);
@ -1365,7 +1374,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitForeachWithoutBody(ASTCodegen.ForeachStatement s) void visitForeachWithoutBody(ASTCodegen.ForeachStatement s)
{ {
write(Token.toString(s.op)); write(Token.toString(s.op));
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
foreach (i, p; *s.parameters) foreach (i, p; *s.parameters)
{ {
if (i) if (i)
@ -1398,7 +1409,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitForeachRangeWithoutBody(ASTCodegen.ForeachRangeStatement s) void visitForeachRangeWithoutBody(ASTCodegen.ForeachRangeStatement s)
{ {
write(Token.toString(s.op)); write(Token.toString(s.op));
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (s.prm.type) if (s.prm.type)
writeTypeWithIdent(s.prm.type, s.prm.ident); writeTypeWithIdent(s.prm.type, s.prm.ident);
else else
@ -1446,7 +1459,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitIf(ASTCodegen.IfStatement s) void visitIf(ASTCodegen.IfStatement s)
{ {
write("if ("); write("if");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (Parameter p = s.prm) if (Parameter p = s.prm)
{ {
StorageClass stc = p.storageClass; StorageClass stc = p.storageClass;
@ -1524,7 +1540,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitPragma(ASTCodegen.PragmaStatement s) void visitPragma(ASTCodegen.PragmaStatement s)
{ {
write("pragma ("); write("pragma");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
write(s.ident.toString()); write(s.ident.toString());
if (s.args && s.args.length) if (s.args && s.args.length)
{ {
@ -1557,7 +1576,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitSwitch(ASTCodegen.SwitchStatement s) void visitSwitch(ASTCodegen.SwitchStatement s)
{ {
write(s.isFinal ? "final switch (" : "switch ("); write(s.isFinal ? "final switch" : "switch");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (auto p = s.param) if (auto p = s.param)
{ {
// Print condition assignment // Print condition assignment
@ -1699,7 +1721,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitWith(ASTCodegen.WithStatement s) void visitWith(ASTCodegen.WithStatement s)
{ {
write("with ("); write("with");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
writeExpr(s.exp); writeExpr(s.exp);
write(")"); write(")");
newline(); newline();
@ -1896,7 +1921,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (auto es = frequire.isExpStatement()) if (auto es = frequire.isExpStatement())
{ {
assert(es.exp && es.exp.op == EXP.assert_); assert(es.exp && es.exp.op == EXP.assert_);
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
writeExpr((cast(ASTCodegen.AssertExp) es.exp).e1); writeExpr((cast(ASTCodegen.AssertExp) es.exp).e1);
write(')'); write(')');
newline(); newline();
@ -1919,7 +1946,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (auto es = fensure.ensure.isExpStatement()) if (auto es = fensure.ensure.isExpStatement())
{ {
assert(es.exp && es.exp.op == EXP.assert_); assert(es.exp && es.exp.op == EXP.assert_);
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (fensure.id) if (fensure.id)
{ {
write(fensure.id.toString()); write(fensure.id.toString());
@ -2545,7 +2574,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
const s = linkageToString(linkage); const s = linkageToString(linkage);
if (s.length) if (s.length)
{ {
write("extern ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
write(s); write(s);
write(')'); write(')');
} }
@ -2881,7 +2912,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitLinkDeclaration(ASTCodegen.LinkDeclaration d) void visitLinkDeclaration(ASTCodegen.LinkDeclaration d)
{ {
write("extern ("); write("extern");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
write(linkageToString(d.linkage)); write(linkageToString(d.linkage));
write(") "); write(") ");
visitAttribDeclaration(d); visitAttribDeclaration(d);
@ -2961,7 +2995,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitPragmaDeclaration(ASTCodegen.PragmaDeclaration d) void visitPragmaDeclaration(ASTCodegen.PragmaDeclaration d)
{ {
write("pragma ("); write("pragma");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
write(d.ident.toString()); write(d.ident.toString());
if (d.args && d.args.length) if (d.args && d.args.length)
{ {
@ -3013,7 +3050,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void foreachWithoutBody(ASTCodegen.ForeachStatement s) void foreachWithoutBody(ASTCodegen.ForeachStatement s)
{ {
write(Token.toString(s.op)); write(Token.toString(s.op));
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
foreach (i, p; *s.parameters) foreach (i, p; *s.parameters)
{ {
if (i) if (i)
@ -3035,7 +3074,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
/* s.op ( prm ; lwr .. upr ) /* s.op ( prm ; lwr .. upr )
*/ */
write(Token.toString(s.op)); write(Token.toString(s.op));
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
if (s.prm.type) if (s.prm.type)
writeTypeWithIdent(s.prm.type, s.prm.ident); writeTypeWithIdent(s.prm.type, s.prm.ident);
else else
@ -3088,7 +3129,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{ {
if (!constraint) if (!constraint)
return; return;
write(" if ("); write(" if");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
writeExpr(constraint); writeExpr(constraint);
write(')'); write(')');
} }
@ -3467,7 +3511,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (auto es = d.fbody.isExpStatement()) if (auto es = d.fbody.isExpStatement())
{ {
assert(es.exp && es.exp.op == EXP.assert_); assert(es.exp && es.exp.op == EXP.assert_);
write(" ("); if (config.dfmt_space_after_keywords)
write(' ');
write('(');
writeExpr((cast(ASTCodegen.AssertExp) es.exp).e1); writeExpr((cast(ASTCodegen.AssertExp) es.exp).e1);
write(");"); write(");");
newline(); newline();
@ -3550,9 +3596,11 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitVersionCondition(ASTCodegen.VersionCondition c) void visitVersionCondition(ASTCodegen.VersionCondition c)
{ {
write("version ("); write("version");
if (c.ident) if (c.ident)
{ {
if (config.dfmt_space_after_keywords)
write(' ');
write('('); write('(');
write(c.ident.toString()); write(c.ident.toString());
write(')'); write(')');
@ -3561,7 +3609,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitStaticIfCondition(ASTCodegen.StaticIfCondition c) void visitStaticIfCondition(ASTCodegen.StaticIfCondition c)
{ {
write("static if ("); write("static if");
if (config.dfmt_space_after_keywords)
write(' ');
write('(');
writeExpr(c.exp); writeExpr(c.exp);
write(')'); write(')');
} }

View File

@ -66,8 +66,6 @@ struct Config
/// ///
OptionalBoolean dfmt_reflow_property_chains; OptionalBoolean dfmt_reflow_property_chains;
/// ///
OptionalBoolean dfmt_space_after_statement_keyword;
///
OptionalBoolean dfmt_space_before_named_arg_colon; OptionalBoolean dfmt_space_before_named_arg_colon;
mixin StandardEditorConfigFields; mixin StandardEditorConfigFields;
@ -114,7 +112,7 @@ struct Config
if (dfmt_soft_max_line_length > max_line_length) if (dfmt_soft_max_line_length > max_line_length)
{ {
stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)", stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)",
max_line_length, dfmt_soft_max_line_length); max_line_length, dfmt_soft_max_line_length);
return false; return false;
} }
return true; return true;