Skip to content

Commit

Permalink
Allow renaming lambda parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and fbricon committed Mar 3, 2020
1 parent b881de5 commit c026aaf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jdt.internal.core.manipulation.search.OccurrencesFinder;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.corext.refactoring.RefactoringAvailabilityTester;
import org.eclipse.jdt.ls.core.internal.corrections.InnovationContext;
import org.eclipse.lsp4j.PrepareRenameResult;
import org.eclipse.lsp4j.Range;
Expand Down Expand Up @@ -80,8 +81,13 @@ private boolean isBinaryOrPackage(ASTNode node) {
if (node instanceof Name) {
IBinding resolvedBinding = ((Name) node).resolveBinding();
IJavaElement element = resolvedBinding != null ? resolvedBinding.getJavaElement() : null;
if (element != null) {
return element.getAncestor(IJavaElement.CLASS_FILE) != null || element.getElementType() == IJavaElement.PACKAGE_FRAGMENT;
try {
if (element == null || element.getElementType() == IJavaElement.PACKAGE_FRAGMENT || !RefactoringAvailabilityTester.isRenameElementAvailable(element)) {
return true;
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
return true;
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,29 @@ public void testRenameTypeParameterInMethod() throws JavaModelException, BadLoca
assertTrue(result.getLeft().getStart().getLine() > 0);
}

@Test
public void testRenameLambdaParameter() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);

// @formatter:off
String[] codes =
{
"package test1;\n",
"import java.util.function.Function;\n",
"public class Test {\n",
" Function<Integer, String> f = i|* -> \"\" + i;\n",
"}\n"
};
// @formatter:on
StringBuilder builder = new StringBuilder();
Position pos = mergeCode(builder, codes);
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", builder.toString(), false, null);

Either<Range, PrepareRenameResult> result = prepareRename(cu, pos, "j");
assertNotNull(result.getLeft());
assertTrue(result.getLeft().getStart().getLine() > 0);
}

@Test
public void testRenameJavadoc() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
Expand Down

0 comments on commit c026aaf

Please sign in to comment.