summaryrefslogtreecommitdiff
path: root/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java
blob: ad742c5a151032da0ad1c43cb71561243f24b38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.spellchecker.quickfixes;

import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.ide.DataManager;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorComponentImpl;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.refactoring.actions.RenameElementAction;
import com.intellij.refactoring.rename.NameSuggestionProvider;
import com.intellij.refactoring.rename.RenameHandlerRegistry;
import com.intellij.spellchecker.util.SpellCheckerBundle;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;


public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix {

  public RenameTo(String wordWithTypo) {
    super(wordWithTypo);
  }

  @NotNull
  public String getName() {
    return SpellCheckerBundle.message("rename.to");
  }

  @NotNull
  public String getFamilyName() {
    return SpellCheckerBundle.message("rename.to");
  }


  @Nullable
  private static DictionarySuggestionProvider findProvider() {
    Object[] extensions = Extensions.getExtensions(NameSuggestionProvider.EP_NAME);

    for (Object extension : extensions) {
      if (extension instanceof DictionarySuggestionProvider) {
        return (DictionarySuggestionProvider)extension;
      }
    }
    return null;
  }


  @NotNull
  public Anchor getPopupActionAnchor() {
    return Anchor.FIRST;
  }

  @SuppressWarnings({"SSBasedInspection"})
  public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    Runnable fix = new Runnable() {
      public void run() {
        DictionarySuggestionProvider provider = findProvider();
        if (provider != null) {
          provider.setActive(true);
        }

        Editor editor = getEditorFromFocus();
        HashMap<String, Object> map = new HashMap<String, Object>();
        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 = new AnActionEvent(null, dataContext, "", action.getTemplatePresentation(), ActionManager.getInstance(), 0);
          action.actionPerformed(event);
          if (provider != null) {
            provider.setActive(false);
          }
        }
        finally {
          editor.putUserData(RenameHandlerRegistry.SELECT_ALL, selectAll);
        }
      }
    };
    
    if (ApplicationManager.getApplication().isUnitTestMode()) fix.run();
    else SwingUtilities.invokeLater(fix); // TODO [shkate] this is hard to test!
  }

  @Nullable
  private static Editor getEditorFromFocus() {
    final Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    if (c instanceof EditorComponentImpl) {
      return ((EditorComponentImpl)c).getEditor();
    }
    return null;
  }
}