From c026aafd84e454f77030132b7095b3971bd5a493 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Mon, 2 Mar 2020 23:14:24 +0100 Subject: [PATCH] Allow renaming lambda parameters Signed-off-by: Snjezana Peco --- .../handlers/PrepareRenameHandler.java | 10 ++++++-- .../handlers/PrepareRenameHandlerTest.java | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandler.java index 40b8ba48f3..0e1b811d6b 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandler.java @@ -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; @@ -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; diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandlerTest.java index 0f88023616..529fc648a9 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PrepareRenameHandlerTest.java @@ -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 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 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);