This commit is contained in:
Hackerpilot 2016-01-22 04:48:29 -08:00
parent ee8fcd41f4
commit 98f443d3f1
5 changed files with 244 additions and 9 deletions

View File

@ -656,6 +656,11 @@ private:
linebreakHints = []; linebreakHints = [];
while (indents.topIs(tok!"enum")) while (indents.topIs(tok!"enum"))
indents.pop(); indents.pop();
if (indents.topAre(tok!"static", tok!"else"))
{
indents.pop();
indents.pop();
}
if (config.dfmt_brace_style == BraceStyle.allman) if (config.dfmt_brace_style == BraceStyle.allman)
{ {
if (!currentIs(tok!"{")) if (!currentIs(tok!"{"))
@ -726,7 +731,7 @@ private:
} }
else else
{ {
if (indents.length && isTempIndent(indents.top)) if (indents.topIsTemp && indents.indentToMostRecent(tok!"static") == -1)
indentLevel = indents.indentLevel - 1; indentLevel = indents.indentLevel - 1;
else else
indentLevel = indents.indentLevel; indentLevel = indents.indentLevel;
@ -777,13 +782,14 @@ private:
if (index + 1 < tokens.length if (index + 1 < tokens.length
&& astInformation.doubleNewlineLocations.canFindIndex( && astInformation.doubleNewlineLocations.canFindIndex(
tokens[index].index) && !peekIs(tok!"}") tokens[index].index) && !peekIs(tok!"}")
&& !peekIs(tok!";") && !peekIs(tok!"comment", false)) && !peekIs(tok!"else") && !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 && peekIs(tok!"else")
&& !indents.topAre(tok!"static", tok!"if"))
{ {
write(" "); write(" ");
index++; index++;
@ -794,6 +800,8 @@ private:
&& !peekIs(tok!";") && !peekIs(tok!"{")) && !peekIs(tok!";") && !peekIs(tok!"{"))
{ {
index++; index++;
if (indents.topIs(tok!"static"))
indents.pop();
newline(); newline();
} }
else else
@ -825,7 +833,16 @@ private:
if (currentIs(tok!"out") && !peekBackIs(tok!"}")) if (currentIs(tok!"out") && !peekBackIs(tok!"}"))
newline(); newline();
if (shouldPushIndent) if (shouldPushIndent)
{
if (peekBackIs(tok!"static"))
{
if (indents.topIs(tok!"else"))
indents.pop();
if (!indents.topIs(tok!"static"))
indents.push(tok!"static");
}
indents.push(current.type); indents.push(current.type);
}
writeToken(); writeToken();
if (currentIs(tok!"(")) if (currentIs(tok!"("))
{ {
@ -844,6 +861,14 @@ private:
else if (!currentIs(tok!"{") && !currentIs(tok!";") else if (!currentIs(tok!"{") && !currentIs(tok!";")
&& !currentIs(tok!"in") && !currentIs(tok!"out") && !currentIs(tok!"body")) && !currentIs(tok!"in") && !currentIs(tok!"out") && !currentIs(tok!"body"))
newline(); newline();
else if (currentIs(tok!"{") && indents.topAre(tok!"static", tok!"if"))
{
// Hacks to format braced vs non-braced static if declarations.
indents.pop();
indents.pop();
indents.push(tok!"if");
formatLeftBrace();
}
} }
void formatElse() void formatElse()
@ -863,7 +888,7 @@ private:
} }
else if (!currentIs(tok!"{") && !currentIs(tok!"comment")) else if (!currentIs(tok!"{") && !currentIs(tok!"comment"))
{ {
if (indents.topIs(tok!"if") || indents.topIs(tok!"version")) if (indents.topIsOneOf(tok!"if", tok!"version"))
indents.pop(); indents.pop();
indents.push(tok!"else"); indents.push(tok!"else");
newline(); newline();
@ -1249,8 +1274,8 @@ private:
{ {
if (currentIs(tok!"else")) if (currentIs(tok!"else"))
{ {
auto i = indents.indentToMostRecent(tok!"if"); immutable i = indents.indentToMostRecent(tok!"if");
auto v = indents.indentToMostRecent(tok!"version"); immutable v = indents.indentToMostRecent(tok!"version");
immutable mostRecent = max(i, v); immutable mostRecent = max(i, v);
if (mostRecent != -1) if (mostRecent != -1)
indentLevel = mostRecent; indentLevel = mostRecent;
@ -1302,7 +1327,7 @@ private:
else if (currentIs(tok!"}")) else if (currentIs(tok!"}"))
{ {
indents.popTempIndents(); indents.popTempIndents();
while (indents.topIs(tok!"case") || indents.topIs(tok!"@")) while (indents.topIsOneOf(tok!"case", tok!"@", tok!"static"))
indents.pop(); indents.pop();
if (indents.topIs(tok!"{")) if (indents.topIs(tok!"{"))
{ {
@ -1310,8 +1335,8 @@ private:
indents.pop(); indents.pop();
} }
while (sBraceDepth == 0 && indents.topIsTemp() while (sBraceDepth == 0 && indents.topIsTemp()
&& ((indents.top != tok!"if" && ((!indents.topIsOneOf(tok!"else", tok!"if", tok!"static", tok!"version"))
&& indents.top != tok!"version") || !peekIs(tok!"else"))) || !peekIs(tok!"else")))
{ {
indents.pop(); indents.pop();
} }

View File

@ -97,6 +97,14 @@ struct IndentStack
index--; index--;
} }
bool topAre(IdType[] types...)
{
if (types.length > index)
return false;
return arr[index - types.length .. index] == types;
}
/** /**
* Returns: `true` if the top of the indent stack is the given indent type. * Returns: `true` if the top of the indent stack is the given indent type.
*/ */
@ -188,6 +196,12 @@ 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")
&& (i + 2 >= index || arr[i + 2] != tok!"{"))
{
parenCount = pc;
continue;
}
if (currentIsNonWrapTemp && (arr[i + 1] == tok!"switch" if (currentIsNonWrapTemp && (arr[i + 1] == tok!"switch"
|| arr[i + 1] == tok!"{" || arr[i + 1] == tok!")")) || arr[i + 1] == tok!"{" || arr[i + 1] == tok!")"))
{ {

View File

@ -0,0 +1,68 @@
static if (someCondition)
void doStuff()
{
}
else
void doStuff()
{
}
static if (someCondition)
void doStuff()
{
}
else static if (otherCondition)
void doStuff()
{
}
static if (someCondition)
void doStuff()
{
}
else static if (otherCondition)
void doStuff()
{
}
else
void doStuff()
{
}
static if (condition)
int a;
else
int b;
static if (condition)
int a;
else static if (otherCondition)
int c;
else
int b;
void doStuff();
static if (stuff)
int a;
else
class C
{
public:
void aFunction();
private:
int a;
int b;
}
static if (condition)
int a;
else
int b;
static if (condition)
int a;
else static if (otherCondition)
int c;
else
int b;

68
tests/issue0220.d Normal file
View File

@ -0,0 +1,68 @@
static if (someCondition)
void doStuff()
{
}
else
void doStuff()
{
}
static if (someCondition)
void doStuff()
{
}
else static if (otherCondition)
void doStuff()
{
}
static if (someCondition)
void doStuff()
{
}
else static if (otherCondition)
void doStuff()
{
}
else
void doStuff()
{
}
static if (condition)
int a;
else
int b;
static if (condition)
int a;
else static if (otherCondition)
int c;
else
int b;
void doStuff();
static if (stuff)
int a;
else
class C
{
public:
void aFunction();
private:
int a;
int b;
}
static if (condition)
int a;
else
int b;
static if (condition)
int a;
else static if (otherCondition)
int c;
else
int b;

View File

@ -0,0 +1,60 @@
static if (someCondition)
void doStuff() {
}
else
void doStuff() {
}
static if (someCondition)
void doStuff() {
}
else static if (otherCondition)
void doStuff() {
}
static if (someCondition)
void doStuff() {
}
else static if (otherCondition)
void doStuff() {
}
else
void doStuff() {
}
static if (condition)
int a;
else
int b;
static if (condition)
int a;
else static if (otherCondition)
int c;
else
int b;
void doStuff();
static if (stuff)
int a;
else
class C {
public:
void aFunction();
private:
int a;
int b;
}
static if (condition)
int a;
else
int b;
static if (condition)
int a;
else static if (otherCondition)
int c;
else
int b;