From 394da5d02a1fea854b3d60551997d8be7b1140f7 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 22 Mar 2020 17:54:59 +0100 Subject: [PATCH 1/2] Fix break after comma, before this --- src/dfmt/formatter.d | 14 +++++++------- tests/allman/keep_line_breaks.d.ref | 4 ++++ tests/keep_line_breaks.d | 4 ++++ tests/otbs/keep_line_breaks.d.ref | 4 ++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index f462b031..e4802c11 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -1574,13 +1574,6 @@ private: newline(); regenLineBreakHints(index - 1); } - else if (!peekIs(tok!"}") && (linebreakHints.canFind(index) - || (linebreakHints.length == 0 && currentLineLength > config.max_line_length))) - { - pushWrapIndent(); - writeToken(); - newline(); - } else if (config.dfmt_keep_line_breaks == OptionalBoolean.t) { const commaLine = tokens[index].line; @@ -1599,6 +1592,13 @@ private: } } } + else if (!peekIs(tok!"}") && (linebreakHints.canFind(index) + || (linebreakHints.length == 0 && currentLineLength > config.max_line_length))) + { + pushWrapIndent(); + writeToken(); + newline(); + } else { writeToken(); diff --git a/tests/allman/keep_line_breaks.d.ref b/tests/allman/keep_line_breaks.d.ref index 5ce1e26c..e6b3e528 100644 --- a/tests/allman/keep_line_breaks.d.ref +++ b/tests/allman/keep_line_breaks.d.ref @@ -21,6 +21,10 @@ int[] func(int argument_1_1, int argument_1_2, .func(argument_2_1) .func(argument_2_2); + auto x = func(argument_1_1, argument_1_2, + this.argument_2_1, this.argument_2_2, + argument_3_1, argument_3_2); + return [ 3, 5, 5, 7, diff --git a/tests/keep_line_breaks.d b/tests/keep_line_breaks.d index 5ce1e26c..e6b3e528 100644 --- a/tests/keep_line_breaks.d +++ b/tests/keep_line_breaks.d @@ -21,6 +21,10 @@ int[] func(int argument_1_1, int argument_1_2, .func(argument_2_1) .func(argument_2_2); + auto x = func(argument_1_1, argument_1_2, + this.argument_2_1, this.argument_2_2, + argument_3_1, argument_3_2); + return [ 3, 5, 5, 7, diff --git a/tests/otbs/keep_line_breaks.d.ref b/tests/otbs/keep_line_breaks.d.ref index 9b694f36..fd596702 100644 --- a/tests/otbs/keep_line_breaks.d.ref +++ b/tests/otbs/keep_line_breaks.d.ref @@ -17,6 +17,10 @@ int[] func(int argument_1_1, int argument_1_2, .func(argument_2_1) .func(argument_2_2); + auto x = func(argument_1_1, argument_1_2, + this.argument_2_1, this.argument_2_2, + argument_3_1, argument_3_2); + return [ 3, 5, 5, 7, From ef83514541b893f9ae827c37b940cc14415fc094 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 24 Mar 2020 10:33:56 +0100 Subject: [PATCH 2/2] keep_line_breaks: Compare with the token end line --- src/dfmt/formatter.d | 18 +++++++++++++++--- tests/allman/keep_line_breaks.d.ref | 5 +++++ tests/keep_line_breaks.d | 5 +++++ tests/otbs/keep_line_breaks.d.ref | 5 +++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index e4802c11..4ec2e855 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -2151,10 +2151,22 @@ const pure @safe @nogc: bool onNextLine() @nogc nothrow pure @safe { import dfmt.editorconfig : OptionalBoolean; + import std.algorithm.searching : count; + import std.string : representation; - return config.dfmt_keep_line_breaks == OptionalBoolean.t - && index > 0 - && tokens[index - 1].line < tokens[index].line; + if (config.dfmt_keep_line_breaks == OptionalBoolean.f || index <= 0) + { + return false; + } + // To compare whether 2 tokens are on same line, we need the end line + // of the first token (tokens[index - 1]) and the start line of the + // second one (tokens[index]). If a token takes multiple lines (e.g. a + // multi-line string), we can sum the number of the newlines in the + // token and tokens[index - 1].line, the start line. + const previousTokenEndLineNo = tokens[index - 1].line + + tokens[index - 1].text.representation.count('\n'); + + return previousTokenEndLineNo < tokens[index].line; } /// Bugs: not unicode correct diff --git a/tests/allman/keep_line_breaks.d.ref b/tests/allman/keep_line_breaks.d.ref index e6b3e528..cd51650f 100644 --- a/tests/allman/keep_line_breaks.d.ref +++ b/tests/allman/keep_line_breaks.d.ref @@ -25,6 +25,11 @@ int[] func(int argument_1_1, int argument_1_2, this.argument_2_1, this.argument_2_2, argument_3_1, argument_3_2); + ` + + + `.format!"%s"; + return [ 3, 5, 5, 7, diff --git a/tests/keep_line_breaks.d b/tests/keep_line_breaks.d index e6b3e528..cd51650f 100644 --- a/tests/keep_line_breaks.d +++ b/tests/keep_line_breaks.d @@ -25,6 +25,11 @@ int[] func(int argument_1_1, int argument_1_2, this.argument_2_1, this.argument_2_2, argument_3_1, argument_3_2); + ` + + + `.format!"%s"; + return [ 3, 5, 5, 7, diff --git a/tests/otbs/keep_line_breaks.d.ref b/tests/otbs/keep_line_breaks.d.ref index fd596702..cd6babef 100644 --- a/tests/otbs/keep_line_breaks.d.ref +++ b/tests/otbs/keep_line_breaks.d.ref @@ -21,6 +21,11 @@ int[] func(int argument_1_1, int argument_1_2, this.argument_2_1, this.argument_2_2, argument_3_1, argument_3_2); + ` + + + `.format!"%s"; + return [ 3, 5, 5, 7,