Fix else indentation after array literal

This commit is contained in:
Eugen Wissner 2022-08-07 21:46:16 +02:00
parent 4a6078779a
commit e43f25909b
7 changed files with 271 additions and 5 deletions

View File

@ -203,7 +203,7 @@ private:
/// Tracks paren depth on a single line. This information can be used to
/// indent array literals inside parens, since arrays are indented only once
/// and paren indentation is ignored.line breaks and "[" reset the counter.
/// and paren indentation is ignored. Line breaks and "[" reset the counter.
int parenDepthOnLine;
string eolStringFromInput() const
@ -726,7 +726,6 @@ private:
{
indents.pop();
}
indents.popTempIndents();
}
parenDepthOnLine = 0;
}
@ -1703,8 +1702,6 @@ private:
import std.algorithm : max, canFind;
import dfmt.editorconfig : OptionalBoolean;
parenDepthOnLine = 0;
if (currentIs(tok!"comment") && index > 0 && current.line == tokenEndLine(tokens[index - 1]))
return;
@ -1822,7 +1819,19 @@ private:
if (indents.topIs(tok!"]"))
{
indents.pop();
indentLevel = indents.indentLevel;
}
// Find the initial indentation of constructs like "if" and
// "foreach" without removing them from the stack, since they
// still can be used later to indent "else".
auto savedIndents = IndentStack(config);
while (indents.length >= 0 && indents.topIsTemp) {
savedIndents.push(indents.top, indents.topDetails);
indents.pop;
}
indentLevel = indents.indentLevel;
while (savedIndents.length > 0) {
indents.push(savedIndents.top, savedIndents.topDetails);
savedIndents.pop;
}
}
else if (astInformation.attributeDeclarationLines.canFindIndex(current.line))
@ -1853,6 +1862,7 @@ private:
}
indent();
}
parenDepthOnLine = 0;
}
void write(string str)

View File

@ -278,6 +278,12 @@ private:
immutable currentIsNonWrapTemp = !details[i].wrap
&& details[i].temp && arr[i] != tok!")" && arr[i] != tok!"!";
if (currentIsNonWrapTemp && arr[i + 1] == tok!"]")
{
parenCount = pc;
continue;
}
if (arr[i] == tok!"static"
&& arr[i + 1].among!(tok!"if", tok!"else", tok!"foreach", tok!"foreach_reverse")
&& (i + 2 >= index || arr[i + 2] != tok!"{"))

View File

@ -0,0 +1,74 @@
private void selectMember(Args...)(Args args)
{
static if (true)
{
static if (args[0])
{
}
else
{
}
}
if (true)
{
if (args[0])
{
}
else
{
}
}
}
private void selectMember(Args...)(Args args)
{
static if (true)
{
static if ([
0,
1,
2,
3,
4,
])
{
}
else
{
}
}
if (true)
{
if (args[
0,
1,
2,
3,
4,
])
{
}
else
{
}
}
}
void f()
{
foreach (x; y)
if (foo)
{
}
else
{
}
if (a)
if (b)
{
}
else
{
}
}

View File

@ -0,0 +1 @@
--keep_line_breaks=true

74
tests/issue0494_else.d Normal file
View File

@ -0,0 +1,74 @@
private void selectMember(Args...)(Args args)
{
static if (true)
{
static if (args[0])
{
}
else
{
}
}
if (true)
{
if (args[0])
{
}
else
{
}
}
}
private void selectMember(Args...)(Args args)
{
static if (true)
{
static if ([
0,
1,
2,
3,
4,
])
{
}
else
{
}
}
if (true)
{
if (args[
0,
1,
2,
3,
4,
])
{
}
else
{
}
}
}
void f()
{
foreach (x; y)
if (foo)
{
}
else
{
}
if (a)
if (b)
{
}
else
{
}
}

View File

@ -0,0 +1,52 @@
private void selectMember(Args...)(Args args)
{
static if (true) {
static if (args[0]) {
} else {
}
}
if (true) {
if (args[0]) {
} else {
}
}
}
private void selectMember(Args...)(Args args)
{
static if (true) {
static if ([
0,
1,
2,
3,
4,
]) {
} else {
}
}
if (true) {
if (args[
0,
1,
2,
3,
4,
]) {
} else {
}
}
}
void f()
{
foreach (x; y)
if (foo) {
} else {
}
if (a)
if (b) {
} else {
}
}

View File

@ -0,0 +1,49 @@
private void selectMember(Args...)(Args args) {
static if (true) {
static if (args[0]) {
} else {
}
}
if (true) {
if (args[0]) {
} else {
}
}
}
private void selectMember(Args...)(Args args) {
static if (true) {
static if ([
0,
1,
2,
3,
4,
]) {
} else {
}
}
if (true) {
if (args[
0,
1,
2,
3,
4,
]) {
} else {
}
}
}
void f() {
foreach (x; y)
if (foo) {
} else {
}
if (a)
if (b) {
} else {
}
}