update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / actions / BaseCodeInsightAction.java
blob2a76da8174ba42b999e3a851071ecc1fcdcaf312
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.codeInsight.actions;
19 import com.intellij.codeInsight.lookup.Lookup;
20 import com.intellij.codeInsight.lookup.LookupManager;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
22 import com.intellij.openapi.actionSystem.DataContext;
23 import com.intellij.openapi.actionSystem.PlatformDataKeys;
24 import com.intellij.openapi.actionSystem.Presentation;
25 import com.intellij.openapi.editor.Editor;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.psi.PsiDocumentManager;
28 import com.intellij.psi.PsiFile;
29 import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
33 public abstract class BaseCodeInsightAction extends CodeInsightAction {
34 private final boolean myLookForInjectedEditor;
36 protected BaseCodeInsightAction() {
37 this(true);
40 protected BaseCodeInsightAction(boolean lookForInjectedEditor) {
41 myLookForInjectedEditor = lookForInjectedEditor;
44 @Nullable
45 protected Editor getEditor(@NotNull final DataContext dataContext, @NotNull final Project project) {
46 Editor editor = getBaseEditor(dataContext, project);
47 if (!myLookForInjectedEditor) return editor;
48 return getInjectedEditor(project, editor);
51 public static Editor getInjectedEditor(@NotNull Project project, final Editor editor) {
52 return getInjectedEditor(project, editor, true);
55 public static Editor getInjectedEditor(@NotNull Project project, final Editor editor, boolean commit) {
56 Editor injectedEditor = editor;
57 if (editor != null) {
58 PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
59 PsiFile psiFile = documentManager.getCachedPsiFile(editor.getDocument());
60 if (psiFile != null) {
61 if (commit) documentManager.commitAllDocuments();
62 injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, psiFile);
65 return injectedEditor;
68 @Nullable
69 protected Editor getBaseEditor(final DataContext dataContext, final Project project) {
70 return super.getEditor(dataContext, project);
73 public void update(AnActionEvent event) {
74 Presentation presentation = event.getPresentation();
75 DataContext dataContext = event.getDataContext();
76 Project project = PlatformDataKeys.PROJECT.getData(dataContext);
77 if (project == null){
78 presentation.setEnabled(false);
79 return;
82 final Lookup activeLookup = LookupManager.getInstance(project).getActiveLookup();
83 if (activeLookup != null){
84 presentation.setEnabled(isValidForLookup());
86 else {
87 super.update(event);
91 protected boolean isValidForLookup() {
92 return false;