Fix formatting of argument lists (#1283)

This fixes several issues:

1. Leading/trailing line comments surrounding a lambda should make that
   lambda not "trailing", because the formatting otherwise looks bad and
   also isn't stable
2. Fix incorrect algorithm for detecting trailing lambda (currently,
   any number of lambdas makes the alg return `true`)
This commit is contained in:
Daniel Chao
2025-11-03 09:15:58 -08:00
committed by GitHub
parent 4226c21a42
commit d29ae07e14
6 changed files with 82 additions and 33 deletions

View File

@@ -77,8 +77,8 @@ public class GenericParser {
}
var lastImport = parseImportDecl();
imports.add(lastImport);
// keep trailling affixes as part of the import
while (lookahead.isAffix() && lastImport.span.sameLine(spanLookahead)) {
// keep trailing affixes as part of the import
while (lookahead.isAffix() && lastImport.span.isSameLine(spanLookahead)) {
imports.add(makeAffix(next()));
}
if (!isImport()) break;
@@ -662,7 +662,7 @@ public class GenericParser {
if (operator.getPrec() < minPrecedence) break;
// `-` and `[]` must be in the same line as the left operand and have no semicolons inbetween
if ((operator == Operator.MINUS || operator == Operator.SUBSCRIPT)
&& (fullOpToken.hasSemicolon || !expr.span.sameLine(fullOpToken.tk.span))) break;
&& (fullOpToken.hasSemicolon || !expr.span.isSameLine(fullOpToken.tk.span))) break;
var children = new ArrayList<Node>();
children.add(expr);
ff(children);

View File

@@ -15,8 +15,6 @@
*/
package org.pkl.parser.syntax.generic;
import org.pkl.parser.Span;
public record FullSpan(
int charIndex, int length, int lineBegin, int colBegin, int lineEnd, int colEnd) {
@@ -30,11 +28,7 @@ public record FullSpan(
end.colEnd);
}
public Span toSpan() {
return new Span(charIndex, length);
}
public boolean sameLine(FullSpan other) {
public boolean isSameLine(FullSpan other) {
return lineEnd == other.lineBegin;
}