Skip to content

Commit

Permalink
Use indexOfNextNonWhitespace() rather than getStartPosition()
Browse files Browse the repository at this point in the history
Lombok messes with the positions of the AST elements and as a result they don't always align with the source text.
  • Loading branch information
knutwannheden authored and sambsnyd committed Nov 11, 2024
1 parent f50e53b commit aa7669a
Showing 1 changed file with 1 addition and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ public J visitWildcard(WildcardTree node, Space fmt) {
return null;
}
try {
String prefix = source.substring(cursor, Math.max(cursor, getActualStartPosition((JCTree) t)));
String prefix = source.substring(cursor, indexOfNextNonWhitespace(cursor, source));
cursor += prefix.length();
// Java 21 and 23 have a different return type from getCommentTree; with reflection we can support both
Method getCommentTreeMethod = DocCommentTable.class.getMethod("getCommentTree", JCTree.class);
Expand All @@ -1695,14 +1695,6 @@ public J visitWildcard(WildcardTree node, Space fmt) {
}
}

private static int getActualStartPosition(JCTree t) {
// not sure if this is a bug in Lombok, but the variable's start position is after the `val` annotation
if (t instanceof JCVariableDecl && isLombokVal((JCVariableDecl) t)) {
return ((JCVariableDecl) t).mods.annotations.get(0).getStartPosition();
}
return t.getStartPosition();
}

private void reportJavaParsingException(Throwable ex) {
// this SHOULD never happen, but is here simply as a diagnostic measure in the event of unexpected exceptions
StringBuilder message = new StringBuilder("Failed to convert for the following cursor stack:");
Expand Down

0 comments on commit aa7669a

Please sign in to comment.