Skip to content

Commit

Permalink
F2 rename of Lombok @Singular attribute does nothing
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza authored and rgrunber committed Sep 20, 2023
1 parent dc1fb02 commit 91ebb5f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> singulars;
List<String> normals;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:<lombok_jar> (-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:<lombok_jar> (-javagent:~/.m2/repository/org/projectlombok/lombok/1.18.28/lombok-1.18.28.jar)
// https://github.com/redhat-developer/vscode-java/issues/2805
@Test
Expand Down

0 comments on commit 91ebb5f

Please sign in to comment.