Run dfmt on itself

This commit is contained in:
Hackerpilot 2016-01-26 13:02:06 -08:00
parent 0ea6b340a4
commit c1f189b124
7 changed files with 57 additions and 63 deletions

View File

@ -88,8 +88,7 @@ struct Config
if (dfmt_soft_max_line_length > max_line_length) if (dfmt_soft_max_line_length > max_line_length)
{ {
stderr.writefln( stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)",
"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;
} }

View File

@ -261,8 +261,7 @@ 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")
|| currentIs(tok!"if")
|| isNumberLiteral(tokens[index].type) || (inAsm || isNumberLiteral(tokens[index].type) || (inAsm
&& peekBack2Is(tok!";") && currentIs(tok!"[")))) && peekBack2Is(tok!";") && currentIs(tok!"["))))
{ {
@ -438,8 +437,8 @@ private:
break; break;
else if (t == tok!"import" && !currentIs(tok!"import") else if (t == tok!"import" && !currentIs(tok!"import")
&& !currentIs(tok!"}") && !(currentIs(tok!"public") && !currentIs(tok!"}") && !(currentIs(tok!"public")
&& peekIs(tok!"import")) && peekIs(tok!"import")) && !indents.topIsOneOf(tok!"if",
&& !indents.topIsOneOf(tok!"if", tok!"debug", tok!"version")) tok!"debug", tok!"version"))
{ {
simpleNewline(); simpleNewline();
currentLineLength = 0; currentLineLength = 0;
@ -503,8 +502,7 @@ private:
spaceAfterParens = true; spaceAfterParens = true;
parenDepth++; parenDepth++;
} }
immutable bool arrayInitializerStart = p == tok!"[" immutable bool arrayInitializerStart = p == tok!"[" && linebreakHints.length != 0
&& linebreakHints.length != 0
&& astInformation.arrayStartLocations.canFindIndex(tokens[index - 1].index); && astInformation.arrayStartLocations.canFindIndex(tokens[index - 1].index);
if (arrayInitializerStart) if (arrayInitializerStart)
{ {
@ -518,8 +516,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.canFindIndex(index - 1) || (linebreakHints.length == 0
|| (linebreakHints.length == 0 && currentLineLength > config.max_line_length))) && currentLineLength > config.max_line_length)))
{ {
newline(); newline();
} }
@ -689,8 +687,8 @@ private:
{ {
sBraceDepth++; sBraceDepth++;
auto e = expressionEndIndex(index); auto e = expressionEndIndex(index);
immutable int l = currentLineLength + tokens[index .. e].map!( immutable int l = currentLineLength + tokens[index .. e].map!(a => tokenLength(a))
a => tokenLength(a)).sum(); .sum();
writeToken(); writeToken();
if (l > config.dfmt_soft_max_line_length) if (l > config.dfmt_soft_max_line_length)
{ {
@ -706,8 +704,8 @@ private:
if (peekBackIs(tok!")")) if (peekBackIs(tok!")"))
write(" "); write(" ");
auto e = expressionEndIndex(index); auto e = expressionEndIndex(index);
immutable int l = currentLineLength + tokens[index .. e].map!( immutable int l = currentLineLength + tokens[index .. e].map!(a => tokenLength(a))
a => tokenLength(a)).sum(); .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" || tokens[index .. e].canFind!(a => a.type == tok!"comment"
|| isBlockHeaderToken(a.type))(); || isBlockHeaderToken(a.type))();
@ -785,16 +783,16 @@ private:
newline(); newline();
write("}"); write("}");
if (index + 1 < tokens.length if (index + 1 < tokens.length
&& astInformation.doubleNewlineLocations.canFindIndex( && astInformation.doubleNewlineLocations.canFindIndex(tokens[index].index)
tokens[index].index) && !peekIs(tok!"}") && !peekIs(tok!"}") && !peekIs(tok!"else")
&& !peekIs(tok!"else") && !peekIs(tok!";") && !peekIs(tok!"comment", false)) && !peekIs(tok!";") && !peekIs(tok!"comment", false))
{ {
simpleNewline(); simpleNewline();
currentLineLength = 0; currentLineLength = 0;
justAddedExtraNewline = true; justAddedExtraNewline = true;
} }
if (config.dfmt_brace_style == BraceStyle.otbs && peekIs(tok!"else") if (config.dfmt_brace_style == BraceStyle.otbs
&& !indents.topAre(tok!"static", tok!"if")) && peekIs(tok!"else") && !indents.topAre(tok!"static", tok!"if"))
{ {
write(" "); write(" ");
index++; index++;
@ -1224,7 +1222,8 @@ private:
{ {
immutable size_t j = expressionEndIndex(i); immutable size_t j = expressionEndIndex(i);
// Use magical negative value for array literals and wrap indents // Use magical negative value for array literals and wrap indents
immutable inLvl = (indents.topIsWrap() || indents.topIs(tok!"]")) ? -indentLevel : indentLevel; immutable inLvl = (indents.topIsWrap() || indents.topIs(tok!"]")) ? -indentLevel
: indentLevel;
linebreakHints = chooseLineBreakTokens(i, tokens[i .. j], depths[i .. j], linebreakHints = chooseLineBreakTokens(i, tokens[i .. j], depths[i .. j],
config, currentLineLength, inLvl); config, currentLineLength, inLvl);
} }
@ -1266,8 +1265,7 @@ 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!"}"
&& tokens[index].type == tok!"}"
&& !assumeSorted(astInformation.funLitEndLocations).equalRange( && !assumeSorted(astInformation.funLitEndLocations).equalRange(
tokens[index].index).empty) tokens[index].index).empty)
{ {
@ -1351,8 +1349,8 @@ private:
indents.pop(); indents.pop();
} }
while (sBraceDepth == 0 && indents.topIsTemp() while (sBraceDepth == 0 && indents.topIsTemp()
&& ((!indents.topIsOneOf(tok!"else", tok!"if", tok!"static", tok!"version")) && ((!indents.topIsOneOf(tok!"else", tok!"if",
|| !peekIs(tok!"else"))) tok!"static", tok!"version")) || !peekIs(tok!"else")))
{ {
indents.pop(); indents.pop();
} }
@ -1714,10 +1712,10 @@ const pure @safe @nogc:
bool isBlockHeaderToken(IdType t) bool isBlockHeaderToken(IdType t)
{ {
return t == tok!"for" || t == tok!"foreach" return t == tok!"for" || t == tok!"foreach" || t == tok!"foreach_reverse"
|| t == tok!"foreach_reverse" || t == tok!"while" || t == tok!"if" || t == tok!"while" || t == tok!"if" || t == tok!"out"
|| t == tok!"out" || t == tok!"do" || t == tok!"catch" || t == tok!"do" || t == tok!"catch" || t == tok!"with"
|| t == tok!"with" || t == tok!"synchronized" || t == tok!"scope"; || t == tok!"synchronized" || t == tok!"scope";
} }
bool isBlockHeader(int i = 0) nothrow bool isBlockHeader(int i = 0) nothrow

View File

@ -183,8 +183,8 @@ private:
int parenCount; int parenCount;
foreach (i; 0 .. j) foreach (i; 0 .. j)
{ {
immutable int pc = (arr[i] == tok!"!" || arr[i] == tok!"(" || arr[i] == tok!")") ? parenCount + 1 immutable int pc = (arr[i] == tok!"!" || arr[i] == tok!"(" || arr[i] == tok!")") ? parenCount
: parenCount; + 1 : parenCount;
if ((isWrapIndent(arr[i]) || arr[i] == tok!"(") && parenCount > 1) if ((isWrapIndent(arr[i]) || arr[i] == tok!"(") && parenCount > 1)
{ {
parenCount = pc; parenCount = pc;
@ -196,8 +196,8 @@ private:
continue; continue;
immutable currentIsNonWrapTemp = !isWrapIndent(arr[i]) immutable currentIsNonWrapTemp = !isWrapIndent(arr[i])
&& isTempIndent(arr[i]) && arr[i] != tok!")" && arr[i] != tok!"!"; && isTempIndent(arr[i]) && arr[i] != tok!")" && arr[i] != tok!"!";
if (arr[i] == tok!"static" && (arr[i + 1] == tok!"if" || arr[i + 1] == tok!"else") if (arr[i] == tok!"static" && (arr[i + 1] == tok!"if"
&& (i + 2 >= index || arr[i + 2] != tok!"{")) || arr[i + 1] == tok!"else") && (i + 2 >= index || arr[i + 2] != tok!"{"))
{ {
parenCount = pc; parenCount = pc;
continue; continue;

View File

@ -209,10 +209,9 @@ Options:
Formatting Options: Formatting Options:
--align_switch_statements --align_switch_statements
--brace_style `, --brace_style `, optionsToString!(typeof(Config.dfmt_brace_style))(),
optionsToString!(typeof(Config.dfmt_brace_style))(), ` `
--end_of_line `, --end_of_line `, optionsToString!(typeof(Config.end_of_line))(), `
optionsToString!(typeof(Config.end_of_line))(), `
--help|h --help|h
--indent_size --indent_size
--indent_style|t `, --indent_style|t `,

View File

@ -217,24 +217,21 @@ private string generateFixedLengthCases()
string[] identifierTokens = [ string[] identifierTokens = [
"abstract", "alias", "align", "asm", "assert", "auto", "body", "bool", "abstract", "alias", "align", "asm", "assert", "auto", "body", "bool",
"break", "byte", "case", "cast", "catch", "cdouble", "cent", "cfloat", "break", "byte", "case", "cast", "catch", "cdouble", "cent", "cfloat", "char", "class",
"char", "class", "const", "continue", "creal", "dchar", "debug", "const", "continue", "creal", "dchar", "debug", "default", "delegate", "delete", "deprecated",
"default", "delegate", "delete", "deprecated", "do", "double", "else", "do", "double", "else", "enum", "export", "extern", "false", "final", "finally", "float",
"enum", "export", "extern", "false", "final", "finally", "float", "for", "foreach", "foreach_reverse", "function", "goto", "idouble", "if", "ifloat", "immutable",
"for", "foreach", "foreach_reverse", "function", "goto", "idouble", "import", "in", "inout", "int", "interface", "invariant", "ireal", "is",
"if", "ifloat", "immutable", "import", "in", "inout", "int", "lazy", "long", "macro", "mixin", "module", "new", "nothrow", "null", "out", "override",
"interface", "invariant", "ireal", "is", "lazy", "long", "macro", "package", "pragma", "private", "protected", "public", "pure", "real", "ref", "return", "scope",
"mixin", "module", "new", "nothrow", "null", "out", "override", "shared", "short", "static", "struct", "super", "switch", "synchronized", "template", "this",
"package", "pragma", "private", "protected", "public", "pure", "real", "throw", "true", "try", "typedef", "typeid", "typeof", "ubyte", "ucent", "uint", "ulong",
"ref", "return", "scope", "shared", "short", "static", "struct", "union", "unittest", "ushort", "version", "void", "volatile", "wchar",
"super", "switch", "synchronized", "template", "this", "throw", "true", "while", "with", "__DATE__", "__EOF__", "__FILE__",
"try", "typedef", "typeid", "typeof", "ubyte", "ucent", "uint",
"ulong", "union", "unittest", "ushort", "version", "void", "volatile",
"wchar", "while", "with", "__DATE__", "__EOF__", "__FILE__",
"__FUNCTION__", "__gshared", "__LINE__", "__MODULE__", "__parameters", "__FUNCTION__", "__gshared", "__LINE__", "__MODULE__", "__parameters",
"__PRETTY_FUNCTION__", "__TIME__", "__TIMESTAMP__", "__traits", "__PRETTY_FUNCTION__", "__TIME__", "__TIMESTAMP__",
"__vector", "__VENDOR__", "__VERSION__", "$", "++", "--", ".", "[", "__traits", "__vector", "__VENDOR__", "__VERSION__", "$", "++", "--",
"]", "(", ")", "{", "}" ".", "[", "]", "(", ")", "{", "}"
]; ];
immutable identifierTokenCases = identifierTokens.map!( immutable identifierTokenCases = identifierTokens.map!(
a => format(`case tok!"%s": return %d;`, a, a.length)).join("\n\t"); a => format(`case tok!"%s": return %d;`, a, a.length)).join("\n\t");

View File

@ -30,7 +30,8 @@ struct State
immutable int l = currentLineLength + tokens.map!(a => tokenLength(a)).sum(); immutable int l = currentLineLength + tokens.map!(a => tokenLength(a)).sum();
if (l > config.dfmt_soft_max_line_length) if (l > config.dfmt_soft_max_line_length)
{ {
immutable int longPenalty = (l - config.dfmt_soft_max_line_length) * remainingCharsMultiplier; immutable int longPenalty = (l - config.dfmt_soft_max_line_length)
* remainingCharsMultiplier;
this._cost += longPenalty; this._cost += longPenalty;
this._solved = longPenalty < newlinePenalty; this._solved = longPenalty < newlinePenalty;
} }
@ -59,7 +60,8 @@ struct State
ll += tokens[i .. j].map!(a => tokenLength(a)).sum(); ll += tokens[i .. j].map!(a => tokenLength(a)).sum();
if (ll > config.dfmt_soft_max_line_length) if (ll > config.dfmt_soft_max_line_length)
{ {
immutable int longPenalty = (ll - config.dfmt_soft_max_line_length) * remainingCharsMultiplier; immutable int longPenalty = (ll - config.dfmt_soft_max_line_length)
* remainingCharsMultiplier;
this._cost += longPenalty; this._cost += longPenalty;
} }
if (ll > config.max_line_length) if (ll > config.max_line_length)
@ -166,9 +168,8 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens,
assert(false); assert(false);
} }
void validMoves(OR)(auto ref OR output, const Token[] tokens, void validMoves(OR)(auto ref OR output, const Token[] tokens, immutable short[] depths,
immutable short[] depths, uint current, const Config* config, uint current, const Config* config, int currentLineLength, int indentLevel)
int currentLineLength, int indentLevel)
{ {
import std.algorithm : sort, canFind, min; import std.algorithm : sort, canFind, min;
import std.array : insertInPlace; import std.array : insertInPlace;