Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F2 rename of Lombok @Singular attribute does nothing #2828

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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