Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve how method and variable positions names are resolved #597

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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