Fix the issue #237
In formatLeftBrace remember if we indented because of a StructInitialzer; If we did we revert the intdent in formatRightBrace
This commit is contained in:
parent
dca63110f7
commit
e535f0b2b0
|
@ -8,10 +8,19 @@ module dfmt.ast_info;
|
||||||
import dparse.lexer;
|
import dparse.lexer;
|
||||||
import dparse.ast;
|
import dparse.ast;
|
||||||
|
|
||||||
|
enum BraceIndentInfoFlags
|
||||||
|
{
|
||||||
|
tempIndent = 1 << 0,
|
||||||
|
}
|
||||||
|
|
||||||
struct BraceIndentInfo
|
struct BraceIndentInfo
|
||||||
{
|
{
|
||||||
size_t startLocation;
|
size_t startLocation;
|
||||||
size_t endLocation;
|
size_t endLocation;
|
||||||
|
|
||||||
|
uint flags;
|
||||||
|
|
||||||
|
uint beginIndentLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// AST information that is needed by the formatter.
|
/// AST information that is needed by the formatter.
|
||||||
|
@ -39,6 +48,9 @@ struct ASTInformation
|
||||||
sort(constructorDestructorLocations);
|
sort(constructorDestructorLocations);
|
||||||
sort(staticConstructorDestructorLocations);
|
sort(staticConstructorDestructorLocations);
|
||||||
sort(sharedStaticConstructorDestructorLocations);
|
sort(sharedStaticConstructorDestructorLocations);
|
||||||
|
|
||||||
|
sort!((a,b) => a.endLocation < b.endLocation)
|
||||||
|
(indentInfoSortedByEndLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Locations of end braces for struct bodies
|
/// Locations of end braces for struct bodies
|
||||||
|
@ -92,7 +104,7 @@ struct ASTInformation
|
||||||
/// Locations of constructor/destructor "this" tokens ?
|
/// Locations of constructor/destructor "this" tokens ?
|
||||||
size_t[] constructorDestructorLocations;
|
size_t[] constructorDestructorLocations;
|
||||||
|
|
||||||
BraceIndentInfo[] sortedByStartLocation;
|
BraceIndentInfo[] indentInfoSortedByEndLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collects information from the AST that is useful for the formatter
|
/// Collects information from the AST that is useful for the formatter
|
||||||
|
@ -195,8 +207,12 @@ final class FormatVisitor : ASTVisitor
|
||||||
{
|
{
|
||||||
if (funcLit.functionBody !is null)
|
if (funcLit.functionBody !is null)
|
||||||
{
|
{
|
||||||
astInformation.funLitStartLocations ~= funcLit.functionBody.blockStatement.startLocation;
|
const bs = funcLit.functionBody.blockStatement;
|
||||||
astInformation.funLitEndLocations ~= funcLit.functionBody.blockStatement.endLocation;
|
|
||||||
|
astInformation.funLitStartLocations ~= bs.startLocation;
|
||||||
|
astInformation.funLitEndLocations ~= bs.endLocation;
|
||||||
|
astInformation.indentInfoSortedByEndLocation ~=
|
||||||
|
BraceIndentInfo(bs.startLocation, bs.endLocation);
|
||||||
}
|
}
|
||||||
funcLit.accept(this);
|
funcLit.accept(this);
|
||||||
}
|
}
|
||||||
|
@ -234,6 +250,9 @@ final class FormatVisitor : ASTVisitor
|
||||||
{
|
{
|
||||||
astInformation.structInitStartLocations ~= structInitializer.startLocation;
|
astInformation.structInitStartLocations ~= structInitializer.startLocation;
|
||||||
astInformation.structInitEndLocations ~= structInitializer.endLocation;
|
astInformation.structInitEndLocations ~= structInitializer.endLocation;
|
||||||
|
astInformation.indentInfoSortedByEndLocation ~=
|
||||||
|
BraceIndentInfo(structInitializer.startLocation, structInitializer.endLocation);
|
||||||
|
|
||||||
structInitializer.accept(this);
|
structInitializer.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -737,7 +737,9 @@ private:
|
||||||
{
|
{
|
||||||
import std.algorithm : map, sum, canFind;
|
import std.algorithm : map, sum, canFind;
|
||||||
|
|
||||||
if (astInformation.structInitStartLocations.canFindIndex(tokens[index].index))
|
auto tIndex = tokens[index].index;
|
||||||
|
|
||||||
|
if (astInformation.structInitStartLocations.canFindIndex(tIndex))
|
||||||
{
|
{
|
||||||
sBraceDepth++;
|
sBraceDepth++;
|
||||||
auto e = expressionEndIndex(index);
|
auto e = expressionEndIndex(index);
|
||||||
|
@ -746,13 +748,21 @@ private:
|
||||||
writeToken();
|
writeToken();
|
||||||
if (l > config.dfmt_soft_max_line_length)
|
if (l > config.dfmt_soft_max_line_length)
|
||||||
{
|
{
|
||||||
|
import std.algorithm.searching : find;
|
||||||
|
|
||||||
|
auto indentInfo = astInformation.indentInfoSortedByEndLocation
|
||||||
|
.find!((a,b) => a.startLocation == b)(tIndex);
|
||||||
|
assert(indentInfo.length > 0);
|
||||||
|
cast()indentInfo[0].flags |= BraceIndentInfoFlags.tempIndent;
|
||||||
|
cast()indentInfo[0].beginIndentLevel = indents.indentLevel;
|
||||||
|
|
||||||
indents.push(tok!"{");
|
indents.push(tok!"{");
|
||||||
newline();
|
newline();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
niBraceDepth++;
|
niBraceDepth++;
|
||||||
}
|
}
|
||||||
else if (astInformation.funLitStartLocations.canFindIndex(tokens[index].index))
|
else if (astInformation.funLitStartLocations.canFindIndex(tIndex))
|
||||||
{
|
{
|
||||||
sBraceDepth++;
|
sBraceDepth++;
|
||||||
if (peekBackIs(tok!")"))
|
if (peekBackIs(tok!")"))
|
||||||
|
@ -808,15 +818,34 @@ private:
|
||||||
|
|
||||||
void formatRightBrace()
|
void formatRightBrace()
|
||||||
{
|
{
|
||||||
if (astInformation.structInitEndLocations.canFindIndex(tokens[index].index))
|
void popToBeginIndent(BraceIndentInfo indentInfo)
|
||||||
|
{
|
||||||
|
foreach(i; indentInfo.beginIndentLevel .. indents.indentLevel)
|
||||||
|
{
|
||||||
|
indents.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
indentLevel = indentInfo.beginIndentLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t pos;
|
||||||
|
if (astInformation.structInitEndLocations.canFindIndex(tokens[index].index, &pos))
|
||||||
{
|
{
|
||||||
if (sBraceDepth > 0)
|
if (sBraceDepth > 0)
|
||||||
sBraceDepth--;
|
sBraceDepth--;
|
||||||
if (niBraceDepth > 0)
|
if (niBraceDepth > 0)
|
||||||
niBraceDepth--;
|
niBraceDepth--;
|
||||||
|
|
||||||
|
auto indentInfo = astInformation.indentInfoSortedByEndLocation[pos];
|
||||||
|
if (indentInfo.flags & BraceIndentInfoFlags.tempIndent)
|
||||||
|
{
|
||||||
|
popToBeginIndent(indentInfo);
|
||||||
|
simpleNewline();
|
||||||
|
indent();
|
||||||
|
}
|
||||||
writeToken();
|
writeToken();
|
||||||
}
|
}
|
||||||
else if (astInformation.funLitEndLocations.canFindIndex(tokens[index].index))
|
else if (astInformation.funLitEndLocations.canFindIndex(tokens[index].index, &pos))
|
||||||
{
|
{
|
||||||
if (niBraceDepth > 0)
|
if (niBraceDepth > 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue