From ac61efe7670acfa06cee2edaf61d4f7c368e4ce7 Mon Sep 17 00:00:00 2001 From: sobaya Date: Tue, 30 Jul 2019 13:02:48 +0900 Subject: [PATCH] Fix: Issue 256 "named member struct initialisers" --- src/dfmt/ast_info.d | 13 +++++++++++++ src/dfmt/formatter.d | 8 +++++++- tests/allman/issue0256.d.ref | 4 ++++ tests/issue0256.d | 4 ++++ tests/otbs/issue0256.d.ref | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/allman/issue0256.d.ref create mode 100644 tests/issue0256.d create mode 100644 tests/otbs/issue0256.d.ref 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..56fc27a 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; 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(); @@ -748,7 +752,9 @@ private: || peekBack2Is(tok!":", true)) && !(isBlockHeader(1) && !peekIs(tok!"if"))) { writeToken(); - if (!currentIs(tok!"{")) + if (isStructInitializer) + write(" "); + else if (!currentIs(tok!"{")) newline(); } else diff --git a/tests/allman/issue0256.d.ref b/tests/allman/issue0256.d.ref new file mode 100644 index 0000000..41c1237 --- /dev/null +++ b/tests/allman/issue0256.d.ref @@ -0,0 +1,4 @@ +void foo() +{ + S s = {a: 3}; +} diff --git a/tests/issue0256.d b/tests/issue0256.d new file mode 100644 index 0000000..59d78e4 --- /dev/null +++ b/tests/issue0256.d @@ -0,0 +1,4 @@ +void foo() +{ + S s = { a: 3 }; +} diff --git a/tests/otbs/issue0256.d.ref b/tests/otbs/issue0256.d.ref new file mode 100644 index 0000000..e5ab49c --- /dev/null +++ b/tests/otbs/issue0256.d.ref @@ -0,0 +1,3 @@ +void foo() { + S s = {a: 3}; +}