From bf560241541383ad2ff9c4fb3763566216495a22 Mon Sep 17 00:00:00 2001 From: Daniel Zuncke Date: Sat, 3 Feb 2024 22:50:51 +0100 Subject: [PATCH 1/4] Issue 586: Fix indentation for named arguments Indentation is wrong if named arguments come after newline. --- src/dfmt/formatter.d | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 0f7e821..0dfdbbb 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -1804,6 +1804,12 @@ private: assert(l2 != -1, "Recent '{' is not found despite being in struct initializer"); indentLevel = l2 + 1; } + else if (canFind(astInformation.namedArgumentColonLocations, tokens[nextNonComment(1)].index)) + { + immutable l2 = indents.indentToMostRecent(tok!"("); + assert(l2 != -1, "Recent '(' is not found despite being in named function argument"); + indentLevel = l2 + 1; + } else if ((config.dfmt_compact_labeled_statements == OptionalBoolean.f || !isBlockHeader(2) || peek2Is(tok!"if")) && !indents.topIs(tok!"]")) { @@ -2316,6 +2322,24 @@ const pure @safe @nogc: return previousTokenEndLineNo < tokens[index].line; } + /++ + + Get the index of the next token that isn't a comment starting from + + current index + n. + + If n is negative, searches backwards. + + If n = 0, returns index. + + Params: + + n = Offset to index where search begins. Negative values search backwards. + + Returns: + + Index of next token that isn't a comment or an index that is out of bounds. + +/ + size_t nextNonComment(int n = 1) + { + size_t i = index + n; + while (n != 0 && i < tokens.length && tokens[i].type == tok!"comment") + i = n > 0 ? i + 1 : i - 1; + return i; + } + /// Bugs: not unicode correct size_t tokenEndLine(const Token t) { From 0c7c86f031a5d29029a99a63ee05cdd4db3ab4c3 Mon Sep 17 00:00:00 2001 From: Daniel Zuncke Date: Sat, 3 Feb 2024 22:58:07 +0100 Subject: [PATCH 2/4] Issue 586: Update tests Add test against bad indentation if named args are after newline. --- tests/allman/issue0586.d.ref | 7 +++++++ tests/issue0586.d | 7 +++++++ tests/knr/issue0586.d.ref | 7 +++++++ tests/otbs/issue0586.d.ref | 6 ++++++ 4 files changed, 27 insertions(+) diff --git a/tests/allman/issue0586.d.ref b/tests/allman/issue0586.d.ref index cd86519..ccfe6f4 100644 --- a/tests/allman/issue0586.d.ref +++ b/tests/allman/issue0586.d.ref @@ -26,3 +26,10 @@ void main() temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void g() +{ + tmp(namedArg1: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc", + namedArg2: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc", + namedArg3: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc"); +} diff --git a/tests/issue0586.d b/tests/issue0586.d index db744f3..4004c86 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 g() +{ + tmp(namedArg1: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc", +namedArg2: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc", +namedArg3: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc"); +} diff --git a/tests/knr/issue0586.d.ref b/tests/knr/issue0586.d.ref index 76c61bc..6810926 100644 --- a/tests/knr/issue0586.d.ref +++ b/tests/knr/issue0586.d.ref @@ -25,3 +25,10 @@ void main() temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void g() +{ + tmp(namedArg1: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc", + namedArg2: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc", + namedArg3: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc"); +} diff --git a/tests/otbs/issue0586.d.ref b/tests/otbs/issue0586.d.ref index e62ff1a..2444e56 100644 --- a/tests/otbs/issue0586.d.ref +++ b/tests/otbs/issue0586.d.ref @@ -22,3 +22,9 @@ void main() { temp(v1: () { S s = S(i: 5); return s.i; }, v2: 1); } + +void g() { + tmp(namedArg1: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc", + namedArg2: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc", + namedArg3: "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc"); +} From f003239e138c5381da3e8b7077d015896c110e62 Mon Sep 17 00:00:00 2001 From: Daniel Zuncke Date: Sat, 3 Feb 2024 23:10:00 +0100 Subject: [PATCH 3/4] Issue 586: Add tests with keep_line_breaks true (Currently) only testing for the indentation problem after a newline. --- tests/allman/issue0586_keep_line_breaks.d.ref | 7 +++++++ tests/issue0586_keep_line_breaks.args | 1 + tests/issue0586_keep_line_breaks.d | 7 +++++++ tests/knr/issue0586_keep_line_breaks.d.ref | 7 +++++++ tests/otbs/issue0586_keep_line_breaks.d.ref | 6 ++++++ 5 files changed, 28 insertions(+) create mode 100644 tests/allman/issue0586_keep_line_breaks.d.ref create mode 100644 tests/issue0586_keep_line_breaks.args create mode 100644 tests/issue0586_keep_line_breaks.d create mode 100644 tests/knr/issue0586_keep_line_breaks.d.ref create mode 100644 tests/otbs/issue0586_keep_line_breaks.d.ref diff --git a/tests/allman/issue0586_keep_line_breaks.d.ref b/tests/allman/issue0586_keep_line_breaks.d.ref new file mode 100644 index 0000000..989a87e --- /dev/null +++ b/tests/allman/issue0586_keep_line_breaks.d.ref @@ -0,0 +1,7 @@ +void test() +{ + return Struct( + foo: field.foo, + bar: field.bar, + baz: field.baz); +} diff --git a/tests/issue0586_keep_line_breaks.args b/tests/issue0586_keep_line_breaks.args new file mode 100644 index 0000000..3e94d38 --- /dev/null +++ b/tests/issue0586_keep_line_breaks.args @@ -0,0 +1 @@ +--keep_line_breaks true diff --git a/tests/issue0586_keep_line_breaks.d b/tests/issue0586_keep_line_breaks.d new file mode 100644 index 0000000..a0da6b4 --- /dev/null +++ b/tests/issue0586_keep_line_breaks.d @@ -0,0 +1,7 @@ +void test() +{ + return Struct( +foo: field.foo, +bar: field.bar, +baz: field.baz); +} diff --git a/tests/knr/issue0586_keep_line_breaks.d.ref b/tests/knr/issue0586_keep_line_breaks.d.ref new file mode 100644 index 0000000..989a87e --- /dev/null +++ b/tests/knr/issue0586_keep_line_breaks.d.ref @@ -0,0 +1,7 @@ +void test() +{ + return Struct( + foo: field.foo, + bar: field.bar, + baz: field.baz); +} diff --git a/tests/otbs/issue0586_keep_line_breaks.d.ref b/tests/otbs/issue0586_keep_line_breaks.d.ref new file mode 100644 index 0000000..9e31fe3 --- /dev/null +++ b/tests/otbs/issue0586_keep_line_breaks.d.ref @@ -0,0 +1,6 @@ +void test() { + return Struct( + foo: field.foo, + bar: field.bar, + baz: field.baz); +} From 21b79d38afc407186a8f7653cfd3a8512bd3fdac Mon Sep 17 00:00:00 2001 From: Daniel Zuncke Date: Sun, 7 Jul 2024 02:14:07 +0200 Subject: [PATCH 4/4] Adjust return value on missing token --- src/dfmt/formatter.d | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 0dfdbbb..1530f29 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -2330,14 +2330,15 @@ const pure @safe @nogc: + Params: + n = Offset to index where search begins. Negative values search backwards. + Returns: - + Index of next token that isn't a comment or an index that is out of bounds. + + Index of next token that isn't a comment or `size_t.max` if no such + + token exists, +/ size_t nextNonComment(int n = 1) { size_t i = index + n; while (n != 0 && i < tokens.length && tokens[i].type == tok!"comment") i = n > 0 ? i + 1 : i - 1; - return i; + return i < tokens.length ? i : size_t.max; } /// Bugs: not unicode correct