From c1f189b1249d8cb06d21b9a6cafa052108dc87e7 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Tue, 26 Jan 2016 13:02:06 -0800 Subject: [PATCH] Run dfmt on itself --- src/dfmt/config.d | 3 +- src/dfmt/formatter.d | 54 +++++++++++++++---------------- src/dfmt/globmatch_editorconfig.d | 2 +- src/dfmt/indentation.d | 8 ++--- src/dfmt/main.d | 7 ++-- src/dfmt/tokens.d | 35 +++++++++----------- src/dfmt/wrapping.d | 11 ++++--- 7 files changed, 57 insertions(+), 63 deletions(-) diff --git a/src/dfmt/config.d b/src/dfmt/config.d index 5158d44..10c504f 100644 --- a/src/dfmt/config.d +++ b/src/dfmt/config.d @@ -88,8 +88,7 @@ struct Config if (dfmt_soft_max_line_length > max_line_length) { - stderr.writefln( - "Column hard limit (%d) must be greater than or equal to column soft limit (%d)", + stderr.writefln("Column hard limit (%d) must be greater than or equal to column soft limit (%d)", max_line_length, dfmt_soft_max_line_length); return false; } diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index abfaf84..b0ab4f8 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -261,8 +261,7 @@ private: { writeToken(); if (index < tokens.length && (currentIs(tok!"identifier") - || isBasicType(current.type) || currentIs(tok!"@") - || currentIs(tok!"if") + || isBasicType(current.type) || currentIs(tok!"@") || currentIs(tok!"if") || isNumberLiteral(tokens[index].type) || (inAsm && peekBack2Is(tok!";") && currentIs(tok!"[")))) { @@ -438,8 +437,8 @@ private: break; else if (t == tok!"import" && !currentIs(tok!"import") && !currentIs(tok!"}") && !(currentIs(tok!"public") - && peekIs(tok!"import")) - && !indents.topIsOneOf(tok!"if", tok!"debug", tok!"version")) + && peekIs(tok!"import")) && !indents.topIsOneOf(tok!"if", + tok!"debug", tok!"version")) { simpleNewline(); currentLineLength = 0; @@ -503,8 +502,7 @@ private: spaceAfterParens = true; parenDepth++; } - immutable bool arrayInitializerStart = p == tok!"[" - && linebreakHints.length != 0 + immutable bool arrayInitializerStart = p == tok!"[" && linebreakHints.length != 0 && astInformation.arrayStartLocations.canFindIndex(tokens[index - 1].index); if (arrayInitializerStart) { @@ -518,8 +516,8 @@ private: depths[index .. j], config, currentLineLength, indentLevel); } else if (!currentIs(tok!")") && !currentIs(tok!"]") - && (linebreakHints.canFindIndex(index - 1) - || (linebreakHints.length == 0 && currentLineLength > config.max_line_length))) + && (linebreakHints.canFindIndex(index - 1) || (linebreakHints.length == 0 + && currentLineLength > config.max_line_length))) { newline(); } @@ -585,7 +583,7 @@ private: immutable bool isCase = astInformation.caseEndLocations.canFindIndex(current.index); immutable bool isAttribute = astInformation.attributeDeclarationLines.canFindIndex( - current.line); + current.line); if (isCase || isAttribute) { writeToken(); @@ -689,8 +687,8 @@ private: { sBraceDepth++; 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(); writeToken(); if (l > config.dfmt_soft_max_line_length) { @@ -706,8 +704,8 @@ private: if (peekBackIs(tok!")")) write(" "); 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 || tokens[index .. e].canFind!(a => a.type == tok!"comment" || isBlockHeaderToken(a.type))(); @@ -785,16 +783,16 @@ private: newline(); write("}"); if (index + 1 < tokens.length - && astInformation.doubleNewlineLocations.canFindIndex( - tokens[index].index) && !peekIs(tok!"}") - && !peekIs(tok!"else") && !peekIs(tok!";") && !peekIs(tok!"comment", false)) + && astInformation.doubleNewlineLocations.canFindIndex(tokens[index].index) + && !peekIs(tok!"}") && !peekIs(tok!"else") + && !peekIs(tok!";") && !peekIs(tok!"comment", false)) { simpleNewline(); currentLineLength = 0; justAddedExtraNewline = true; } - if (config.dfmt_brace_style == BraceStyle.otbs && peekIs(tok!"else") - && !indents.topAre(tok!"static", tok!"if")) + if (config.dfmt_brace_style == BraceStyle.otbs + && peekIs(tok!"else") && !indents.topAre(tok!"static", tok!"if")) { write(" "); index++; @@ -951,7 +949,7 @@ private: write(" "); writeToken(); immutable isFunctionLit = astInformation.funLitStartLocations.canFindIndex( - current.index); + current.index); if (isFunctionLit && config.dfmt_brace_style == BraceStyle.allman) newline(); else if (!isContract) @@ -1224,7 +1222,8 @@ private: { immutable size_t j = expressionEndIndex(i); // 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], config, currentLineLength, inLvl); } @@ -1266,8 +1265,7 @@ private: immutable bool hasCurrent = index < tokens.length; - if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent - && tokens[index].type == tok!"}" + if (niBraceDepth > 0 && !peekBackIsSlashSlash() && hasCurrent && tokens[index].type == tok!"}" && !assumeSorted(astInformation.funLitEndLocations).equalRange( tokens[index].index).empty) { @@ -1351,8 +1349,8 @@ private: indents.pop(); } while (sBraceDepth == 0 && indents.topIsTemp() - && ((!indents.topIsOneOf(tok!"else", tok!"if", tok!"static", tok!"version")) - || !peekIs(tok!"else"))) + && ((!indents.topIsOneOf(tok!"else", tok!"if", + tok!"static", tok!"version")) || !peekIs(tok!"else"))) { indents.pop(); } @@ -1714,10 +1712,10 @@ const pure @safe @nogc: bool isBlockHeaderToken(IdType t) { - return t == tok!"for" || t == tok!"foreach" - || t == tok!"foreach_reverse" || t == tok!"while" || t == tok!"if" - || t == tok!"out" || t == tok!"do" || t == tok!"catch" - || t == tok!"with" || t == tok!"synchronized" || t == tok!"scope"; + return t == tok!"for" || t == tok!"foreach" || t == tok!"foreach_reverse" + || t == tok!"while" || t == tok!"if" || t == tok!"out" + || t == tok!"do" || t == tok!"catch" || t == tok!"with" + || t == tok!"synchronized" || t == tok!"scope"; } bool isBlockHeader(int i = 0) nothrow diff --git a/src/dfmt/globmatch_editorconfig.d b/src/dfmt/globmatch_editorconfig.d index 980ce6c..ac1106f 100644 --- a/src/dfmt/globmatch_editorconfig.d +++ b/src/dfmt/globmatch_editorconfig.d @@ -11,7 +11,7 @@ import std.path : filenameCharCmp, isDirSeparator; // * changes meaning to match all characters except '/' // ** added to take over the old meaning of * bool globMatchEditorConfig(CaseSensitive cs = CaseSensitive.osDefault, C, Range)( - Range path, const(C)[] pattern) @safe pure nothrow + Range path, const(C)[] pattern) @safe pure nothrow if (isForwardRange!Range && isSomeChar!(ElementEncodingType!Range) && isSomeChar!C && is(Unqual!C == Unqual!(ElementEncodingType!Range))) in diff --git a/src/dfmt/indentation.d b/src/dfmt/indentation.d index b11f5b9..a7b1005 100644 --- a/src/dfmt/indentation.d +++ b/src/dfmt/indentation.d @@ -183,8 +183,8 @@ private: int parenCount; foreach (i; 0 .. j) { - immutable int pc = (arr[i] == tok!"!" || arr[i] == tok!"(" || arr[i] == tok!")") ? parenCount + 1 - : parenCount; + immutable int pc = (arr[i] == tok!"!" || arr[i] == tok!"(" || arr[i] == tok!")") ? parenCount + + 1 : parenCount; if ((isWrapIndent(arr[i]) || arr[i] == tok!"(") && parenCount > 1) { parenCount = pc; @@ -196,8 +196,8 @@ private: continue; immutable currentIsNonWrapTemp = !isWrapIndent(arr[i]) && isTempIndent(arr[i]) && arr[i] != tok!")" && arr[i] != tok!"!"; - if (arr[i] == tok!"static" && (arr[i + 1] == tok!"if" || arr[i + 1] == tok!"else") - && (i + 2 >= index || arr[i + 2] != tok!"{")) + if (arr[i] == tok!"static" && (arr[i + 1] == tok!"if" + || arr[i + 1] == tok!"else") && (i + 2 >= index || arr[i + 2] != tok!"{")) { parenCount = pc; continue; diff --git a/src/dfmt/main.d b/src/dfmt/main.d index 2fa1912..13d890a 100644 --- a/src/dfmt/main.d +++ b/src/dfmt/main.d @@ -209,10 +209,9 @@ Options: Formatting Options: --align_switch_statements - --brace_style `, - optionsToString!(typeof(Config.dfmt_brace_style))(), ` - --end_of_line `, - optionsToString!(typeof(Config.end_of_line))(), ` + --brace_style `, optionsToString!(typeof(Config.dfmt_brace_style))(), + ` + --end_of_line `, optionsToString!(typeof(Config.end_of_line))(), ` --help|h --indent_size --indent_style|t `, diff --git a/src/dfmt/tokens.d b/src/dfmt/tokens.d index a04b32c..8c59c37 100644 --- a/src/dfmt/tokens.d +++ b/src/dfmt/tokens.d @@ -213,30 +213,27 @@ private string generateFixedLengthCases() "^^=", "|", "|=", "||", "~", "~=" ]; immutable spacedOperatorTokenCases = spacedOperatorTokens.map!( - a => format(`case tok!"%s": return %d + 1;`, a, a.length)).join("\n\t"); + a => format(`case tok!"%s": return %d + 1;`, a, a.length)).join("\n\t"); string[] identifierTokens = [ "abstract", "alias", "align", "asm", "assert", "auto", "body", "bool", - "break", "byte", "case", "cast", "catch", "cdouble", "cent", "cfloat", - "char", "class", "const", "continue", "creal", "dchar", "debug", - "default", "delegate", "delete", "deprecated", "do", "double", "else", - "enum", "export", "extern", "false", "final", "finally", "float", - "for", "foreach", "foreach_reverse", "function", "goto", "idouble", - "if", "ifloat", "immutable", "import", "in", "inout", "int", - "interface", "invariant", "ireal", "is", "lazy", "long", "macro", - "mixin", "module", "new", "nothrow", "null", "out", "override", - "package", "pragma", "private", "protected", "public", "pure", "real", - "ref", "return", "scope", "shared", "short", "static", "struct", - "super", "switch", "synchronized", "template", "this", "throw", "true", - "try", "typedef", "typeid", "typeof", "ubyte", "ucent", "uint", - "ulong", "union", "unittest", "ushort", "version", "void", "volatile", - "wchar", "while", "with", "__DATE__", "__EOF__", "__FILE__", + "break", "byte", "case", "cast", "catch", "cdouble", "cent", "cfloat", "char", "class", + "const", "continue", "creal", "dchar", "debug", "default", "delegate", "delete", "deprecated", + "do", "double", "else", "enum", "export", "extern", "false", "final", "finally", "float", + "for", "foreach", "foreach_reverse", "function", "goto", "idouble", "if", "ifloat", "immutable", + "import", "in", "inout", "int", "interface", "invariant", "ireal", "is", + "lazy", "long", "macro", "mixin", "module", "new", "nothrow", "null", "out", "override", + "package", "pragma", "private", "protected", "public", "pure", "real", "ref", "return", "scope", + "shared", "short", "static", "struct", "super", "switch", "synchronized", "template", "this", + "throw", "true", "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", - "__PRETTY_FUNCTION__", "__TIME__", "__TIMESTAMP__", "__traits", - "__vector", "__VENDOR__", "__VERSION__", "$", "++", "--", ".", "[", - "]", "(", ")", "{", "}" + "__PRETTY_FUNCTION__", "__TIME__", "__TIMESTAMP__", + "__traits", "__vector", "__VENDOR__", "__VERSION__", "$", "++", "--", + ".", "[", "]", "(", ")", "{", "}" ]; 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"); return spacedOperatorTokenCases ~ identifierTokenCases; } diff --git a/src/dfmt/wrapping.d b/src/dfmt/wrapping.d index 8b8d7ec..e12936a 100644 --- a/src/dfmt/wrapping.d +++ b/src/dfmt/wrapping.d @@ -30,7 +30,8 @@ struct State immutable int l = currentLineLength + tokens.map!(a => tokenLength(a)).sum(); 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._solved = longPenalty < newlinePenalty; } @@ -59,7 +60,8 @@ struct State ll += tokens[i .. j].map!(a => tokenLength(a)).sum(); 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; } if (ll > config.max_line_length) @@ -166,9 +168,8 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens, assert(false); } -void validMoves(OR)(auto ref OR output, const Token[] tokens, - immutable short[] depths, uint current, const Config* config, - int currentLineLength, int indentLevel) +void validMoves(OR)(auto ref OR output, const Token[] tokens, immutable short[] depths, + uint current, const Config* config, int currentLineLength, int indentLevel) { import std.algorithm : sort, canFind, min; import std.array : insertInPlace;