Skip to content

Commit

Permalink
Implementation code lens for non-{interface,abstract} base types/meth…
Browse files Browse the repository at this point in the history
…ods.

- A base type/method that that is not an interface/abstract (or a member
  of an interface/abstract class) may still be inherited and that
  information should be indicated in the implementation code lens
- Add testcase

Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
  • Loading branch information
rgrunber committed Jan 3, 2025
1 parent 27e06a1 commit 67849a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
Expand Down Expand Up @@ -233,17 +232,15 @@ private void collectCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, Coll
}
}
String implementationsPreference = preferenceManager.getPreferences().getImplementationsCodeLens();
if (("all".equals(implementationsPreference) || "types".equals(implementationsPreference)) && element instanceof IType type) {
if (type.isInterface() || Flags.isAbstract(type.getFlags())) {
CodeLens lens = getCodeLens(IMPLEMENTATION_TYPE, element, typeRoot);
if (lens != null) {
lenses.add(lens);
}
if (("all".equals(implementationsPreference) || "types".equals(implementationsPreference)) && element instanceof IType) {
CodeLens lens = getCodeLens(IMPLEMENTATION_TYPE, element, typeRoot);
if (lens != null) {
lenses.add(lens);
}
}

if (("all".equals(implementationsPreference) || "methods".equals(implementationsPreference)) && element instanceof IMethod methodElement) {
if ((methodElement.getParent() instanceof IType parentType && parentType.isInterface()) || Flags.isAbstract(methodElement.getFlags())) {
if (methodElement.getParent() instanceof IType) {
CodeLens lens = getCodeLens(IMPLEMENTATION_TYPE, element, typeRoot);
if (lens != null) {
lenses.add(lens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@ public void testEnableImplementationsCodeLensSymbols() throws Exception {
assertEquals(type, "implementations");
}

@Test
public void testEnableImplementationsCodeLensSymbolsForBaseTypes() throws Exception {
Preferences implementationsCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.IMPLEMENTATIONS_CODE_LENS_KEY, "all"));
Mockito.reset(preferenceManager);
when(preferenceManager.getPreferences()).thenReturn(implementationsCodeLenses);
handler = new CodeLensHandler(preferenceManager);

String payload = createCodeLensSymbolsRequest("src/java/Foo.java");
CodeLensParams codeLensParams = getParams(payload);
String uri = codeLensParams.getTextDocument().getUri();
assertFalse(uri.isEmpty());

//when
List<CodeLens> result = handler.getCodeLensSymbols(uri, monitor);

//then
assertEquals(6, result.size());
@SuppressWarnings("unchecked")
long implementations = result.stream().filter(cl -> "implementations".equals(((List<Object>) cl.getData()).get(2))).count();
assertEquals(3, implementations);
}

@Test
public void testDisableImplementationsCodeLensSymbols() throws Exception {
Preferences noImplementationsCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.IMPLEMENTATIONS_CODE_LENS_KEY, "types"));
Expand Down

0 comments on commit 67849a7

Please sign in to comment.