feat: add brace styles

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
Prajwal S N 2023-12-01 17:45:33 +05:30
parent b4e36d998b
commit 07bfe5c208
No known key found for this signature in database
GPG Key ID: 60701A603988FAC2
1 changed files with 139 additions and 34 deletions

View File

@ -26,7 +26,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
uint depth; uint depth;
bool declString; // set while declaring alias for string,wstring or dstring bool declString; // set while declaring alias for string,wstring or dstring
bool isNewline; // used to indent before writing the line bool isNewline; // used to indent before writing the line
bool insideCase; // true if the node a child of a CaseStatement bool insideCase; // true if the node is a child of a CaseStatement
bool insideIfOrDo; // true if the node is a child of an IfStatement or DoStatement
this(File.LockingTextWriter buf, Config* config) this(File.LockingTextWriter buf, Config* config)
{ {
@ -385,7 +386,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (i) if (i)
write(", "); write(", ");
writeExprWithPrecedence(key, PREC.assign); writeExprWithPrecedence(key, PREC.assign);
if (config.dfmt_space_before_aa_colon == OptionalBoolean.t) if (config.dfmt_space_before_aa_colon)
write(' '); write(' ');
write(": "); write(": ");
auto value = (*e.values)[i]; auto value = (*e.values)[i];
@ -1281,7 +1282,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{ {
if (!insideCase) if (!insideCase)
{ {
write('{'); if (config.dfmt_brace_style != BraceStyle.knr || s.statement.isCompoundStatement.statements.length != 1)
write('{');
newline(); newline();
} }
depth++; depth++;
@ -1290,8 +1292,12 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
depth--; depth--;
if (!insideCase) if (!insideCase)
{ {
write('}'); if (config.dfmt_brace_style != BraceStyle.knr || s.statement.isCompoundStatement.statements.length != 1)
newline(); {
write('}');
if (!insideIfOrDo)
newline();
}
} }
} }
@ -1324,9 +1330,27 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitDo(ASTCodegen.DoStatement s) void visitDo(ASTCodegen.DoStatement s)
{ {
write("do"); write("do");
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
if (s._body) newline();
else
write(' ');
if (s._body.isScopeStatement)
{
insideIfOrDo = true;
writeStatement(s._body); writeStatement(s._body);
insideIfOrDo = false;
}
else
{
depth++;
writeStatement(s._body);
depth--;
}
if (config.dfmt_brace_style == BraceStyle.otbs)
write(' ');
else if (config.dfmt_brace_style != BraceStyle.knr || s._body
.isScopeStatement.statement.isCompoundStatement.statements.length > 1)
newline();
write("while"); write("while");
if (config.dfmt_space_after_keywords) if (config.dfmt_space_after_keywords)
write(' '); write(' ');
@ -1360,7 +1384,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
writeExpr(s.increment); writeExpr(s.increment);
} }
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -1390,7 +1417,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write("; "); write("; ");
writeExpr(s.aggr); writeExpr(s.aggr);
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
} }
void visitForeach(ASTCodegen.ForeachStatement s) void visitForeach(ASTCodegen.ForeachStatement s)
@ -1421,7 +1451,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write(" .. "); write(" .. ");
writeExpr(s.upr); writeExpr(s.upr);
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
} }
void visitForeachRange(ASTCodegen.ForeachRangeStatement s) void visitForeachRange(ASTCodegen.ForeachRangeStatement s)
@ -1477,10 +1510,15 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
} }
writeExpr(s.condition); writeExpr(s.condition);
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
if (s.ifbody.isScopeStatement()) if (s.ifbody.isScopeStatement())
{ {
insideIfOrDo = true;
writeStatement(s.ifbody); writeStatement(s.ifbody);
insideIfOrDo = false;
} }
else else
{ {
@ -1490,8 +1528,13 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
} }
if (s.elsebody) if (s.elsebody)
{ {
if (config.dfmt_brace_style == BraceStyle.otbs)
write(' ');
else if (config.dfmt_brace_style != BraceStyle.knr ||
s.ifbody.isScopeStatement.statement.isCompoundStatement.statements.length > 1)
newline();
write("else"); write("else");
if (!s.elsebody.isIfStatement()) if (!s.elsebody.isIfStatement() && config.dfmt_brace_style == BraceStyle.allman)
{ {
newline(); newline();
} }
@ -1510,12 +1553,19 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
depth--; depth--;
} }
} }
else
{
newline();
}
} }
void visitConditional(ASTCodegen.ConditionalStatement s) void visitConditional(ASTCodegen.ConditionalStatement s)
{ {
s.condition.accept(this); s.condition.accept(this);
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -1527,7 +1577,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (s.elsebody) if (s.elsebody)
{ {
write("else"); write("else");
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
depth++; depth++;
newline(); newline();
@ -1553,7 +1606,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write(')'); write(')');
if (s._body) if (s._body)
{ {
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -1595,7 +1651,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
} }
writeExpr(s.condition); writeExpr(s.condition);
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
if (s._body) if (s._body)
{ {
if (!s._body.isScopeStatement()) if (!s._body.isScopeStatement())
@ -1758,7 +1817,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
writeTypeWithIdent(c.type, c.ident); writeTypeWithIdent(c.type, c.ident);
write(')'); write(')');
} }
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -1773,7 +1835,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitTryFinally(ASTCodegen.TryFinallyStatement s) void visitTryFinally(ASTCodegen.TryFinallyStatement s)
{ {
write("try"); write("try");
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -1888,7 +1953,11 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void writeFuncBody(ASTCodegen.FuncDeclaration f) void writeFuncBody(ASTCodegen.FuncDeclaration f)
{ {
newline(); if (config.dfmt_brace_style == BraceStyle.allman || config.dfmt_brace_style == BraceStyle
.knr)
newline();
else
write(' ');
writeContracts(f); writeContracts(f);
write('{'); write('{');
newline(); newline();
@ -1977,7 +2046,11 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (requireDo) if (requireDo)
{ {
write("do"); write("do");
newline(); if (config.dfmt_brace_style == BraceStyle.allman || config.dfmt_brace_style == BraceStyle
.knr)
newline();
else
write(' ');
} }
} }
@ -2021,7 +2094,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
if (ex) if (ex)
{ {
writeExpr(ex); writeExpr(ex);
if (config.dfmt_space_before_aa_colon == OptionalBoolean.t) if (config.dfmt_space_before_aa_colon)
write(' '); write(' ');
write(": "); write(": ");
} }
@ -2887,7 +2960,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
} }
else else
{ {
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -2981,8 +3057,11 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitAnonDeclaration(ASTCodegen.AnonDeclaration d) void visitAnonDeclaration(ASTCodegen.AnonDeclaration d)
{ {
write(d.isunion ? "union" : "struct"); write(d.isunion ? "union" : "struct");
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
write("{"); newline();
else
write(' ');
write('{');
newline(); newline();
depth++; depth++;
if (d.decl) if (d.decl)
@ -2991,7 +3070,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
de.accept(this); de.accept(this);
} }
depth--; depth--;
write("}"); write('}');
newline(); newline();
} }
@ -3017,7 +3096,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
d.condition.accept(this); d.condition.accept(this);
if (d.decl || d.elsedecl) if (d.decl || d.elsedecl)
{ {
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3032,7 +3114,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{ {
newline(); newline();
write("else"); write("else");
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3088,7 +3173,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write(" .. "); write(" .. ");
writeExpr(s.upr); writeExpr(s.upr);
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
} }
write("static "); write("static ");
@ -3181,7 +3269,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
visitBaseClasses(ad.isClassDeclaration()); visitBaseClasses(ad.isClassDeclaration());
if (ad.members) if (ad.members)
{ {
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3272,7 +3363,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
newline(); newline();
return; return;
} }
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3294,7 +3388,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
write("extern (C++, "); write("extern (C++, ");
write(d.ident.toString()); write(d.ident.toString());
write(')'); write(')');
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3317,7 +3414,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
newline(); newline();
return; return;
} }
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3339,7 +3439,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
visitBaseClasses(d); visitBaseClasses(d);
if (d.members) if (d.members)
{ {
newline(); if (config.dfmt_brace_style == BraceStyle.allman)
newline();
else
write(' ');
write('{'); write('{');
newline(); newline();
depth++; depth++;
@ -3587,9 +3690,11 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitDebugCondition(ASTCodegen.DebugCondition c) void visitDebugCondition(ASTCodegen.DebugCondition c)
{ {
write("debug "); write("debug");
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(')');