Skip to content

Commit

Permalink
Do not show references for unnamed class
Browse files Browse the repository at this point in the history
Fixes eclipse-jdtls#3069

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Feb 29, 2024
1 parent 27a1a1e commit 94a774d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1854,4 +1854,8 @@ public static boolean isGenerated(IMember member) {
return result;
}

public static boolean isUnnamedClass(IJavaElement element) {
return element.getElementType() == IJavaElement.TYPE && element.getElementName().startsWith("<unnamed_class$");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,19 @@ private void collectCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, Coll
}
//ignore element if method range overlaps the type range, happens for generated bytcode, i.e. with lombok
IJavaElement parentType = element.getAncestor(IJavaElement.TYPE);
if (parentType != null && overlaps(((ISourceReference) parentType).getNameRange(), ((ISourceReference) element).getNameRange())) {
if (parentType != null && !JDTUtils.isUnnamedClass(parentType) && overlaps(((ISourceReference) parentType).getNameRange(), ((ISourceReference) element).getNameRange())) {
continue;
}
} else {//neither a type nor a method, we bail
continue;
}

if (preferenceManager.getPreferences().isReferencesCodeLensEnabled()) {
CodeLens lens = getCodeLens(REFERENCES_TYPE, element, typeRoot);
if (lens != null) {
lenses.add(lens);
if (!JDTUtils.isUnnamedClass(element)) {
CodeLens lens = getCodeLens(REFERENCES_TYPE, element, typeRoot);
if (lens != null) {
lenses.add(lens);
}
}
}
if (preferenceManager.getPreferences().isImplementationsCodeLensEnabled() && element instanceof IType type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
String foo() {
return "foo";
}

void main() {
System.out.println(foo());
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,27 @@ public void testIgnoreLombokCodeLensSymbols() throws Exception {
assertRange(5, 13, 16, cl.getRange());
}

@Test
public void testNoReferenceCodeLensForUnnamedClasses() throws Exception {
String payload = createCodeLensSymbolsRequest("src/java/UnnamedWithString.java");
CodeLensParams codeLensParams = getParams(payload);
String uri = codeLensParams.getTextDocument().getUri();
assertFalse(uri.isEmpty());
//when
List<CodeLens> result = handler.getCodeLensSymbols(uri, monitor);

//then
assertEquals("Found " + result, 2, result.size());

// CodeLens on foo()
CodeLens cl = result.get(0);
assertRange(0, 7, 10, cl.getRange());

// CodeLens on main()
cl = result.get(1);
assertRange(4, 5, 9, cl.getRange());
}

String createCodeLensSymbolsRequest(String file) {
URI uri = project.getFile(file).getRawLocationURI();
return createCodeLensSymbolRequest(uri);
Expand Down

0 comments on commit 94a774d

Please sign in to comment.