summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/codeInsight/actions/MultiCaretCodeInsightAction.java
blob: 568e17b58bd6cb90a527b8e631824c75493395c5 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * 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.codeInsight.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.editor.actionSystem.DocCommandGroupId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.util.PsiUtilBase;
import org.jetbrains.annotations.NotNull;

/**
 * Base class for PSI-aware editor actions that need to support multiple carets.
 * Recognizes multi-root PSI and injected fragments, so different carets might be processed in context of different
 * {@link com.intellij.openapi.editor.Editor} and {@link com.intellij.psi.PsiFile} instances.
 * <p>
 * Implementations should implement {@link #getHandler()} method, and might override {@link
 * #isValidFor(com.intellij.openapi.project.Project, com.intellij.openapi.editor.Editor, com.intellij.openapi.editor.Caret, com.intellij.psi.PsiFile)} method.
 *
 * @see com.intellij.codeInsight.actions.MultiCaretCodeInsightActionHandler
 */
public abstract class MultiCaretCodeInsightAction extends AnAction {
  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null) {
      return;
    }
    final Editor hostEditor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    if (hostEditor == null) {
      return;
    }

    actionPerformedImpl(project, hostEditor);
  }

  public void actionPerformedImpl(final Project project, final Editor hostEditor) {
    CommandProcessor.getInstance().executeCommand(project, new Runnable() {
      @Override
      public void run() {
        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          @Override
          public void run() {
            MultiCaretCodeInsightActionHandler handler = getHandler();
            try {
              iterateOverCarets(project, hostEditor, handler);
            }
            finally {
              handler.postInvoke();
            }
          }
        });
      }
    }, getCommandName(), DocCommandGroupId.noneGroupId(hostEditor.getDocument()));

    hostEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
  }

  @Override
  public void update(AnActionEvent e) {
    final Presentation presentation = e.getPresentation();

    Project project = e.getProject();
    if (project == null) {
      presentation.setEnabled(false);
      return;
    }

    Editor hostEditor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    if (hostEditor == null) {
      presentation.setEnabled(false);
      return;
    }

    final Ref<Boolean> enabled  = new Ref<Boolean>(Boolean.FALSE);
    iterateOverCarets(project, hostEditor, new MultiCaretCodeInsightActionHandler() {
      @Override
      public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull PsiFile file) {
        if (isValidFor(project, editor, caret, file)) {
          enabled.set(Boolean.TRUE);
        }
      }
    });
    presentation.setEnabled(enabled.get());
  }

  private static void iterateOverCarets(@NotNull final Project project,
                                 @NotNull final Editor hostEditor,
                                 @NotNull final MultiCaretCodeInsightActionHandler handler) {
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final PsiFile psiFile = documentManager.getCachedPsiFile(hostEditor.getDocument());
    documentManager.commitAllDocuments();

    hostEditor.getCaretModel().runForEachCaret(new CaretAction() {
      @Override
      public void perform(Caret caret) {
        Editor editor = hostEditor;
        if (psiFile != null) {
          Caret injectedCaret = InjectedLanguageUtil.getCaretForInjectedLanguageNoCommit(caret, psiFile);
          if (injectedCaret != null) {
            caret = injectedCaret;
            editor = caret.getEditor();
          }
        }
        final PsiFile file = PsiUtilBase.getPsiFileInEditor(caret, project);
        if (file != null) {
          handler.invoke(project, editor, caret, file);
        }
      }
    }, true);
  }

  /**
   * During action status update this method is invoked for each caret in editor. If at least for a single caret it returns
   * <code>true</code>, action is considered enabled.
   */
  protected boolean isValidFor(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull PsiFile file) {
    return true;
  }

  @NotNull
  protected abstract MultiCaretCodeInsightActionHandler getHandler();

  protected String getCommandName() {
    String text = getTemplatePresentation().getText();
    return text == null ? "" : text;
  }
}