Attempt to improve indentation code

This commit is contained in:
Hackerpilot 2016-01-19 01:45:30 -08:00
parent 7fa11c8505
commit bf0843f321
2 changed files with 15 additions and 15 deletions

View File

@ -351,7 +351,7 @@ private:
indents.pop(); indents.pop();
else if (peekBack2Is(tok!",") && !indents.topIs(tok!",") else if (peekBack2Is(tok!",") && !indents.topIs(tok!",")
&& indents.indentToMostRecent(tok!"enum") == -1) && indents.indentToMostRecent(tok!"enum") == -1)
indents.push(tok!","); pushWrapIndent(tok!",");
newline(); newline();
} }
} }
@ -440,11 +440,12 @@ private:
} }
body body
{ {
immutable p = tokens[index].type; immutable p = current.type;
regenLineBreakHintsIfNecessary(index); regenLineBreakHintsIfNecessary(index);
writeToken(); writeToken();
if (p == tok!"(") if (p == tok!"(")
{ {
indents.push(p);
spaceAfterParens = true; spaceAfterParens = true;
parenDepth++; parenDepth++;
} }
@ -454,7 +455,7 @@ private:
if (arrayInitializerStart) if (arrayInitializerStart)
{ {
// Use the close bracket as the indent token to distinguish // Use the close bracket as the indent token to distinguish
// the array initialiazer from an array index in the newling // the array initialiazer from an array index in the newline
// handling code // handling code
pushWrapIndent(tok!"]"); pushWrapIndent(tok!"]");
newline(); newline();
@ -466,7 +467,6 @@ private:
&& (linebreakHints.canFindIndex(index - 1) && (linebreakHints.canFindIndex(index - 1)
|| (linebreakHints.length == 0 && currentLineLength > config.max_line_length))) || (linebreakHints.length == 0 && currentLineLength > config.max_line_length)))
{ {
pushWrapIndent(p);
newline(); newline();
} }
} }
@ -481,6 +481,8 @@ private:
parenDepth--; parenDepth--;
if (parenDepth == 0) if (parenDepth == 0)
indents.popWrapIndents(); indents.popWrapIndents();
if (indents.topIs(tok!"("))
indents.pop();
if (parenDepth == 0 && (peekIs(tok!"is") || peekIs(tok!"in") if (parenDepth == 0 && (peekIs(tok!"is") || peekIs(tok!"in")
|| peekIs(tok!"out") || peekIs(tok!"body"))) || peekIs(tok!"out") || peekIs(tok!"body")))
@ -505,17 +507,13 @@ private:
immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]); immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
if (l > config.dfmt_soft_max_line_length) if (l > config.dfmt_soft_max_line_length)
{ {
// The order of these two calls is intentional
newline(); newline();
pushWrapIndent(tok!"!");
} }
else else
write(" "); write(" ");
break; break;
case always_newline: case always_newline:
// The order of these two calls is intentional
newline(); newline();
pushWrapIndent(tok!"!");
break; break;
case conditional_newline_indent: case conditional_newline_indent:
immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]); immutable l = currentLineLength + betweenParenLength(tokens[index + 1 .. $]);
@ -801,6 +799,8 @@ private:
void formatBlockHeader() void formatBlockHeader()
{ {
if (indents.topIs(tok!"!"))
indents.pop();
immutable bool a = !currentIs(tok!"version") && !currentIs(tok!"debug"); immutable bool a = !currentIs(tok!"version") && !currentIs(tok!"debug");
immutable bool b = a immutable bool b = a
|| astInformation.conditionalWithElseLocations.canFindIndex(current.index); || astInformation.conditionalWithElseLocations.canFindIndex(current.index);
@ -1152,8 +1152,8 @@ private:
|| (linebreakHints.length == 0 || (linebreakHints.length == 0
&& currentLineLength > config.dfmt_soft_max_line_length))) && currentLineLength > config.dfmt_soft_max_line_length)))
{ {
pushWrapIndent();
writeToken(); writeToken();
pushWrapIndent(tok!",");
newline(); newline();
} }
else else
@ -1326,7 +1326,7 @@ private:
if (indents.topIsTemp() && (peekBackIsOneOf(true, tok!"}", if (indents.topIsTemp() && (peekBackIsOneOf(true, tok!"}",
tok!";") && indents.top != tok!";")) tok!";") && indents.top != tok!";"))
indents.popTempIndents(); indents.popTempIndents();
indentLevel = indents.indentLevel; indentLevel = indents.indentLevel + parenDepth;
} }
indent(); indent();
} }
@ -1378,7 +1378,7 @@ private:
{ {
if (currentLineLength >= config.dfmt_soft_max_line_length) if (currentLineLength >= config.dfmt_soft_max_line_length)
{ {
pushWrapIndent(tok!";"); pushWrapIndent();
writeToken(); writeToken();
newline(); newline();
} }

View File

@ -13,7 +13,7 @@ import dparse.lexer;
bool isWrapIndent(IdType type) pure nothrow @nogc @safe bool isWrapIndent(IdType type) pure nothrow @nogc @safe
{ {
return type != tok!"{" && type != tok!"case" && type != tok!"@" return type != tok!"{" && type != tok!"case" && type != tok!"@"
&& type != tok!"]" && isOperator(type); && type != tok!"]" && type != tok!"(" && isOperator(type);
} }
/** /**
@ -30,8 +30,7 @@ bool isTempIndent(IdType type) pure nothrow @nogc @safe
struct IndentStack struct IndentStack
{ {
/** /**
* Modifies the indent stack to match the state that it had at the most * Get the indent size at the most recent occurence of the given indent type
* recent appearance of the given token type.
*/ */
int indentToMostRecent(IdType item) const int indentToMostRecent(IdType item) const
{ {
@ -173,7 +172,8 @@ private:
immutable bool currentIsTemp = isTempIndent(arr[i]); immutable bool currentIsTemp = isTempIndent(arr[i]);
immutable bool nextIsTemp = isTempIndent(arr[i + 1]); immutable bool nextIsTemp = isTempIndent(arr[i + 1]);
immutable bool nextIsSwitch = arr[i + 1] == tok!"switch"; immutable bool nextIsSwitch = arr[i + 1] == tok!"switch";
if (currentIsTemp && (!nextIsTemp || nextIsSwitch)) immutable bool nextIsWrap = isWrapIndent(arr[i + 1]);
if (((nextIsSwitch || nextIsWrap) && currentIsTemp))
continue; continue;
} }
size++; size++;