Fix formatter instability (#1911)
This commit is contained in:
1 parent
fe96e0b1b0
commit
b97abe13bc
3 files changed
+28
-2
No files matched your search
@@ -1467,7 +1467,10 @@ final class Builder {
|
||||
if (endsInLineComment(prev)) {
|
||||
return linesBetween(prev, next) > 1 ? TWO_NEWLINES : mustForceLine();
|
||||
}
|
||||
if (hasTrailingAffix(prev, next)) return Space.INSTANCE;
|
||||
if (hasTrailingAffix(prev, next)) {
|
||||
if (isTerminal(prev, "(") && startsWithBlockComment(next)) return line();
|
||||
return Space.INSTANCE;
|
||||
}
|
||||
if (prev.type == NodeType.DOC_COMMENT) return mustForceLine();
|
||||
if (prev.type == NodeType.ANNOTATION) return forceLine();
|
||||
if (FORCE_LINE_AFFIXES.contains(prev.type) || next.type.isAffix()) {
|
||||
@@ -1528,10 +1531,24 @@ final class Builder {
|
||||
return noNewlines ? ifNotWrap : new IfWrap(id, ifWrap, ifNotWrap);
|
||||
}
|
||||
|
||||
private static boolean startsWithBlockComment(Node node) {
|
||||
var n = node;
|
||||
while (n != null) {
|
||||
if (n.type == NodeType.BLOCK_COMMENT) return true;
|
||||
n = n.children.isEmpty() ? null : n.children.get(0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean hasTrailingAffix(Node node, Node next) {
|
||||
var n = next;
|
||||
while (n != null) {
|
||||
if (n.type.isAffix() && node.span.lineEnd() == n.span.lineBegin()) return true;
|
||||
// semicolons are dropped when formatting, so they never produce a trailing affix.
|
||||
if (n.type.isAffix()
|
||||
&& n.type != NodeType.SEMICOLON
|
||||
&& node.span.lineEnd() == n.span.lineBegin()) {
|
||||
return true;
|
||||
}
|
||||
n = n.children.isEmpty() ? null : n.children.get(0);
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -31,3 +31,6 @@ foo6 =
|
||||
foo7 =
|
||||
if /* some comment */ (bar) 1
|
||||
else 2
|
||||
|
||||
foo8 = if (
|
||||
/* c */ bar) 1 else 2
|
||||
@@ -41,3 +41,9 @@ foo7 =
|
||||
1
|
||||
else
|
||||
2
|
||||
|
||||
foo8 =
|
||||
if (/* c */ bar)
|
||||
1
|
||||
else
|
||||
2
|
||||
Reference in new issue
Block a user