Skip to content

Commit

Permalink
improve how method and variable positions names are resoved
Browse files Browse the repository at this point in the history
fix: #592
  • Loading branch information
gayanper committed Jul 15, 2024
1 parent b2c10a1 commit 0739982
Showing 1 changed file with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ void commonSettings(ASTNode res, JCTree javac, int length) {
}
}

private void nameSettings(SimpleName name, JCMethodDecl javac) {
var start = javac.getModifiers().getEndPosition(this.javacCompilationUnit.endPositions);
if (javac.getReturnType() != null) {
start = javac.getReturnType().getEndPosition(this.javacCompilationUnit.endPositions);
}
var length = javac.getName().length();
name.setSourceRange(start, length);
}

private void nameSettings(SimpleName name, JCVariableDecl javac) {
var start = javac.getModifiers().getEndPosition(this.javacCompilationUnit.endPositions);
if (javac.getType() != null) {
start = javac.getType().getEndPosition(this.javacCompilationUnit.endPositions);
}
var length = javac.getName().length();
name.setSourceRange(start, length);
}

private Name toName(JCTree expression) {
return toName(expression, this::commonSettings);
}
Expand Down Expand Up @@ -781,11 +799,15 @@ 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);
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()));
var name = this.ast.newSimpleName(jcid.getName().toString());
nameSettings(name, javac);
res.setName(name);
retTypeTree = null;
if( jcid.toString().equals(getNodeName(parent))) {
res.setConstructor(true);
Expand Down Expand Up @@ -916,19 +938,20 @@ 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);
}
}
// 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);
res.setName(simpleName);
}
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
Expand Down

0 comments on commit 0739982

Please sign in to comment.