diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index e4802c1..4ec2e85 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 e6b3e52..cd51650 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 e6b3e52..cd51650 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 fd59670..cd6babe 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,