diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java index 67c486bd06..73d6d4bc03 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java @@ -992,7 +992,15 @@ public static IJavaElement[] findElementsAtSelection(ITypeRoot unit, int line, i return null; } if (offset > -1) { - return unit.codeSelect(offset, 0); + IJavaElement[] elements = unit.codeSelect(offset, 0); + if (elements == null || elements.length == 0) { + IJavaElement element = unit.getElementAt(offset); + if (element != null) { + return new IJavaElement[] { element }; + } + } else { + return elements; + } } return null; } diff --git a/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test2.java b/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test2.java new file mode 100644 index 0000000000..adbe7b6005 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test2.java @@ -0,0 +1,12 @@ +package org.sample; +import java.util.List; +import lombok.Builder; +import lombok.Getter; +import lombok.Singular; +@Builder +@Getter +public class Test2 { + @Singular("singular") + List singulars; + List normals; +} diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java index 1af62f8add..378dbb10fa 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java @@ -593,6 +593,25 @@ public void testRenameTypeLombok() throws Exception { assertEquals(expected, TextEditUtil.apply(source, edit.getChanges().get(JDTUtils.toURI(cu)))); } + // this test should pass when starting with -javaagent: (-javagent:~/.m2/repository/org/projectlombok/lombok/1.18.28/lombok-1.18.28.jar) + // https://github.com/redhat-developer/vscode-java/issues/3203 + @Test + public void testLombokSingular() throws Exception { + when(preferenceManager.getPreferences().isImportMavenEnabled()).thenReturn(true); + importProjects("maven/mavenlombok"); + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("mavenlombok"); + IFile file = project.getFile("src/main/java/org/sample/Test2.java"); + assertTrue(file.exists()); + ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file); + Position pos = new Position(9, 18); + String source = cu.getSource(); + String expected = source.replace("singulars", "singulars2"); + WorkspaceEdit edit = getRenameEdit(cu, pos, "singulars2"); + assertNotNull(edit); + assertEquals(1, edit.getChanges().size()); + assertEquals(expected, TextEditUtil.apply(source, edit.getChanges().get(JDTUtils.toURI(cu)))); + } + // this test should pass when starting with -javaagent: (-javagent:~/.m2/repository/org/projectlombok/lombok/1.18.28/lombok-1.18.28.jar) // https://github.com/redhat-developer/vscode-java/issues/2805 @Test