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.
20 package com
.intellij
.psi
.formatter
;
22 import com
.intellij
.lang
.ASTFactory
;
23 import com
.intellij
.lang
.ASTNode
;
24 import com
.intellij
.psi
.impl
.source
.codeStyle
.CodeEditUtil
;
25 import com
.intellij
.psi
.impl
.source
.tree
.Factory
;
26 import com
.intellij
.psi
.impl
.source
.tree
.LeafElement
;
27 import com
.intellij
.psi
.impl
.source
.tree
.SharedImplUtil
;
28 import com
.intellij
.psi
.impl
.source
.tree
.TreeElement
;
29 import com
.intellij
.psi
.xml
.XmlElementType
;
30 import com
.intellij
.psi
.xml
.XmlText
;
31 import com
.intellij
.psi
.xml
.XmlTokenType
;
32 import com
.intellij
.util
.CharTable
;
34 public class XmlFormatterUtilHelper
implements FormatterUtilHelper
{
35 private static void addWhitespaceToTagBody(final ASTNode treePrev
, final LeafElement whiteSpaceElement
) {
36 final CharTable charTable
= SharedImplUtil
.findCharTableByTree(treePrev
);
37 final ASTNode treeParent
= treePrev
.getTreeParent();
40 final XmlText xmlText
;
41 if(treePrev
.getElementType() == XmlElementType
.XML_TEXT
) {
42 xmlText
= (XmlText
)treePrev
.getPsi();
45 else if(treePrev
.getTreePrev().getElementType() == XmlElementType
.XML_TEXT
){
46 xmlText
= (XmlText
)treePrev
.getTreePrev().getPsi();
50 xmlText
= (XmlText
)Factory
.createCompositeElement(XmlElementType
.XML_TEXT
, charTable
, treeParent
.getPsi().getManager());
51 CodeEditUtil
.setNodeGenerated(xmlText
.getNode(), true);
52 treeParent
.addChild(xmlText
.getNode(), treePrev
);
55 final ASTNode node
= xmlText
.getNode();
57 final TreeElement anchorInText
= (TreeElement
) (before ? node
.getFirstChildNode() : node
.getLastChildNode());
58 if (anchorInText
== null) node
.addChild(whiteSpaceElement
);
59 else if (anchorInText
.getElementType() != XmlTokenType
.XML_WHITE_SPACE
) node
.addChild(whiteSpaceElement
, before ? anchorInText
: null);
61 final String text
= before ? whiteSpaceElement
.getText() + anchorInText
.getText() : anchorInText
.getText() +
62 whiteSpaceElement
.getText();
63 node
.replaceChild(anchorInText
, ASTFactory
.whitespace(text
));
67 protected boolean isInsideTagBody(ASTNode place
) {
68 final ASTNode treeParent
= place
.getTreeParent();
69 if(treeParent
.getElementType() != XmlElementType
.XML_TAG
70 && treeParent
.getElementType() != XmlElementType
.HTML_TAG
) return false;
72 if(place
.getElementType() == XmlTokenType
.XML_TAG_END
) return true;
73 place
= place
.getTreePrev();
78 public boolean addWhitespace(final ASTNode treePrev
, final LeafElement whiteSpaceElement
) {
79 if (isInsideTagBody(treePrev
)) {
80 addWhitespaceToTagBody(treePrev
, whiteSpaceElement
);
87 public boolean containsWhitespacesOnly(final ASTNode node
) {
88 return (node
.getElementType() == XmlTokenType
.XML_DATA_CHARACTERS
) &&
89 node
.getText().trim().length() == 0;