Fix #267 - Indentation with debug inconsistent

This commit is contained in:
Laurent Tréguier 2018-10-10 11:11:00 +02:00 committed by The Dlang Bot
parent 908f32e433
commit 2578dbf2a7
4 changed files with 59 additions and 1 deletions

View File

@ -174,6 +174,9 @@ private:
/// True if the next "this" should have a space behind it /// True if the next "this" should have a space behind it
bool thisSpace; bool thisSpace;
/// True if the next "else" should be formatted as a single line
bool inlineElse;
void formatStep() void formatStep()
{ {
import std.range : assumeSorted; import std.range : assumeSorted;
@ -281,6 +284,8 @@ private:
} }
else if (isKeyword(current.type)) else if (isKeyword(current.type))
{ {
if (currentIs(tok!"debug"))
inlineElse = true;
formatKeyword(); formatKeyword();
} }
else if (current.text == "body" && peekBackIsFunctionDeclarationEnding()) else if (current.text == "body" && peekBackIsFunctionDeclarationEnding())
@ -710,6 +715,9 @@ private:
void formatSemicolon() void formatSemicolon()
{ {
if (inlineElse && !peekIs(tok!"else"))
inlineElse = false;
if ((parenDepth > 0 && sBraceDepth == 0) || (sBraceDepth > 0 && niBraceDepth > 0)) if ((parenDepth > 0 && sBraceDepth == 0) || (sBraceDepth > 0 && niBraceDepth > 0))
{ {
if (currentLineLength > config.dfmt_soft_max_line_length) if (currentLineLength > config.dfmt_soft_max_line_length)
@ -1001,11 +1009,12 @@ private:
void formatElse() void formatElse()
{ {
writeToken(); writeToken();
if (currentIs(tok!"if") || currentIs(tok!"version") if (inlineElse || currentIs(tok!"if") || currentIs(tok!"version")
|| (currentIs(tok!"static") && peekIs(tok!"if"))) || (currentIs(tok!"static") && peekIs(tok!"if")))
{ {
if (indents.topIs(tok!"if") || indents.topIs(tok!"version")) if (indents.topIs(tok!"if") || indents.topIs(tok!"version"))
indents.pop(); indents.pop();
inlineElse = false;
write(" "); write(" ");
} }
else if (currentIs(tok!":")) else if (currentIs(tok!":"))

View File

@ -0,0 +1,19 @@
void main()
{
debug foo();
else bar();
debug (0)
foo();
else
bar();
// inlineElse reset check
debug foo();
if (true)
foo();
else
bar();
}

12
tests/issue0267.d Normal file
View File

@ -0,0 +1,12 @@
void main()
{
debug foo(); else bar();
debug (0) foo(); else bar();
// inlineElse reset check
debug foo();
if (true) foo(); else bar();
}

View File

@ -0,0 +1,18 @@
void main() {
debug foo();
else bar();
debug (0)
foo();
else
bar();
// inlineElse reset check
debug foo();
if (true)
foo();
else
bar();
}