From 9842daff3ab83ca8ab2206762299c0316bdf89e7 Mon Sep 17 00:00:00 2001 From: Mathis Beer Date: Thu, 25 Sep 2025 11:14:32 +0200 Subject: [PATCH] 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); +}