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 Mar 1, 2024
1 parent 27a1a1e commit fa448a2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 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 @@ -118,13 +118,15 @@ public class CodeLensHandlerTest extends AbstractProjectsManagerBasedTest {
private CodeLensHandler handler;

private IProject project;
private IProject java21Project;

private PreferenceManager preferenceManager;

@Before
public void setup() throws Exception {
importProjects("eclipse/hello");
importProjects(List.of("eclipse/hello", "eclipse/java21"));
project = WorkspaceHelper.getProject("hello");
java21Project = WorkspaceHelper.getProject("java21");
preferenceManager = mock(PreferenceManager.class);
when(preferenceManager.getPreferences()).thenReturn(new Preferences());
handler = new CodeLensHandler(preferenceManager);
Expand Down Expand Up @@ -377,11 +379,39 @@ public void testIgnoreLombokCodeLensSymbols() throws Exception {
assertRange(5, 13, 16, cl.getRange());
}

@Test
public void testNoReferenceCodeLensForUnnamedClasses() throws Exception {
String payload = createCodeLensSymbolsRequestJava21("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());
assertEquals("1 reference", cl.getCommand().getTitle());

// CodeLens on main()
cl = result.get(1);
assertRange(4, 5, 9, cl.getRange());
assertEquals("0 references", cl.getCommand().getTitle());
}

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

String createCodeLensSymbolsRequestJava21(String file) {
URI uri = java21Project.getFile(file).getRawLocationURI();
return createCodeLensSymbolRequest(uri);
}

String createCodeLensSymbolRequest(URI file) {
String fileURI = ResourceUtils.fixURI(file);
return CODELENS_PARAM_TEMPLATE.replace("${file}", fileURI);
Expand Down

0 comments on commit fa448a2

Please sign in to comment.