Merge pull request #446 from Sobaya007/fix-issue256
Fix: Issue 256 "named member struct initialisers" merged-on-behalf-of: Jan Jurzitza <gh@webfreak.org>
This commit is contained in:
commit
3f141b272d
|
|
@ -23,6 +23,12 @@ struct BraceIndentInfo
|
|||
uint beginIndentLevel;
|
||||
}
|
||||
|
||||
struct StructInitializerInfo
|
||||
{
|
||||
size_t startLocation;
|
||||
size_t endLocation;
|
||||
}
|
||||
|
||||
/// AST information that is needed by the formatter.
|
||||
struct ASTInformation
|
||||
{
|
||||
|
|
@ -53,6 +59,8 @@ struct ASTInformation
|
|||
sort(sharedStaticConstructorDestructorLocations);
|
||||
sort!((a,b) => a.endLocation < b.endLocation)
|
||||
(indentInfoSortedByEndLocation);
|
||||
sort!((a,b) => a.endLocation < b.endLocation)
|
||||
(structInfoSortedByEndLocation);
|
||||
sort(ufcsHintLocations);
|
||||
ufcsHintLocations = ufcsHintLocations.uniq().array();
|
||||
}
|
||||
|
|
@ -118,6 +126,9 @@ struct ASTInformation
|
|||
size_t[] ufcsHintLocations;
|
||||
|
||||
BraceIndentInfo[] indentInfoSortedByEndLocation;
|
||||
|
||||
/// Opening & closing braces of struct initializers
|
||||
StructInitializerInfo[] structInfoSortedByEndLocation;
|
||||
}
|
||||
|
||||
/// Collects information from the AST that is useful for the formatter
|
||||
|
|
@ -272,6 +283,8 @@ final class FormatVisitor : ASTVisitor
|
|||
{
|
||||
astInformation.structInitStartLocations ~= structInitializer.startLocation;
|
||||
astInformation.structInitEndLocations ~= structInitializer.endLocation;
|
||||
astInformation.structInfoSortedByEndLocation ~=
|
||||
StructInitializerInfo(structInitializer.startLocation, structInitializer.endLocation);
|
||||
astInformation.indentInfoSortedByEndLocation ~=
|
||||
BraceIndentInfo(structInitializer.startLocation, structInitializer.endLocation);
|
||||
|
||||
|
|
|
|||
|
|
@ -725,10 +725,14 @@ private:
|
|||
void formatColon()
|
||||
{
|
||||
import dfmt.editorconfig : OptionalBoolean;
|
||||
import std.algorithm : canFind, any;
|
||||
|
||||
immutable bool isCase = astInformation.caseEndLocations.canFindIndex(current.index);
|
||||
immutable bool isAttribute = astInformation.attributeDeclarationLines.canFindIndex(
|
||||
current.line);
|
||||
immutable bool isStructInitializer = astInformation.structInfoSortedByEndLocation
|
||||
.canFind!(st => st.startLocation < current.index && current.index < st.endLocation);
|
||||
|
||||
if (isCase || isAttribute)
|
||||
{
|
||||
writeToken();
|
||||
|
|
@ -743,12 +747,15 @@ private:
|
|||
newline();
|
||||
}
|
||||
}
|
||||
else if (peekBackIs(tok!"identifier") && (peekBack2Is(tok!"{", true)
|
||||
|| peekBack2Is(tok!"}", true) || peekBack2Is(tok!";", true)
|
||||
|| peekBack2Is(tok!":", true)) && !(isBlockHeader(1) && !peekIs(tok!"if")))
|
||||
else if (peekBackIs(tok!"identifier")
|
||||
&& [tok!"{", tok!"}", tok!";", tok!":", tok!","]
|
||||
.any!((ptrdiff_t token) => peekBack2Is(cast(IdType)token, true))
|
||||
&& (!isBlockHeader(1) || peekIs(tok!"if")))
|
||||
{
|
||||
writeToken();
|
||||
if (!currentIs(tok!"{"))
|
||||
if (isStructInitializer)
|
||||
write(" ");
|
||||
else if (!currentIs(tok!"{"))
|
||||
newline();
|
||||
}
|
||||
else
|
||||
|
|
@ -1508,7 +1515,7 @@ private:
|
|||
void newline()
|
||||
{
|
||||
import std.range : assumeSorted;
|
||||
import std.algorithm : max;
|
||||
import std.algorithm : max, canFind;
|
||||
import dfmt.editorconfig : OptionalBoolean;
|
||||
|
||||
if (currentIs(tok!"comment") && index > 0 && current.line == tokenEndLine(tokens[index - 1]))
|
||||
|
|
@ -1551,6 +1558,12 @@ private:
|
|||
immutable l = indents.indentToMostRecent(tok!"switch");
|
||||
if (l != -1 && config.dfmt_align_switch_statements == OptionalBoolean.t)
|
||||
indentLevel = l;
|
||||
else if (astInformation.structInfoSortedByEndLocation
|
||||
.canFind!(st => st.startLocation < current.index && current.index < st.endLocation)) {
|
||||
immutable l2 = indents.indentToMostRecent(tok!"{");
|
||||
assert(l2 != -1, "Recent '{' is not found despite being in struct initializer");
|
||||
indentLevel = l2 + 1;
|
||||
}
|
||||
else if (config.dfmt_compact_labeled_statements == OptionalBoolean.f
|
||||
|| !isBlockHeader(2) || peek2Is(tok!"if"))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
void main()
|
||||
{
|
||||
S s1 = {a: 3};
|
||||
S s2 = {a: 3, b: "test string"};
|
||||
S s3 = {a: 3, b: "test string", c: {x: 3.14, y: 3 + 4}};
|
||||
T t = {
|
||||
someStructMember1: 2, someStructMember2: 42, someStructMember3: null, // foobar
|
||||
someOtherMember1: objA, someOtherMember2: objB, someOtherMember3: 0,
|
||||
somethingMore: null, someFlagInThisStruct: -1
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
void main() {
|
||||
S s1 = { a: 3 };
|
||||
S s2 = { a: 3, b:"test string" };
|
||||
S s3 = { a: 3, b:"test string", c: {x: 3.14, y: 3 + 4} };
|
||||
T t = {
|
||||
someStructMember1: 2,
|
||||
someStructMember2: 42,
|
||||
someStructMember3: null, // foobar
|
||||
someOtherMember1: objA,
|
||||
someOtherMember2: objB,
|
||||
someOtherMember3: 0,
|
||||
somethingMore: null,
|
||||
someFlagInThisStruct: -1
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
void main() {
|
||||
S s1 = {a: 3};
|
||||
S s2 = {a: 3, b: "test string"};
|
||||
S s3 = {a: 3, b: "test string", c: {x: 3.14, y: 3 + 4}};
|
||||
T t = {
|
||||
someStructMember1: 2, someStructMember2: 42, someStructMember3: null, // foobar
|
||||
someOtherMember1: objA, someOtherMember2: objB, someOtherMember3: 0,
|
||||
somethingMore: null, someFlagInThisStruct: -1
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue