Skip to content

Commit 90bb91d

Browse files
committed
fix: use temporary buffer for conditional newline
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
1 parent 3eb0928 commit 90bb91d

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

src/dfmt/ast.d

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import dfmt.config;
1616
import dfmt.editorconfig;
1717
import std.range;
1818
import std.format : format;
19-
import std.stdio : writeln, File;
19+
import std.stdio : File;
2020

2121
extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
2222
{
2323
File.LockingTextWriter buf;
2424
const Config* config;
2525
string eol;
26+
string tempBuf; // a string buffer to temporarily store data for line splitting
27+
bool useTempBuf; // toggles the buffer being written to
2628
uint depth; // the current indentation level
2729
uint length; // the length of the current line of code
2830
bool declString; // set while declaring alias for string,wstring or dstring
@@ -81,14 +83,24 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
8183
isNewline = true;
8284
}
8385

84-
void conditionalNewline(T)(T data)
86+
// Writes a newline only if the data will exceed the maximum allowed line length
87+
bool conditionalNewline()
8588
{
86-
// If the current length is crosses the soft limit OR
89+
// If the current length crosses the soft limit OR
8790
// if the current length + data length crosses the hard limit,
8891
// insert a newline.
8992
if (length > config.dfmt_soft_max_line_length
90-
|| (length + data.length) > config.max_line_length)
93+
|| (length + tempBuf.length) > config.max_line_length) {
9194
newline();
95+
return true;
96+
}
97+
return false;
98+
}
99+
100+
void writeTempBuf() {
101+
useTempBuf = false;
102+
write(tempBuf);
103+
tempBuf = "";
92104
}
93105

94106
void write(T)(T data) if (is(T : char) || is(T : dchar))
@@ -98,7 +110,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
98110
indent();
99111
isNewline = false;
100112
}
101-
buf.put(data);
113+
if (useTempBuf)
114+
tempBuf ~= data;
115+
else
116+
buf.put(data);
102117
length += 1;
103118
}
104119

@@ -109,7 +124,10 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
109124
indent();
110125
isNewline = false;
111126
}
112-
buf.put(data);
127+
if (useTempBuf)
128+
tempBuf ~= data;
129+
else
130+
buf.put(data);
113131
length += data.length;
114132
}
115133

@@ -232,8 +250,6 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
232250

233251
void visitInteger(ASTCodegen.IntegerExp e)
234252
{
235-
import core.stdc.stdio : sprintf;
236-
237253
auto v = e.toInteger();
238254
if (e.type)
239255
{
@@ -3241,6 +3257,7 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
32413257

32423258
void visitTemplateConstraint(ASTCodegen.Expression constraint)
32433259
{
3260+
32443261
if (!constraint)
32453262
return;
32463263

@@ -3249,32 +3266,49 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
32493266
case TemplateConstraintStyle._unspecified:
32503267
// Fallthrough to the default case
32513268
case TemplateConstraintStyle.conditional_newline_indent:
3252-
conditionalNewline();
3269+
useTempBuf = true;
32533270
depth++;
32543271
break;
32553272
case TemplateConstraintStyle.always_newline_indent:
32563273
newline();
32573274
depth++;
32583275
break;
32593276
case TemplateConstraintStyle.conditional_newline:
3260-
conditionalNewline();
3277+
useTempBuf = true;
32613278
break;
32623279
case TemplateConstraintStyle.always_newline:
32633280
newline();
32643281
break;
32653282
}
32663283

3267-
write(" if");
3284+
write("if");
32683285
if (config.dfmt_space_after_keywords)
32693286
write(' ');
32703287
write('(');
32713288
writeExpr(constraint);
32723289
write(')');
32733290

3274-
if (config.dfmt_template_constraint_style == TemplateConstraintStyle.always_newline_indent
3275-
|| config.dfmt_template_constraint_style
3276-
== TemplateConstraintStyle.conditional_newline_indent)
3291+
final switch (config.dfmt_template_constraint_style)
3292+
{
3293+
case TemplateConstraintStyle._unspecified:
3294+
// Fallthrough to the default case
3295+
case TemplateConstraintStyle.conditional_newline_indent:
3296+
if (!conditionalNewline())
3297+
buf.put(' ');
3298+
writeTempBuf();
3299+
depth--;
3300+
break;
3301+
case TemplateConstraintStyle.always_newline_indent:
32773302
depth--;
3303+
break;
3304+
case TemplateConstraintStyle.conditional_newline:
3305+
if (!conditionalNewline())
3306+
buf.put(' ');
3307+
writeTempBuf();
3308+
break;
3309+
case TemplateConstraintStyle.always_newline:
3310+
break;
3311+
}
32783312
}
32793313

32803314
override void visitBaseClasses(ASTCodegen.ClassDeclaration d)

0 commit comments

Comments
 (0)