From 91ebb5f9798946294c27d53e4eccebd847b650a3 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Wed, 20 Sep 2023 03:31:59 +0200 Subject: [PATCH] F2 rename of Lombok @Singular attribute does nothing Signed-off-by: Snjezana Peco --- .../jdt/ls/core/internal/JDTUtils.java | 14 +++++++++++++- .../src/main/java/org/sample/Test2.java | 12 ++++++++++++ .../internal/handlers/RenameHandlerTest.java | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test2.java 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 ab1a9cb5f3..861296a7d1 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 @@ -109,6 +109,7 @@ import org.eclipse.jdt.internal.codeassist.impl.Engine; import org.eclipse.jdt.internal.compiler.lookup.Binding; import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; +import org.eclipse.jdt.internal.core.NamedMember; import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelComposerCore; import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore; import org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation; @@ -1003,7 +1004,18 @@ 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); + // a workaround for https://github.com/redhat-developer/vscode-java/issues/3203 + if (elements == null || elements.length == 0) { + IJavaElement element = unit.getElementAt(offset); + if (element instanceof NamedMember namedMember) { + ISourceRange range = namedMember.getNameRange(); + if (range.getOffset() <= offset && (range.getOffset() + range.getLength()) >= offset) { + return new IJavaElement[] { element }; + } + } + } + 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