[mod-commands] Rename: clear selection before the rename. Otherwise, renamer tries to rename the selected symbol instead

Fixes IDEA-335357 Replace constructor with factory method renames also constructor

GitOrigin-RevId: 93b5b9a8bcaabdb99796222b29449780dab7558a
This commit is contained in:
Tagir Valeev
2023-11-29 11:44:41 +00:00
committed by intellij-monorepo-bot
parent 47d91d14d3
commit 8e57917700
4 changed files with 55 additions and 0 deletions
@@ -0,0 +1,17 @@
public class Main {
private String s;
private int i;
private Main() {
}
public Main(String s, int i) {
this.s = s;
this.i = i;
}
public static Main newMain() {
return new Main();
}
}
@@ -0,0 +1,14 @@
public class Main {
private String s;
private int i;
public <selection>Main<caret></selection>() {
}
public Main(String s, int i) {
this.s = s;
this.i = i;
}
}
@@ -3,6 +3,11 @@ package com.intellij.java.refactoring;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.intention.ReplaceConstructorWithFactoryAction;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupEx;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.ide.IdeEventQueue;
import com.intellij.modcommand.ActionContext;
import com.intellij.modcommand.ModCommand;
@@ -10,6 +15,7 @@ import com.intellij.modcommand.ModCommandExecutor;
import com.intellij.modcommand.Presentation;
import com.intellij.ui.ChooserInterceptor;
import com.intellij.ui.UiInterceptors;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -23,6 +29,23 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase
}
public void testEmptyConstructor() { runTest("01", null); }
public void testWithSelection() {
TemplateManagerImpl.setTemplateTesting(getTestRootDisposable());
configureByFile("/refactoring/replaceConstructorWithFactory/beforeWithSelection.java");
ReplaceConstructorWithFactoryAction action = new ReplaceConstructorWithFactoryAction();
ActionContext context = ActionContext.from(getEditor(), getFile());
Presentation presentation = action.getPresentation(context);
assertNotNull(presentation);
ModCommand command = action.perform(context);
ModCommandExecutor.getInstance().executeInteractively(context, command, getEditor());
final LookupEx lookup = LookupManager.getActiveLookup(getEditor());
assertNotNull(lookup);
LookupElement newMain = ContainerUtil.find(lookup.getItems(), l -> l.getLookupString().equals("newMain"));
assertNotNull(newMain);
((LookupImpl)lookup).finishLookup('\n', newMain);
checkResultByFile("/refactoring/replaceConstructorWithFactory/afterWithSelection.java");
}
public void testSubclass() { runTest("02", null); }
@@ -417,6 +417,7 @@ public class ModCommandExecutorImpl implements ModCommandExecutor {
.build();
PsiElement anchor = namedElement instanceof PsiNameIdentifierOwner owner ?
requireNonNullElse(owner.getNameIdentifier(), namedElement) : namedElement;
finalEditor.getSelectionModel().removeSelection();
finalEditor.getCaretModel().moveToOffset(anchor.getTextOffset());
Renamer renamer = RenamerFactory.EP_NAME.getExtensionList().stream().flatMap(factory -> factory.createRenamers(finalContext).stream())
.findFirst().orElse(null);