Search in sources :

Example 1 with RenameElementAction

use of com.intellij.refactoring.actions.RenameElementAction in project android by JetBrains.

the class AndroidRenameTest method checkAndRenameUsingHandler.

private void checkAndRenameUsingHandler(String newName) {
    final RenameElementAction action = new RenameElementAction();
    final AnActionEvent e = new TestActionEvent(DataManager.getInstance().getDataContext(myFixture.getEditor().getComponent()), action);
    action.update(e);
    assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
    // Note: This fails when trying to rename XML attribute values: Use myFixture.renameElementAtCaretUsingHandler() instead!
    myFixture.renameElementAtCaretUsingHandler(newName);
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) TestActionEvent(com.intellij.testFramework.TestActionEvent) RenameElementAction(com.intellij.refactoring.actions.RenameElementAction)

Example 2 with RenameElementAction

use of com.intellij.refactoring.actions.RenameElementAction in project android by JetBrains.

the class AndroidRenameTest method checkAndRename.

private void checkAndRename(String newName) {
    final RenameElementAction action = new RenameElementAction();
    final AnActionEvent e = new TestActionEvent(DataManager.getInstance().getDataContext(myFixture.getEditor().getComponent()), action);
    action.update(e);
    assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
    // Note: This fails when trying to rename XML attribute values: Use myFixture.renameElementAtCaretUsingHandler() instead!
    myFixture.renameElementAtCaret(newName);
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) TestActionEvent(com.intellij.testFramework.TestActionEvent) RenameElementAction(com.intellij.refactoring.actions.RenameElementAction)

Example 3 with RenameElementAction

use of com.intellij.refactoring.actions.RenameElementAction in project intellij-community by JetBrains.

the class RenameTo method applyFix.

@SuppressWarnings({ "SSBasedInspection" })
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    DictionarySuggestionProvider provider = findProvider();
    if (provider != null) {
        provider.setActive(true);
    }
    Editor editor = getEditorFromFocus();
    HashMap<String, Object> map = new HashMap<>();
    PsiElement psiElement = descriptor.getPsiElement();
    if (psiElement == null)
        return;
    PsiFile containingFile = psiElement.getContainingFile();
    if (editor == null) {
        editor = InjectedLanguageUtil.openEditorFor(containingFile, project);
    }
    if (editor == null)
        return;
    if (editor instanceof EditorWindow) {
        map.put(CommonDataKeys.EDITOR.getName(), editor);
        map.put(CommonDataKeys.PSI_ELEMENT.getName(), psiElement);
    } else if (ApplicationManager.getApplication().isUnitTestMode()) {
        // TextEditorComponent / FiledEditorManagerImpl give away the data in real life
        map.put(CommonDataKeys.PSI_ELEMENT.getName(), new TextEditorPsiDataProvider().getData(CommonDataKeys.PSI_ELEMENT.getName(), editor, editor.getCaretModel().getCurrentCaret()));
    }
    final Boolean selectAll = editor.getUserData(RenameHandlerRegistry.SELECT_ALL);
    try {
        editor.putUserData(RenameHandlerRegistry.SELECT_ALL, true);
        DataContext dataContext = SimpleDataContext.getSimpleContext(map, DataManager.getInstance().getDataContext(editor.getComponent()));
        AnAction action = new RenameElementAction();
        AnActionEvent event = AnActionEvent.createFromAnAction(action, null, "", dataContext);
        action.actionPerformed(event);
        if (provider != null) {
            provider.setActive(false);
        }
    } finally {
        editor.putUserData(RenameHandlerRegistry.SELECT_ALL, selectAll);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) EditorWindow(com.intellij.injected.editor.EditorWindow) RenameElementAction(com.intellij.refactoring.actions.RenameElementAction) TextEditorPsiDataProvider(com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

RenameElementAction (com.intellij.refactoring.actions.RenameElementAction)3 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)2 TestActionEvent (com.intellij.testFramework.TestActionEvent)2 EditorWindow (com.intellij.injected.editor.EditorWindow)1 SimpleDataContext (com.intellij.openapi.actionSystem.impl.SimpleDataContext)1 Editor (com.intellij.openapi.editor.Editor)1 TextEditorPsiDataProvider (com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 HashMap (com.intellij.util.containers.HashMap)1