Run dfmt on itself
This commit is contained in:
parent
b3faa3103c
commit
8035beec4f
|
@ -32,8 +32,8 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
|
||||||
astInformation.cleanup();
|
astInformation.cleanup();
|
||||||
auto tokens = byToken(buffer, config, &cache).array();
|
auto tokens = byToken(buffer, config, &cache).array();
|
||||||
auto depths = generateDepthInfo(tokens);
|
auto depths = generateDepthInfo(tokens);
|
||||||
auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths, output,
|
auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths,
|
||||||
&astInformation, formatterConfig);
|
output, &astInformation, formatterConfig);
|
||||||
tokenFormatter.format();
|
tokenFormatter.format();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ struct TokenFormatter(OutputRange)
|
||||||
* decisions.
|
* decisions.
|
||||||
*/
|
*/
|
||||||
this(const ubyte[] rawSource, const(Token)[] tokens, immutable short[] depths,
|
this(const ubyte[] rawSource, const(Token)[] tokens, immutable short[] depths,
|
||||||
OutputRange output, ASTInformation* astInformation, Config* config)
|
OutputRange output, ASTInformation* astInformation, Config* config)
|
||||||
{
|
{
|
||||||
this.rawSource = rawSource;
|
this.rawSource = rawSource;
|
||||||
this.tokens = tokens;
|
this.tokens = tokens;
|
||||||
|
@ -153,8 +153,8 @@ private:
|
||||||
{
|
{
|
||||||
formatComment();
|
formatComment();
|
||||||
}
|
}
|
||||||
else if (isStringLiteral(current.type) || isNumberLiteral(current.type)
|
else if (isStringLiteral(current.type)
|
||||||
|| currentIs(tok!"characterLiteral"))
|
|| isNumberLiteral(current.type) || currentIs(tok!"characterLiteral"))
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
if (index < tokens.length)
|
if (index < tokens.length)
|
||||||
|
@ -253,8 +253,9 @@ private:
|
||||||
writeToken();
|
writeToken();
|
||||||
if (index < tokens.length && (currentIs(tok!"identifier")
|
if (index < tokens.length && (currentIs(tok!"identifier")
|
||||||
|| isBasicType(current.type) || currentIs(tok!"@")
|
|| isBasicType(current.type) || currentIs(tok!"@")
|
||||||
|| currentIs(tok!"if") || isNumberLiteral(tokens[index].type)
|
|| currentIs(tok!"if")
|
||||||
|| (inAsm && peekBack2Is(tok!";") && currentIs(tok!"["))))
|
|| isNumberLiteral(tokens[index].type) || (inAsm
|
||||||
|
&& peekBack2Is(tok!";") && currentIs(tok!"["))))
|
||||||
{
|
{
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
}
|
||||||
|
@ -271,9 +272,10 @@ private:
|
||||||
string commentText(size_t i)
|
string commentText(size_t i)
|
||||||
{
|
{
|
||||||
import std.string : strip;
|
import std.string : strip;
|
||||||
|
|
||||||
assert(tokens[i].type == tok!"comment");
|
assert(tokens[i].type == tok!"comment");
|
||||||
string commentText = tokens[i].text;
|
string commentText = tokens[i].text;
|
||||||
if (commentText[0 ..2] == "//")
|
if (commentText[0 .. 2] == "//")
|
||||||
commentText = commentText[2 .. $];
|
commentText = commentText[2 .. $];
|
||||||
else
|
else
|
||||||
commentText = commentText[2 .. $ - 2];
|
commentText = commentText[2 .. $ - 2];
|
||||||
|
@ -284,7 +286,7 @@ private:
|
||||||
{
|
{
|
||||||
size_t dfmtOff = index;
|
size_t dfmtOff = index;
|
||||||
size_t dfmtOn = index;
|
size_t dfmtOn = index;
|
||||||
foreach (i; dfmtOff + 1.. tokens.length)
|
foreach (i; dfmtOff + 1 .. tokens.length)
|
||||||
{
|
{
|
||||||
dfmtOn = i;
|
dfmtOn = i;
|
||||||
if (tokens[i].type != tok!"comment")
|
if (tokens[i].type != tok!"comment")
|
||||||
|
@ -374,8 +376,9 @@ private:
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (t == tok!"import" && !currentIs(tok!"import") && !currentIs(tok!"}")
|
else if (t == tok!"import" && !currentIs(tok!"import")
|
||||||
&& !(currentIs(tok!"public") && peekIs(tok!"import")))
|
&& !currentIs(tok!"}") && !(currentIs(tok!"public")
|
||||||
|
&& peekIs(tok!"import")))
|
||||||
{
|
{
|
||||||
simpleNewline();
|
simpleNewline();
|
||||||
currentLineLength = 0;
|
currentLineLength = 0;
|
||||||
|
@ -430,7 +433,8 @@ private:
|
||||||
spaceAfterParens = true;
|
spaceAfterParens = true;
|
||||||
parenDepth++;
|
parenDepth++;
|
||||||
}
|
}
|
||||||
immutable bool arrayInitializerStart = p == tok!"[" && linebreakHints.length != 0
|
immutable bool arrayInitializerStart = p == tok!"["
|
||||||
|
&& linebreakHints.length != 0
|
||||||
&& astInformation.arrayStartLocations.canFindIndex(tokens[index - 1].index);
|
&& astInformation.arrayStartLocations.canFindIndex(tokens[index - 1].index);
|
||||||
if (arrayInitializerStart)
|
if (arrayInitializerStart)
|
||||||
{
|
{
|
||||||
|
@ -444,8 +448,8 @@ private:
|
||||||
depths[index .. j], config, currentLineLength, indentLevel);
|
depths[index .. j], config, currentLineLength, indentLevel);
|
||||||
}
|
}
|
||||||
else if (!currentIs(tok!")") && !currentIs(tok!"]")
|
else if (!currentIs(tok!")") && !currentIs(tok!"]")
|
||||||
&& (linebreakHints.canFindIndex(index - 1) || (linebreakHints.length == 0
|
&& (linebreakHints.canFindIndex(index - 1)
|
||||||
&& currentLineLength > config.max_line_length)))
|
|| (linebreakHints.length == 0 && currentLineLength > config.max_line_length)))
|
||||||
{
|
{
|
||||||
pushWrapIndent(p);
|
pushWrapIndent(p);
|
||||||
newline();
|
newline();
|
||||||
|
@ -490,7 +494,8 @@ private:
|
||||||
else
|
else
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
}
|
||||||
else if (index < tokens.length && (currentIs(tok!"@") || isBasicType(tokens[index].type)
|
else if (index < tokens.length && (currentIs(tok!"@")
|
||||||
|
|| isBasicType(tokens[index].type)
|
||||||
|| currentIs(tok!"identifier") || currentIs(tok!"if")))
|
|| currentIs(tok!"identifier") || currentIs(tok!"if")))
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
}
|
||||||
|
@ -500,15 +505,18 @@ private:
|
||||||
import dfmt.editorconfig : OptionalBoolean;
|
import dfmt.editorconfig : OptionalBoolean;
|
||||||
|
|
||||||
immutable bool isCase = astInformation.caseEndLocations.canFindIndex(current.index);
|
immutable bool isCase = astInformation.caseEndLocations.canFindIndex(current.index);
|
||||||
immutable bool isAttribute = astInformation.attributeDeclarationLines.canFindIndex(current.line);
|
immutable bool isAttribute = astInformation.attributeDeclarationLines.canFindIndex(
|
||||||
|
current.line);
|
||||||
if (isCase || isAttribute)
|
if (isCase || isAttribute)
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
if (!currentIs(tok!"{"))
|
if (!currentIs(tok!"{"))
|
||||||
{
|
{
|
||||||
if (isCase && !indents.topIs(tok!"case") && config.dfmt_align_switch_statements == OptionalBoolean.f)
|
if (isCase && !indents.topIs(tok!"case")
|
||||||
|
&& config.dfmt_align_switch_statements == OptionalBoolean.f)
|
||||||
indents.push(tok!"case");
|
indents.push(tok!"case");
|
||||||
else if (isAttribute && !indents.topIs(tok!"@") && config.dfmt_outdent_attributes == OptionalBoolean.f)
|
else if (isAttribute && !indents.topIs(tok!"@")
|
||||||
|
&& config.dfmt_outdent_attributes == OptionalBoolean.f)
|
||||||
indents.push(tok!"@");
|
indents.push(tok!"@");
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
|
@ -616,7 +624,8 @@ private:
|
||||||
auto e = expressionEndIndex(index);
|
auto e = expressionEndIndex(index);
|
||||||
immutable int l = currentLineLength + tokens[index .. e].map!(a => tokenLength(a)).sum();
|
immutable int l = currentLineLength + tokens[index .. e].map!(a => tokenLength(a)).sum();
|
||||||
immutable bool multiline = l > config.dfmt_soft_max_line_length
|
immutable bool multiline = l > config.dfmt_soft_max_line_length
|
||||||
|| tokens[index .. e].canFind!(a => a.type == tok!"comment" || isBlockHeaderToken(a.type))();
|
|| tokens[index .. e].canFind!(a => a.type == tok!"comment"
|
||||||
|
|| isBlockHeaderToken(a.type))();
|
||||||
writeToken();
|
writeToken();
|
||||||
if (multiline)
|
if (multiline)
|
||||||
{
|
{
|
||||||
|
@ -646,7 +655,8 @@ private:
|
||||||
indentLevel = indents.indentLevel - 1;
|
indentLevel = indents.indentLevel - 1;
|
||||||
else
|
else
|
||||||
indentLevel = indents.indentLevel;
|
indentLevel = indents.indentLevel;
|
||||||
if (config.dfmt_brace_style == BraceStyle.allman || peekBackIsOneOf(true, tok!"{", tok!"}"))
|
if (config.dfmt_brace_style == BraceStyle.allman
|
||||||
|
|| peekBackIsOneOf(true, tok!"{", tok!"}"))
|
||||||
newline();
|
newline();
|
||||||
else if (!peekBackIsOneOf(true, tok!"{", tok!"}", tok!";"))
|
else if (!peekBackIsOneOf(true, tok!"{", tok!"}", tok!";"))
|
||||||
write(" ");
|
write(" ");
|
||||||
|
@ -691,7 +701,8 @@ private:
|
||||||
write("}");
|
write("}");
|
||||||
if (index + 1 < tokens.length
|
if (index + 1 < tokens.length
|
||||||
&& astInformation.doubleNewlineLocations.canFindIndex(tokens[index].index)
|
&& astInformation.doubleNewlineLocations.canFindIndex(tokens[index].index)
|
||||||
&& !peekIs(tok!"}") && !peekIs(tok!";") && !peekIs(tok!"comment", false))
|
&& !peekIs(tok!"}") && !peekIs(tok!";") && !peekIs(tok!"comment",
|
||||||
|
false))
|
||||||
{
|
{
|
||||||
simpleNewline();
|
simpleNewline();
|
||||||
currentLineLength = 0;
|
currentLineLength = 0;
|
||||||
|
@ -704,7 +715,8 @@ private:
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!peekIs(tok!",") && !peekIs(tok!")") && !peekIs(tok!";") && !peekIs(tok!"{"))
|
if (!peekIs(tok!",") && !peekIs(tok!")")
|
||||||
|
&& !peekIs(tok!";") && !peekIs(tok!"{"))
|
||||||
{
|
{
|
||||||
index++;
|
index++;
|
||||||
newline();
|
newline();
|
||||||
|
@ -789,7 +801,8 @@ private:
|
||||||
writeParens(config.dfmt_space_after_cast == OptionalBoolean.t);
|
writeParens(config.dfmt_space_after_cast == OptionalBoolean.t);
|
||||||
break;
|
break;
|
||||||
case tok!"out":
|
case tok!"out":
|
||||||
if (!peekBackIs(tok!"}") && astInformation.contractLocations.canFindIndex(current.index))
|
if (!peekBackIs(tok!"}")
|
||||||
|
&& astInformation.contractLocations.canFindIndex(current.index))
|
||||||
newline();
|
newline();
|
||||||
else if (peekBackIsKeyword)
|
else if (peekBackIsKeyword)
|
||||||
write(" ");
|
write(" ");
|
||||||
|
@ -814,15 +827,16 @@ private:
|
||||||
else if (!peekBackIsOneOf(false, tok!"(", tok!",", tok!"!"))
|
else if (!peekBackIsOneOf(false, tok!"(", tok!",", tok!"!"))
|
||||||
write(" ");
|
write(" ");
|
||||||
writeToken();
|
writeToken();
|
||||||
immutable isFunctionLit = astInformation.funLitStartLocations.canFindIndex(current.index);
|
immutable isFunctionLit = astInformation.funLitStartLocations.canFindIndex(
|
||||||
|
current.index);
|
||||||
if (isFunctionLit && config.dfmt_brace_style == BraceStyle.allman)
|
if (isFunctionLit && config.dfmt_brace_style == BraceStyle.allman)
|
||||||
newline();
|
newline();
|
||||||
else if (!isContract)
|
else if (!isContract)
|
||||||
write(" ");
|
write(" ");
|
||||||
break;
|
break;
|
||||||
case tok!"is":
|
case tok!"is":
|
||||||
if (!peekBackIsOneOf(false, tok!"!", tok!"(", tok!",", tok!"}", tok!"=",
|
if (!peekBackIsOneOf(false, tok!"!", tok!"(", tok!",",
|
||||||
tok!"&&", tok!"||") && !peekBackIsKeyword())
|
tok!"}", tok!"=", tok!"&&", tok!"||") && !peekBackIsKeyword())
|
||||||
write(" ");
|
write(" ");
|
||||||
writeToken();
|
writeToken();
|
||||||
if (!currentIs(tok!"(") && !currentIs(tok!"{"))
|
if (!currentIs(tok!"(") && !currentIs(tok!"{"))
|
||||||
|
@ -834,9 +848,12 @@ private:
|
||||||
write(" ");
|
write(" ");
|
||||||
break;
|
break;
|
||||||
case tok!"enum":
|
case tok!"enum":
|
||||||
if (peekIs(tok!")") || peekIs(tok!"==")) {
|
if (peekIs(tok!")") || peekIs(tok!"=="))
|
||||||
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (peekBackIs(tok!"identifier"))
|
if (peekBackIs(tok!"identifier"))
|
||||||
write(" ");
|
write(" ");
|
||||||
indents.push(tok!"enum");
|
indents.push(tok!"enum");
|
||||||
|
@ -850,7 +867,8 @@ private:
|
||||||
write(" ");
|
write(" ");
|
||||||
if (index + 1 < tokens.length)
|
if (index + 1 < tokens.length)
|
||||||
{
|
{
|
||||||
if (!peekIs(tok!"@") && (peekIsOperator() || peekIs(tok!"out") || peekIs(tok!"in")))
|
if (!peekIs(tok!"@") && (peekIsOperator()
|
||||||
|
|| peekIs(tok!"out") || peekIs(tok!"in")))
|
||||||
writeToken();
|
writeToken();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -919,7 +937,8 @@ private:
|
||||||
formatAt();
|
formatAt();
|
||||||
break;
|
break;
|
||||||
case tok!"!":
|
case tok!"!":
|
||||||
if ((peekIs(tok!"is") || peekIs(tok!"in")) && !peekBackIsOneOf(false, tok!"(", tok!"="))
|
if ((peekIs(tok!"is") || peekIs(tok!"in"))
|
||||||
|
&& !peekBackIsOneOf(false, tok!"(", tok!"="))
|
||||||
write(" ");
|
write(" ");
|
||||||
goto case;
|
goto case;
|
||||||
case tok!"...":
|
case tok!"...":
|
||||||
|
@ -1003,7 +1022,8 @@ private:
|
||||||
case tok!"..":
|
case tok!"..":
|
||||||
case tok!"%":
|
case tok!"%":
|
||||||
binary:
|
binary:
|
||||||
immutable bool isWrapToken = linebreakHints.canFind(index) || peekIs(tok!"comment", false);
|
immutable bool isWrapToken = linebreakHints.canFind(index)
|
||||||
|
|| peekIs(tok!"comment", false);
|
||||||
if (config.dfmt_split_operator_at_line_end)
|
if (config.dfmt_split_operator_at_line_end)
|
||||||
{
|
{
|
||||||
if (isWrapToken)
|
if (isWrapToken)
|
||||||
|
@ -1044,14 +1064,15 @@ private:
|
||||||
import std.algorithm : canFind;
|
import std.algorithm : canFind;
|
||||||
|
|
||||||
regenLineBreakHintsIfNecessary(index);
|
regenLineBreakHintsIfNecessary(index);
|
||||||
if (indents.indentToMostRecent(tok!"enum") != -1 && !peekIs(tok!"}")
|
if (indents.indentToMostRecent(tok!"enum") != -1
|
||||||
&& indents.top == tok!"{" && parenDepth == 0)
|
&& !peekIs(tok!"}") && indents.top == tok!"{" && parenDepth == 0)
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
else if (!peekIs(tok!"}") && (linebreakHints.canFind(index)
|
else if (!peekIs(tok!"}") && (linebreakHints.canFind(index)
|
||||||
|| (linebreakHints.length == 0 && currentLineLength > config.dfmt_soft_max_line_length)))
|
|| (linebreakHints.length == 0
|
||||||
|
&& currentLineLength > config.dfmt_soft_max_line_length)))
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
pushWrapIndent(tok!",");
|
pushWrapIndent(tok!",");
|
||||||
|
@ -1088,10 +1109,17 @@ private:
|
||||||
|
|
||||||
final switch (config.end_of_line)
|
final switch (config.end_of_line)
|
||||||
{
|
{
|
||||||
case EOL.cr: output.put("\r"); break;
|
case EOL.cr:
|
||||||
case EOL.lf: output.put("\n"); break;
|
output.put("\r");
|
||||||
case EOL.crlf: output.put("\r\n"); break;
|
break;
|
||||||
case EOL.unspecified: assert(false, "config.end_of_line was unspecified");
|
case EOL.lf:
|
||||||
|
output.put("\n");
|
||||||
|
break;
|
||||||
|
case EOL.crlf:
|
||||||
|
output.put("\r\n");
|
||||||
|
break;
|
||||||
|
case EOL.unspecified:
|
||||||
|
assert(false, "config.end_of_line was unspecified");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1107,8 +1135,8 @@ private:
|
||||||
immutable bool hasCurrent = index < tokens.length;
|
immutable bool hasCurrent = index < tokens.length;
|
||||||
|
|
||||||
if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent
|
if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent
|
||||||
&& tokens[index].type == tok!"}" && !assumeSorted(
|
&& tokens[index].type == tok!"}"
|
||||||
astInformation.funLitEndLocations).equalRange(tokens[index].index).empty)
|
&& !assumeSorted(astInformation.funLitEndLocations).equalRange(tokens[index].index).empty)
|
||||||
{
|
{
|
||||||
write(" ");
|
write(" ");
|
||||||
return;
|
return;
|
||||||
|
@ -1161,7 +1189,8 @@ private:
|
||||||
}
|
}
|
||||||
immutable l = indents.indentToMostRecent(tok!"switch");
|
immutable l = indents.indentToMostRecent(tok!"switch");
|
||||||
if (l != -1)
|
if (l != -1)
|
||||||
indentLevel = config.dfmt_align_switch_statements == OptionalBoolean.t ? l : indents.indentLevel;
|
indentLevel = config.dfmt_align_switch_statements == OptionalBoolean.t ? l
|
||||||
|
: indents.indentLevel;
|
||||||
}
|
}
|
||||||
else if (currentIs(tok!"{"))
|
else if (currentIs(tok!"{"))
|
||||||
{
|
{
|
||||||
|
@ -1182,7 +1211,8 @@ private:
|
||||||
indentLevel = indents.indentToMostRecent(tok!"{");
|
indentLevel = indents.indentToMostRecent(tok!"{");
|
||||||
indents.pop();
|
indents.pop();
|
||||||
}
|
}
|
||||||
while (sBraceDepth == 0 && indents.topIsTemp() && ((indents.top != tok!"if"
|
while (sBraceDepth == 0 && indents.topIsTemp()
|
||||||
|
&& ((indents.top != tok!"if"
|
||||||
&& indents.top != tok!"version") || !peekIs(tok!"else")))
|
&& indents.top != tok!"version") || !peekIs(tok!"else")))
|
||||||
{
|
{
|
||||||
indents.pop();
|
indents.pop();
|
||||||
|
@ -1214,8 +1244,8 @@ private:
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (indents.topIsTemp() && (peekBackIsOneOf(true, tok!"}", tok!";")
|
if (indents.topIsTemp() && (peekBackIsOneOf(true, tok!"}",
|
||||||
&& indents.top != tok!";"))
|
tok!";") && indents.top != tok!";"))
|
||||||
indents.popTempIndents();
|
indents.popTempIndents();
|
||||||
indentLevel = indents.indentLevel;
|
indentLevel = indents.indentLevel;
|
||||||
}
|
}
|
||||||
|
@ -1291,6 +1321,7 @@ private:
|
||||||
void indent()
|
void indent()
|
||||||
{
|
{
|
||||||
import dfmt.editorconfig : IndentStyle;
|
import dfmt.editorconfig : IndentStyle;
|
||||||
|
|
||||||
if (config.indent_style == IndentStyle.tab)
|
if (config.indent_style == IndentStyle.tab)
|
||||||
{
|
{
|
||||||
foreach (i; 0 .. indentLevel)
|
foreach (i; 0 .. indentLevel)
|
||||||
|
@ -1369,7 +1400,8 @@ const pure @safe @nogc:
|
||||||
return tokenLength(tokens[i]);
|
return tokenLength(tokens[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ref current() nothrow in
|
ref current() nothrow
|
||||||
|
in
|
||||||
{
|
{
|
||||||
assert(index < tokens.length);
|
assert(index < tokens.length);
|
||||||
}
|
}
|
||||||
|
@ -1536,10 +1568,10 @@ const pure @safe @nogc:
|
||||||
|
|
||||||
bool isBlockHeaderToken(IdType t)
|
bool isBlockHeaderToken(IdType t)
|
||||||
{
|
{
|
||||||
return t == tok!"for" || t == tok!"foreach" || t == tok!"foreach_reverse"
|
return t == tok!"for" || t == tok!"foreach"
|
||||||
|| t == tok!"while" || t == tok!"if" || t == tok!"out" || t == tok!"do"
|
|| t == tok!"foreach_reverse" || t == tok!"while" || t == tok!"if"
|
||||||
|| t == tok!"catch" || t == tok!"with" || t == tok!"synchronized"
|
|| t == tok!"out" || t == tok!"do" || t == tok!"catch"
|
||||||
|| t == tok!"scope";
|
|| t == tok!"with" || t == tok!"synchronized" || t == tok!"scope";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isBlockHeader(int i = 0) nothrow
|
bool isBlockHeader(int i = 0) nothrow
|
||||||
|
|
Loading…
Reference in New Issue