Skip to content

Commit 165d2c6

Browse files
committed
Break new line ties by order.
1 parent d0a56c8 commit 165d2c6

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

rascal-lsp/src/main/rascal/library/util/Format.rsc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Map;
55
import Set;
66
import String;
77

8-
set[str] newLineCharacters = {
8+
list[str] newLineCharacters = [
99
"\u000A", // LF
1010
"\u000B", // VT
1111
"\u000C", // FF
@@ -14,11 +14,16 @@ set[str] newLineCharacters = {
1414
"\u0085", // NEL
1515
"\u2028", // LS
1616
"\u2029" // PS
17-
};
17+
];
1818

1919
private bool bySize(str a, str b) = size(a) < size(b);
20+
private bool(str, str) byIndex(list[str] indices) {
21+
return bool(str a, str b) {
22+
return indexOf(indices, a) < indexOf(indices, b);
23+
};
24+
}
2025
21-
str mostUsedNewline(str input, set[str] lineseps = newLineCharacters, str(set[str]) tieBreaker = getFirstFrom) {
26+
str mostUsedNewline(str input, list[str] lineseps = newLineCharacters, str(list[str]) tieBreaker = getFirstFrom) {
2227
linesepCounts = (nl: 0 | nl <- lineseps);
2328
for (nl <- sort(lineseps, bySize)) {
2429
int count = size(findAll(input, nl));
@@ -30,7 +35,7 @@ str mostUsedNewline(str input, set[str] lineseps = newLineCharacters, str(set[st
3035
}
3136
3237
byCount = invert(linesepCounts);
33-
return tieBreaker(byCount[max(domain(byCount))]);
38+
return tieBreaker(sort(byCount[max(domain(byCount))], byIndex(lineseps)));
3439
}
3540
3641
tuple[str indentation, str rest] splitIndentation(/^<indentation:\s*><rest:.*>/)
@@ -64,7 +69,7 @@ test bool mostUsedNewlineTestTie()
6469
test bool mostUsedNewlineTestGreedy()
6570
= mostUsedNewline("\r\n\r\n\n") == "\r\n";
6671
67-
str insertFinalNewline(str input, set[str] lineseps = newLineCharacters)
72+
str insertFinalNewline(str input, list[str] lineseps = newLineCharacters)
6873
= any(nl <- lineseps, endsWith(input, nl))
6974
? input
7075
: input + mostUsedNewline(input)
@@ -82,7 +87,7 @@ test bool insertFinalNewlineTestMixed()
8287
= insertFinalNewline("a\nb\r\n")
8388
== "a\nb\r\n";
8489
85-
str trimFinalNewline(str input, set[str] lineseps = newLineCharacters) {
90+
str trimFinalNewline(str input, list[str] lineseps = newLineCharacters) {
8691
orderedSeps = reverse(sort(lineseps, bySize));
8792
while (nl <- orderedSeps, endsWith(input, nl)) {
8893
input = input[0..-size(nl)];
@@ -99,7 +104,7 @@ test bool trimFinalNewlineTestEndOnly()
99104
test bool trimFinalNewlineTestWhiteSpace()
100105
= trimFinalNewline("a\n\n\nb\n\n ") == "a\n\n\nb\n\n ";
101106
102-
list[tuple[str, str]] separateLines(str input, set[str] lineseps = newLineCharacters) {
107+
list[tuple[str, str]] separateLines(str input, list[str] lineseps = newLineCharacters) {
103108
orderedSeps = reverse(sort(lineseps, bySize));
104109
105110
list[tuple[str, str]] lines = [];
@@ -123,7 +128,7 @@ list[tuple[str, str]] separateLines(str input, set[str] lineseps = newLineCharac
123128
str mergeLines(list[tuple[str, str]] lines)
124129
= ("" | it + line + sep | <line, sep> <- lines);
125130
126-
str perLine(str input, str(str) lineFunc, set[str] lineseps = newLineCharacters)
131+
str perLine(str input, str(str) lineFunc, list[str] lineseps = newLineCharacters)
127132
= mergeLines([<lineFunc(l), nl> | <l, nl> <- separateLines(input, lineseps=lineseps)]);
128133
129134
test bool perLineTest()

0 commit comments

Comments
 (0)