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 com
.intellij
.lang
.xml
;
18 import com
.intellij
.codeInsight
.template
.HtmlContextType
;
19 import com
.intellij
.codeInsight
.template
.XmlContextType
;
20 import com
.intellij
.codeInsight
.template
.impl
.TemplateContext
;
21 import com
.intellij
.codeInsight
.template
.impl
.TemplateImpl
;
22 import com
.intellij
.lang
.surroundWith
.SurroundDescriptor
;
23 import com
.intellij
.lang
.surroundWith
.Surrounder
;
24 import com
.intellij
.openapi
.util
.Pair
;
25 import com
.intellij
.psi
.PsiElement
;
26 import com
.intellij
.psi
.PsiFile
;
27 import com
.intellij
.psi
.xml
.XmlTagChild
;
28 import com
.intellij
.psi
.xml
.XmlToken
;
29 import com
.intellij
.psi
.xml
.XmlTokenType
;
30 import com
.intellij
.xml
.util
.XmlUtil
;
31 import org
.jetbrains
.annotations
.NotNull
;
33 import java
.util
.ArrayList
;
34 import java
.util
.List
;
39 public class XmlSurroundDescriptor
implements SurroundDescriptor
{
40 @NotNull public PsiElement
[] getElementsToSurround(PsiFile file
, int startOffset
, int endOffset
) {
41 final Pair
<XmlTagChild
, XmlTagChild
> childrenInRange
= XmlUtil
.findTagChildrenInRange(file
, startOffset
, endOffset
);
42 if (childrenInRange
== null) {
43 final PsiElement elementAt
= file
.findElementAt(startOffset
);
44 if (elementAt
instanceof XmlToken
&&
45 ((XmlToken
)elementAt
).getTokenType() == XmlTokenType
.XML_DATA_CHARACTERS
) {
46 return new PsiElement
[] {elementAt
};
48 return PsiElement
.EMPTY_ARRAY
;
50 List
<PsiElement
> result
= new ArrayList
<PsiElement
>();
51 PsiElement first
= childrenInRange
.getFirst();
52 PsiElement last
= childrenInRange
.getSecond();
55 if (first
== last
) break;
56 first
= first
.getNextSibling();
59 return result
.toArray(new PsiElement
[result
.size()]);
62 @NotNull public Surrounder
[] getSurrounders() {
63 return new Surrounder
[0]; //everything is in live templates now
66 protected boolean isEnabled(final TemplateImpl template
) {
67 final TemplateContext context
= template
.getTemplateContext();
68 return context
.isEnabled(new XmlContextType()) || context
.isEnabled(new HtmlContextType());