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
.codeInsight
.editorActions
;
18 import com
.intellij
.psi
.PsiElement
;
19 import com
.intellij
.psi
.PsiWhiteSpace
;
20 import com
.intellij
.psi
.xml
.*;
21 import com
.intellij
.openapi
.util
.TextRange
;
22 import com
.intellij
.openapi
.editor
.Editor
;
24 import java
.util
.List
;
26 public class XmlTagSelectioner
extends ExtendWordSelectionHandlerBase
{
27 public boolean canSelect(PsiElement e
) {
28 return e
instanceof XmlTag
;
31 public List
<TextRange
> select(PsiElement e
, CharSequence editorText
, int cursorOffset
, Editor editor
) {
32 List
<TextRange
> result
= super.select(e
, editorText
, cursorOffset
, editor
);
33 PsiElement
[] children
= e
.getChildren();
35 addTagContentSelection(children
, result
, editorText
);
37 PsiElement prev
= e
.getPrevSibling();
38 while (prev
instanceof PsiWhiteSpace
|| prev
instanceof XmlText
|| prev
instanceof XmlComment
) {
39 if (prev
instanceof XmlText
&& prev
.getText().trim().length() > 0) break;
40 if (prev
instanceof XmlComment
) {
41 result
.addAll(expandToWholeLine(editorText
,
42 new TextRange(prev
.getTextRange().getStartOffset(),
43 e
.getTextRange().getEndOffset()),
46 prev
= prev
.getPrevSibling();
52 private static void addTagContentSelection(final PsiElement
[] children
, final List
<TextRange
> result
, final CharSequence editorText
) {
53 PsiElement first
= null;
54 PsiElement last
= null;
55 for (PsiElement child
: children
) {
56 if (child
instanceof XmlToken
) {
57 XmlToken token
= (XmlToken
)child
;
58 if (token
.getTokenType() == XmlTokenType
.XML_TAG_END
) {
59 first
= token
.getNextSibling();
61 if (token
.getTokenType() == XmlTokenType
.XML_END_TAG_START
) {
62 last
= token
.getPrevSibling();
68 if (first
!= null && last
!= null) {
69 result
.addAll(expandToWholeLine(editorText
,
70 new TextRange(first
.getTextRange().getStartOffset(),
71 last
.getTextRange().getEndOffset()),