diff --git a/src/dfmt.d b/src/dfmt.d index be2b98e..6b4ab01 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -594,52 +594,69 @@ private: if (current.type == tok!"{") { depth++; - if (config.braceStyle == BraceStyle.otbs) + if (assumeSorted(astInformation.structInitStartLocations) + .equalRange(tokens[index].index).length) { - write(" "); - write("{"); + writeToken(); } else { + if (config.braceStyle == BraceStyle.otbs) + { + write(" "); + write("{"); + } + else + { + newline(); + write("{"); + } + indentLevel++; + index++; newline(); - write("{"); } - indentLevel++; - index++; - newline(); } else if (current.type == tok!"}") { - // Silly hack to format enums better. - if (peekBackIs(tok!"identifier")) - newline(); - write("}"); - depth--; - if (index < tokens.length - 1 && - assumeSorted(astInformation.doubleNewlineLocations) - .equalRange(tokens[index].index).length && !peekIs(tok!"}")) + if (assumeSorted(astInformation.structInitEndLocations) + .equalRange(tokens[index].index).length) { - output.put("\n"); + writeToken(); + depth--; } - if (config.braceStyle == BraceStyle.otbs) + else { - index++; - if (index < tokens.length && current.type == tok!"else") - write(" "); + // Silly hack to format enums better. + if (peekBackIs(tok!"identifier")) + newline(); + write("}"); + depth--; + if (index < tokens.length - 1 && + assumeSorted(astInformation.doubleNewlineLocations) + .equalRange(tokens[index].index).length && !peekIs(tok!"}")) + { + output.put("\n"); + } + if (config.braceStyle == BraceStyle.otbs) + { + index++; + if (index < tokens.length && current.type == tok!"else") + write(" "); + else + { + if (peekIs(tok!"case") || peekIs(tok!"default")) + indentLevel--; + newline(); + } + } else { + index++; if (peekIs(tok!"case") || peekIs(tok!"default")) indentLevel--; newline(); } } - else - { - index++; - if (peekIs(tok!"case") || peekIs(tok!"default")) - indentLevel--; - newline(); - } } else formatStep(); @@ -950,6 +967,8 @@ struct ASTInformation sort(unaryLocations); sort(attributeDeclarationLines); sort(caseEndLocations); + sort(structInitStartLocations); + sort(structInitEndLocations); } /// Locations of end braces for struct bodies @@ -966,6 +985,12 @@ struct ASTInformation /// Case statement colon locations size_t[] caseEndLocations; + + /// Opening braces of struct initializers + size_t[] structInitStartLocations; + + /// Closing braces of struct initializers + size_t[] structInitEndLocations; } /// Collects information from the AST that is useful for the formatter @@ -1004,6 +1029,13 @@ final class FormatVisitor : ASTVisitor functionBody.accept(this); } + override void visit(const StructInitializer structInitializer) + { + astInformation.structInitStartLocations ~= structInitializer.startLocation; + astInformation.structInitEndLocations ~= structInitializer.endLocation; + structInitializer.accept(this); + } + override void visit(const EnumBody enumBody) { astInformation.doubleNewlineLocations ~= enumBody.endLocation; diff --git a/tests/issue0017.d b/tests/issue0017.d new file mode 100644 index 0000000..907614f --- /dev/null +++ b/tests/issue0017.d @@ -0,0 +1,5 @@ +immutable NameId[] namesA = +[ + {"Aacgr", 0x00386}, // GREEK CAPITAL LETTER ALPHA WITH TONOS + {"aacgr", 0x003AC}, // GREEK SMALL LETTER ALPHA WITH TONOS +]; diff --git a/tests/issue0017.d.ref b/tests/issue0017.d.ref new file mode 100644 index 0000000..4c5d68f --- /dev/null +++ b/tests/issue0017.d.ref @@ -0,0 +1,3 @@ +immutable NameId[] namesA = [{"Aacgr", 0x00386}, // GREEK CAPITAL LETTER ALPHA WITH TONOS +{"aacgr", 0x003AC}, // GREEK SMALL LETTER ALPHA WITH TONOS +];