diff --git a/src/dfmt/ast_info.d b/src/dfmt/ast_info.d index 9c3f68f..83082d1 100644 --- a/src/dfmt/ast_info.d +++ b/src/dfmt/ast_info.d @@ -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); diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index ee36f40..c8d63a6 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -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")) { diff --git a/tests/allman/issue0256.d.ref b/tests/allman/issue0256.d.ref new file mode 100644 index 0000000..5776aec --- /dev/null +++ b/tests/allman/issue0256.d.ref @@ -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 + }; +} diff --git a/tests/issue0256.d b/tests/issue0256.d new file mode 100644 index 0000000..18f4c98 --- /dev/null +++ b/tests/issue0256.d @@ -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 + }; +} diff --git a/tests/otbs/issue0256.d.ref b/tests/otbs/issue0256.d.ref new file mode 100644 index 0000000..ca215a4 --- /dev/null +++ b/tests/otbs/issue0256.d.ref @@ -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 + }; +}