From 9842daff3ab83ca8ab2206762299c0316bdf89e7 Mon Sep 17 00:00:00 2001 From: Mathis Beer Date: Thu, 25 Sep 2025 11:14:32 +0200 Subject: [PATCH 1/2] Avoid interpreting named args as labels when indenting newline in keep_line_breaks mode. --- src/dfmt/formatter.d | 4 +++- tests/allman/issue0586.d.ref | 5 +++++ tests/issue0586.d | 7 +++++++ tests/knr/issue0586.d.ref | 5 +++++ tests/otbs/issue0586.d.ref | 4 ++++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 0f7e821..2545edb 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -1783,6 +1783,8 @@ private: if (hasCurrent) { + const isNamedArg = index + 1 < tokens.length + && astInformation.namedArgumentColonLocations.canFindIndex(tokens[index + 1].index); if (currentIs(tok!"else")) { immutable i = indents.indentToMostRecent(tok!"if"); @@ -1791,7 +1793,7 @@ private: if (mostRecent != -1) indentLevel = mostRecent; } - else if (currentIs(tok!"identifier") && peekIs(tok!":")) + else if (currentIs(tok!"identifier") && peekIs(tok!":") && !isNamedArg) { if (peekBackIs(tok!"}", true) || peekBackIs(tok!";", true)) indents.popTempIndents(); diff --git a/tests/allman/issue0586.d.ref b/tests/allman/issue0586.d.ref index cd86519..d2d7847 100644 --- a/tests/allman/issue0586.d.ref +++ b/tests/allman/issue0586.d.ref @@ -26,3 +26,8 @@ void main() temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void test() +{ + return Struct(foo: field.foo, bar: field.bar, baz: field.baz); +} diff --git a/tests/issue0586.d b/tests/issue0586.d index db744f3..c786255 100644 --- a/tests/issue0586.d +++ b/tests/issue0586.d @@ -29,3 +29,10 @@ void main() temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void test() { + return Struct( + foo: field.foo, + bar: field.bar, + baz: field.baz); +} diff --git a/tests/knr/issue0586.d.ref b/tests/knr/issue0586.d.ref index 76c61bc..8b0b9f0 100644 --- a/tests/knr/issue0586.d.ref +++ b/tests/knr/issue0586.d.ref @@ -25,3 +25,8 @@ void main() temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void test() +{ + return Struct(foo: field.foo, bar: field.bar, baz: field.baz); +} diff --git a/tests/otbs/issue0586.d.ref b/tests/otbs/issue0586.d.ref index e62ff1a..802f62c 100644 --- a/tests/otbs/issue0586.d.ref +++ b/tests/otbs/issue0586.d.ref @@ -22,3 +22,7 @@ void main() { temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void test() { + return Struct(foo: field.foo, bar: field.bar, baz: field.baz); +} From c4ea14c87927f4d65d114154fac31027531d868c Mon Sep 17 00:00:00 2001 From: Mathis Beer Date: Thu, 25 Sep 2025 17:34:54 +0200 Subject: [PATCH 2/2] Track 'new' expressions in the named argument offset list as well. --- src/dfmt/ast_info.d | 21 +++++++++++++++------ tests/allman/issue0586.d.ref | 1 + tests/issue0586.d | 4 ++++ tests/knr/issue0586.d.ref | 1 + tests/otbs/issue0586.d.ref | 1 + 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/dfmt/ast_info.d b/src/dfmt/ast_info.d index 34a8ede..7101471 100644 --- a/src/dfmt/ast_info.d +++ b/src/dfmt/ast_info.d @@ -454,14 +454,25 @@ final class FormatVisitor : ASTVisitor override void visit(const FunctionCallExpression functionCall) { - // Check if function has any arguments. - if (functionCall.arguments.namedArgumentList is null) + visit(functionCall.arguments); + functionCall.accept(this); + } + + override void visit(const NewExpression newCall) + { + visit(newCall.arguments); + newCall.accept(this); + } + + private void visit(const Arguments arguments) + { + // Check if call has any arguments. + if (!arguments || arguments.namedArgumentList is null) { - functionCall.accept(this); return; } - foreach (item; functionCall.arguments.namedArgumentList.items) + foreach (item; arguments.namedArgumentList.items) { // Do nothing if not a named argument. if (item.name == tok!"") @@ -479,8 +490,6 @@ final class FormatVisitor : ASTVisitor } } } - - functionCall.accept(this); } private: diff --git a/tests/allman/issue0586.d.ref b/tests/allman/issue0586.d.ref index d2d7847..1a33a3d 100644 --- a/tests/allman/issue0586.d.ref +++ b/tests/allman/issue0586.d.ref @@ -30,4 +30,5 @@ void main() void test() { return Struct(foo: field.foo, bar: field.bar, baz: field.baz); + return new Class(foo: field.foo, bar: field.bar, baz: field.baz); } diff --git a/tests/issue0586.d b/tests/issue0586.d index c786255..3d8d32a 100644 --- a/tests/issue0586.d +++ b/tests/issue0586.d @@ -35,4 +35,8 @@ void test() { foo: field.foo, bar: field.bar, baz: field.baz); + return new Class( + foo: field.foo, + bar: field.bar, + baz: field.baz); } diff --git a/tests/knr/issue0586.d.ref b/tests/knr/issue0586.d.ref index 8b0b9f0..2a6a6a3 100644 --- a/tests/knr/issue0586.d.ref +++ b/tests/knr/issue0586.d.ref @@ -29,4 +29,5 @@ void main() void test() { return Struct(foo: field.foo, bar: field.bar, baz: field.baz); + return new Class(foo: field.foo, bar: field.bar, baz: field.baz); } diff --git a/tests/otbs/issue0586.d.ref b/tests/otbs/issue0586.d.ref index 802f62c..d6ab363 100644 --- a/tests/otbs/issue0586.d.ref +++ b/tests/otbs/issue0586.d.ref @@ -25,4 +25,5 @@ void main() { void test() { return Struct(foo: field.foo, bar: field.bar, baz: field.baz); + return new Class(foo: field.foo, bar: field.bar, baz: field.baz); }