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 org
.intellij
.plugins
.intelliLang
.inject
;
19 import com
.intellij
.openapi
.components
.ServiceManager
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.util
.Condition
;
22 import com
.intellij
.psi
.*;
23 import com
.intellij
.util
.PairProcessor
;
24 import com
.intellij
.util
.containers
.ContainerUtil
;
25 import org
.intellij
.plugins
.intelliLang
.Configuration
;
27 import java
.util
.Collections
;
28 import java
.util
.List
;
29 import java
.util
.concurrent
.CopyOnWriteArrayList
;
32 * @author Gregory.Shrago
34 public class TemporaryPlacesRegistry
{
35 private final Project myProject
;
36 private final List
<TemporaryPlace
> myTempPlaces
= new CopyOnWriteArrayList
<TemporaryPlace
>();
38 public static TemporaryPlacesRegistry
getInstance(final Project project
) {
39 return ServiceManager
.getService(project
, TemporaryPlacesRegistry
.class);
42 public TemporaryPlacesRegistry(final Project project
) {
46 public List
<TemporaryPlace
> getTempInjectionsSafe() {
47 final List
<TemporaryPlace
> placesToRemove
= ContainerUtil
.findAll(myTempPlaces
, new Condition
<TemporaryPlace
>() {
48 public boolean value(final TemporaryPlace place
) {
49 return place
.elementPointer
.getElement() == null;
52 if (!placesToRemove
.isEmpty()) {
53 myTempPlaces
.removeAll(placesToRemove
);
58 public List
<TemporaryPlace
> getTempInjectionsSafe(final PsiLanguageInjectionHost host
) {
59 return ContainerUtil
.findAll(getTempInjectionsSafe(), new Condition
<TemporaryPlace
>() {
60 public boolean value(final TemporaryPlace pair
) {
61 return pair
.elementPointer
.getElement() == host
;
66 public void removeHostWithUndo(final Project project
, final PsiLanguageInjectionHost host
) {
67 final List
<TemporaryPlace
> places
= getTempInjectionsSafe(host
);
68 if (places
.isEmpty()) return;
69 Configuration
.replaceInjectionsWithUndo(project
, Collections
.<TemporaryPlace
>emptyList(), places
, Collections
.<PsiElement
>emptyList(), new PairProcessor
<List
<TemporaryPlace
>, List
<TemporaryPlace
>>() {
70 public boolean process(final List
<TemporaryPlace
> add
,
71 final List
<TemporaryPlace
> remove
) {
72 myTempPlaces
.addAll(add
);
73 myTempPlaces
.removeAll(remove
);
79 public void addHostWithUndo(final PsiLanguageInjectionHost host
, final InjectedLanguage language
) {
80 final SmartPointerManager manager
= SmartPointerManager
.getInstance(myProject
);
81 final SmartPsiElementPointer
<PsiLanguageInjectionHost
> pointer
= manager
.createSmartPsiElementPointer(host
);
82 final TemporaryPlace place
= new TemporaryPlace(language
, pointer
);
83 if (myTempPlaces
.contains(place
)) return;
84 Configuration
.replaceInjectionsWithUndo(myProject
, Collections
.singletonList(place
), Collections
.<TemporaryPlace
>emptyList(), Collections
.<PsiElement
>emptyList(), new PairProcessor
<List
<TemporaryPlace
>, List
<TemporaryPlace
>>() {
85 public boolean process(final List
<TemporaryPlace
> add
, final List
<TemporaryPlace
> remove
) {
86 myTempPlaces
.addAll(add
);
87 myTempPlaces
.removeAll(remove
);
93 public static class TemporaryPlace
{
94 final InjectedLanguage language
;
95 final SmartPsiElementPointer
<PsiLanguageInjectionHost
> elementPointer
;
97 public TemporaryPlace(final InjectedLanguage language
, final SmartPsiElementPointer
<PsiLanguageInjectionHost
> elementPointer
) {
98 this.language
= language
;
99 this.elementPointer
= elementPointer
;
103 public boolean equals(final Object o
) {
104 if (this == o
) return true;
105 if (o
== null || getClass() != o
.getClass()) return false;
107 final TemporaryPlace place
= (TemporaryPlace
)o
;
109 if (!elementPointer
.equals(place
.elementPointer
)) return false;
110 if (!language
.equals(place
.language
)) return false;
116 public int hashCode() {
117 int result
= language
.hashCode();
118 result
= 31 * result
+ elementPointer
.hashCode();