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 accuracy of MethodReference matches #97

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -5,10 +5,18 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.search.MethodReferenceMatch;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.lsp4j.SymbolInformation;
Expand All @@ -25,12 +33,50 @@ public List<SymbolInformation> get(SearchMatch match) {
try {
MethodReferenceMatch m = (MethodReferenceMatch) match;
IMethod e = (IMethod) m.getElement();
SymbolInformation symbol = new SymbolInformation();
symbol.setName(e.getElementName());
symbol.setKind(convertSymbolKind(e));
symbol.setContainerName(e.getParent().getElementName());
symbol.setLocation(getLocation(e, match));
symbols.add(symbol);
if (this.query.contains(".")) {
ICompilationUnit unit = e.getCompilationUnit();
jmle marked this conversation as resolved.
Show resolved Hide resolved
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
astParser.setSource(unit);
astParser.setResolveBindings(true);
CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
cu.accept(new ASTVisitor() {
// we are only doing this for MethodInvocation right now
// look into MethodDeclaration if needed
public boolean visit(MethodInvocation node) {
try {
IMethodBinding binding = node.resolveMethodBinding();
if (binding != null) {
// get fqn of the method being called
ITypeBinding declaringClass = binding.getDeclaringClass();
if (declaringClass != null) {
String fullyQualifiedName = declaringClass.getQualifiedName() + "." + binding.getName();
// match fqn with query pattern
if (fullyQualifiedName.matches(getCleanedQuery(query))) {
SymbolInformation symbol = new SymbolInformation();
symbol.setName(e.getElementName());
symbol.setKind(convertSymbolKind(e));
symbol.setContainerName(e.getParent().getElementName());
symbol.setLocation(getLocation(e, match));
symbols.add(symbol);
} else {
logInfo("fqn " + fullyQualifiedName + " did not match with " + query);
}
}
}
} catch (Exception e) {
logInfo("error determining accuracy of match: " + e);
}
return true;
}
});
} else {
SymbolInformation symbol = new SymbolInformation();
symbol.setName(e.getElementName());
symbol.setKind(convertSymbolKind(e));
symbol.setContainerName(e.getParent().getElementName());
symbol.setLocation(getLocation(e, match));
symbols.add(symbol);
}
} catch (Exception e) {
logInfo("unable to convert for variable: " + e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,13 @@ private static void setPosition(Position position, int[] coords) {
position.setLine(coords[0]);
position.setCharacter(coords[1]);
}

/*
* When comparing query pattern with an actual found java element's fqn
* we need to make sure that * that are not preceded with a . are replaced
* by .* so that java regex works as expected on them
*/
default String getCleanedQuery(String query) {
return query.replaceAll("(?<!\\.)\\*", ".*");
}
}
Loading