fix: iron out some bugs

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
Prajwal S N 2023-11-09 23:23:51 +05:30
parent decdc79445
commit ded6e8d37b
No known key found for this signature in database
GPG Key ID: 60701A603988FAC2
2 changed files with 66 additions and 55 deletions

View File

@ -24,8 +24,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
const Config* config; const Config* config;
string eol; string eol;
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 doindent; // insert indentation before writing the string bool isNewline; // used to indent before writing the line
bool insideCase; // true if the node a child of a CaseStatement
this(File.LockingTextWriter buf, Config* config) this(File.LockingTextWriter buf, Config* config)
{ {
@ -74,15 +75,15 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{ {
buf.put(eol); buf.put(eol);
// Indicate that the next write should be indented // Indicate that the next write should be indented
doindent = true; isNewline = true;
} }
extern (D) void write(T)(T data) extern (D) void write(T)(T data)
{ {
if (doindent) if (isNewline)
{ {
indent(); indent();
doindent = false; isNewline = false;
} }
buf.put(data); buf.put(data);
} }
@ -1079,8 +1080,6 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
} }
else else
{ {
// Sparse style formatting, for debug use only
// [0..length: basis, 1: e1, 5: e5]
if (basis) if (basis)
{ {
write("0.."); write("0..");
@ -1274,14 +1273,20 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitScope(ASTCodegen.ScopeStatement s) void visitScope(ASTCodegen.ScopeStatement s)
{ {
write('{'); if (!insideCase)
newline(); {
write('{');
newline();
}
depth++; depth++;
if (s.statement) if (s.statement)
writeStatement(s.statement); writeStatement(s.statement);
depth--; depth--;
write('}'); if (!insideCase)
newline(); {
write('}');
newline();
}
} }
void visitWhile(ASTCodegen.WhileStatement s) void visitWhile(ASTCodegen.WhileStatement s)
@ -1507,8 +1512,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
writeStatement(s.elsebody); writeStatement(s.elsebody);
depth--; depth--;
write('}'); write('}');
newline();
} }
newline();
} }
void visitPragma(ASTCodegen.PragmaStatement s) void visitPragma(ASTCodegen.PragmaStatement s)
@ -1588,7 +1593,9 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
writeExpr(s.exp); writeExpr(s.exp);
write(':'); write(':');
newline(); newline();
insideCase = true;
writeStatement(s.statement); writeStatement(s.statement);
insideCase = false;
} }
void visitCaseRange(ASTCodegen.CaseRangeStatement s) void visitCaseRange(ASTCodegen.CaseRangeStatement s)
@ -1842,37 +1849,28 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void writeFuncBody(ASTCodegen.FuncDeclaration f) void writeFuncBody(ASTCodegen.FuncDeclaration f)
{ {
if (!f.fbody)
{
if (f.fensures || f.frequires)
{
newline();
writeContracts(f);
}
write(';');
newline();
return;
}
newline(); newline();
bool requireDo = writeContracts(f); writeContracts(f);
if (requireDo)
{
write("do");
newline();
}
write('{'); write('{');
newline(); newline();
depth++; depth++;
writeStatement(f.fbody); if (f.fbody)
{
writeStatement(f.fbody);
}
else
{
write('{');
newline();
write('}');
newline();
}
depth--; depth--;
write('}'); write('}');
newline(); newline();
} }
// Returns: whether `do` is needed to write the function body void writeContracts(ASTCodegen.FuncDeclaration f)
bool writeContracts(ASTCodegen.FuncDeclaration f)
{ {
bool requireDo = false; bool requireDo = false;
// in{} // in{}
@ -1932,7 +1930,12 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
} }
} }
} }
return requireDo;
if (requireDo)
{
write("do");
newline();
}
} }
void writeInitializer(Initializer inx) void writeInitializer(Initializer inx)
@ -1949,7 +1952,6 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitStruct(StructInitializer si) void visitStruct(StructInitializer si)
{ {
//printf("StructInitializer::toCBuffer()\n");
write('{'); write('{');
foreach (i, const id; si.field) foreach (i, const id; si.field)
{ {
@ -2286,7 +2288,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitDArray(TypeDArray t) void visitDArray(TypeDArray t)
{ {
Type ut = t.castMod(0); Type ut = t.castMod(0);
if (declstring) if (declString)
goto L1; goto L1;
if (ut.equals(Type.tstring)) if (ut.equals(Type.tstring))
write("string"); write("string");
@ -2732,10 +2734,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitDebugSymbol(ASTCodegen.DebugSymbol s) void visitDebugSymbol(ASTCodegen.DebugSymbol s)
{ {
write("debug = "); write("debug = ");
if (s.ident) write(s.ident.toString());
write(s.ident.toString());
else
write(format("%d", s.level));
write(';'); write(';');
newline(); newline();
} }
@ -2743,10 +2742,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitVersionSymbol(ASTCodegen.VersionSymbol s) void visitVersionSymbol(ASTCodegen.VersionSymbol s)
{ {
write("version = "); write("version = ");
if (s.ident) write(s.ident.toString());
write(s.ident.toString());
else
write(format("%d", s.level));
write(';'); write(';');
newline(); newline();
} }
@ -2813,6 +2809,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
override void visitAttribDeclaration(ASTCodegen.AttribDeclaration d) override void visitAttribDeclaration(ASTCodegen.AttribDeclaration d)
{ {
if (isNewline)
{
newline();
}
if (auto stcd = d.isStorageClassDeclaration) if (auto stcd = d.isStorageClassDeclaration)
{ {
writeStc(stcd.stc); writeStc(stcd.stc);
@ -2824,6 +2824,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
newline(); newline();
return; return;
} }
if (d.decl.length == 0) if (d.decl.length == 0)
{ {
write("{}"); write("{}");
@ -2890,6 +2891,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitVisibilityDeclaration(ASTCodegen.VisibilityDeclaration d) void visitVisibilityDeclaration(ASTCodegen.VisibilityDeclaration d)
{ {
if (isNewline)
{
newline();
}
writeVisibility(d.visibility); writeVisibility(d.visibility);
ASTCodegen.AttribDeclaration ad = cast(ASTCodegen.AttribDeclaration) d; ASTCodegen.AttribDeclaration ad = cast(ASTCodegen.AttribDeclaration) d;
if (ad.decl.length <= 1) if (ad.decl.length <= 1)
@ -3318,13 +3323,13 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
{ {
import dmd.id : Id; import dmd.id : Id;
declstring = (d.ident == Id.string || d.ident == Id.wstring || d.ident == Id declString = (d.ident == Id.string || d.ident == Id.wstring || d.ident == Id
.dstring); .dstring);
write(d.ident.toString()); write(d.ident.toString());
write(" = "); write(" = ");
writeStc(d.storage_class); writeStc(d.storage_class);
writeTypeWithIdent(d.type, null); writeTypeWithIdent(d.type, null);
declstring = false; declString = false;
} }
write(';'); write(';');
newline(); newline();
@ -3353,6 +3358,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
void visitFuncDeclaration(ASTCodegen.FuncDeclaration f) void visitFuncDeclaration(ASTCodegen.FuncDeclaration f)
{ {
if (isNewline)
{
newline();
}
writeStc(f.storage_class); writeStc(f.storage_class);
auto tf = cast(TypeFunction) f.type; auto tf = cast(TypeFunction) f.type;
writeTypeWithIdent(tf, f.ident); writeTypeWithIdent(tf, f.ident);
@ -3512,22 +3521,24 @@ 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)
{
write('(');
write(c.ident.toString()); write(c.ident.toString());
else write(')');
write(format("%d", c.level)); }
write(')');
} }
void visitVersionCondition(ASTCodegen.VersionCondition c) void visitVersionCondition(ASTCodegen.VersionCondition c)
{ {
write("version ("); write("version (");
if (c.ident) if (c.ident)
{
write('(');
write(c.ident.toString()); write(c.ident.toString());
else write(')');
write(format("%d", c.level)); }
write(')');
} }
void visitStaticIfCondition(ASTCodegen.StaticIfCondition c) void visitStaticIfCondition(ASTCodegen.StaticIfCondition c)

View File

@ -12,7 +12,7 @@ import std.path : filenameCharCmp, isDirSeparator;
// * changes meaning to match all characters except '/' // * changes meaning to match all characters except '/'
// ** added to take over the old meaning of * // ** added to take over the old meaning of *
bool globMatchEditorConfig(CaseSensitive cs = CaseSensitive.osDefault, C, Range)( bool globMatchEditorConfig(CaseSensitive cs = CaseSensitive.osDefault, C, Range)(
Range path, const(C)[] pattern) @safe pure nothrow Range path, const(C)[] pattern) @safe pure
if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range)
&& isSomeChar!C && is(Unqual!C == Unqual!(ElementEncodingType!Range))) && isSomeChar!C && is(Unqual!C == Unqual!(ElementEncodingType!Range)))
in in