Skip to content

Commit

Permalink
improve how method and variable positions names are resolved (#597)
Browse files Browse the repository at this point in the history
fix: #592

* Use simple name length instead of javac node length.
* consider the fake name and error names when calculating length.
* skip error and fake identifier nodes from position update.
* improve fix how position is calculated
  • Loading branch information
gayanper authored and mickaelistria committed Sep 5, 2024
1 parent dc6f454 commit 1106756
Showing 1 changed file with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import com.sun.tools.javac.tree.DCTree.DCDocComment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAnyPattern;
Expand Down Expand Up @@ -388,6 +389,26 @@ void commonSettings(ASTNode res, JCTree javac, int length) {
}
}

private void nameSettings(SimpleName name, JCMethodDecl javac, String selector, boolean isConstructor) {
if ((selector.equals(ERROR) || selector.equals(FAKE_IDENTIFIER)))
return;
var start = javac.getPreferredPosition();
if (start > -1) {
// handle constructor length using type name instead of selector.
var length = isConstructor ? name.toString().length() : selector.length();
name.setSourceRange(start, length);
}
}

private void nameSettings(SimpleName name, JCVariableDecl javac, String varName) {
if (varName.equals(ERROR) || varName.equals(FAKE_IDENTIFIER))
return;
var start = javac.getPreferredPosition();
if (start > -1) {
name.setSourceRange(start, varName.length());
}
}

private Name toName(JCTree expression) {
return toName(expression, this::commonSettings);
}
Expand Down Expand Up @@ -783,13 +804,17 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
JCTree retTypeTree = javac.getReturnType();
Type retType = null;
if( !javacNameMatchesError) {
res.setName(this.ast.newSimpleName(methodDeclName));
var name = this.ast.newSimpleName(methodDeclName);
nameSettings(name, javac, javacName, isConstructor);
res.setName(name);
} else {
// javac name is an error, so let's treat the return type as the name
if( retTypeTree instanceof JCIdent jcid) {
res.setName(this.ast.newSimpleName(jcid.getName().toString()));
if (retTypeTree instanceof JCIdent jcid) {
var name = this.ast.newSimpleName(jcid.getName().toString());
nameSettings(name, javac, javacName, isConstructor);
res.setName(name);
retTypeTree = null;
if( jcid.toString().equals(getNodeName(parent))) {
if (jcid.toString().equals(getNodeName(parent))) {
res.setConstructor(true);
isConstructor = true;
}
Expand Down Expand Up @@ -918,19 +943,7 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
SingleVariableDeclaration res = this.ast.newSingleVariableDeclaration();
commonSettings(res, javac);
if (convertName(javac.getName()) instanceof SimpleName simpleName) {
int endPos = javac.getEndPosition(this.javacCompilationUnit.endPositions);
if( !simpleName.toString().equals(FAKE_IDENTIFIER)) {
char theChar = this.rawText.charAt(endPos);
char soughtLastChar = simpleName.toString().charAt(simpleName.toString().length() - 1);
while (endPos > res.getStartPosition() && theChar != soughtLastChar) {
theChar = this.rawText.charAt(--endPos);
}
endPos++;
int length = simpleName.toString().length();
if( endPos != -1 && endPos - length > 0) {
simpleName.setSourceRange(endPos - length, length);
}
}
nameSettings(simpleName, javac, simpleName.toString());
res.setName(simpleName);
}
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
Expand Down

0 comments on commit 1106756

Please sign in to comment.