Skip to content

Commit

Permalink
Merge pull request #473 from belka-ew/break-on-this
Browse files Browse the repository at this point in the history
keep_line_breaks: multi-line tokens and argument list break
  • Loading branch information
WebFreak001 authored Mar 24, 2020
2 parents 66faac4 + ef83514 commit 1964f80
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;

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 config.dfmt_keep_line_breaks == OptionalBoolean.t
&& index > 0
&& tokens[index - 1].line < tokens[index].line;
return previousTokenEndLineNo < tokens[index].line;
}

/// Bugs: not unicode correct
Expand Down
9 changes: 9 additions & 0 deletions tests/allman/keep_line_breaks.d.ref
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ 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);

`
<html>
</html>
`.format!"%s";

return [
3, 5,
5, 7,
Expand Down
9 changes: 9 additions & 0 deletions tests/keep_line_breaks.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ 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);

`
<html>
</html>
`.format!"%s";

return [
3, 5,
5, 7,
Expand Down
9 changes: 9 additions & 0 deletions tests/otbs/keep_line_breaks.d.ref
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ 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);

`
<html>
</html>
`.format!"%s";

return [
3, 5,
5, 7,
Expand Down

0 comments on commit 1964f80

Please sign in to comment.