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.
16 package org
.intellij
.plugins
.intelliLang
.inject
;
18 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
19 import com
.intellij
.lang
.Language
;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.extensions
.Extensions
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.util
.Pair
;
24 import com
.intellij
.openapi
.util
.TextRange
;
25 import com
.intellij
.openapi
.vfs
.VirtualFile
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.psi
.PsiFile
;
28 import com
.intellij
.psi
.PsiLanguageInjectionHost
;
29 import com
.intellij
.psi
.impl
.source
.tree
.injected
.InjectedLanguageUtil
;
30 import com
.intellij
.util
.FileContentUtil
;
31 import com
.intellij
.util
.IncorrectOperationException
;
32 import gnu
.trove
.THashSet
;
33 import org
.intellij
.plugins
.intelliLang
.Configuration
;
34 import static org
.intellij
.plugins
.intelliLang
.inject
.InjectLanguageAction
.findInjectionHost
;
35 import org
.jetbrains
.annotations
.NotNull
;
37 import java
.util
.Collections
;
38 import java
.util
.List
;
41 * @author Dmitry Avdeev
43 public class UnInjectLanguageAction
implements IntentionAction
{
46 public String
getText() {
47 return "Un-inject Language";
51 public String
getFamilyName() {
52 return InjectLanguageAction
.INJECT_LANGUAGE_FAMILY
;
55 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
56 PsiLanguageInjectionHost host
= findInjectionHost(editor
, file
);
57 if (host
== null) return false;
58 final List
<Pair
<PsiElement
, TextRange
>> injectedPsi
= InjectedLanguageUtil
.getInjectedPsiFiles(host
);
59 return injectedPsi
!= null && !injectedPsi
.isEmpty();
62 public void invoke(@NotNull final Project project
, Editor editor
, PsiFile file
) throws IncorrectOperationException
{
63 final PsiLanguageInjectionHost host
= findInjectionHost(editor
, file
);
65 if (defaultFunctionalityWorked(host
)) return;
66 for (LanguageInjectionSupport support
: Extensions
.getExtensions(LanguageInjectionSupport
.EP_NAME
)) {
67 if (support
.removeInjectionInPlace(host
)) return;
69 TemporaryPlacesRegistry
.getInstance(project
).removeHostWithUndo(project
, host
);
72 FileContentUtil
.reparseFiles(project
, Collections
.<VirtualFile
>emptyList(), true);
76 private static boolean defaultFunctionalityWorked(final PsiLanguageInjectionHost host
) {
77 final THashSet
<String
> languages
= new THashSet
<String
>();
78 final List
<Pair
<PsiElement
, TextRange
>> files
= InjectedLanguageUtil
.getInjectedPsiFiles(host
);
79 if (files
== null) return false;
80 for (Pair
<PsiElement
, TextRange
> pair
: files
) {
81 for (Language lang
= pair
.first
.getLanguage(); lang
!= null; lang
= lang
.getBaseLanguage()) {
82 languages
.add(lang
.getID());
85 // todo there is a problem: host i.e. literal expression is confused with "target" i.e. parameter
86 // todo therefore this part doesn't work for java
87 return Configuration
.getInstance().setHostInjectionEnabled(host
, languages
, false);
90 public boolean startInWriteAction() {