update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / psi / formatter / XmlFormatterUtilHelper.java
blobfb39592bd81eb8a3597b120cee7e6bbdf67277d0
1 /*
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.
18 * @author max
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();
39 final boolean before;
40 final XmlText xmlText;
41 if(treePrev.getElementType() == XmlElementType.XML_TEXT) {
42 xmlText = (XmlText)treePrev.getPsi();
43 before = true;
45 else if(treePrev.getTreePrev().getElementType() == XmlElementType.XML_TEXT){
46 xmlText = (XmlText)treePrev.getTreePrev().getPsi();
47 before = false;
49 else{
50 xmlText = (XmlText)Factory.createCompositeElement(XmlElementType.XML_TEXT, charTable, treeParent.getPsi().getManager());
51 CodeEditUtil.setNodeGenerated(xmlText.getNode(), true);
52 treeParent.addChild(xmlText.getNode(), treePrev);
53 before = true;
55 final ASTNode node = xmlText.getNode();
56 assert node != null;
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);
60 else {
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;
71 while(place != null){
72 if(place.getElementType() == XmlTokenType.XML_TAG_END) return true;
73 place = place.getTreePrev();
75 return false;
78 public boolean addWhitespace(final ASTNode treePrev, final LeafElement whiteSpaceElement) {
79 if (isInsideTagBody(treePrev)) {
80 addWhitespaceToTagBody(treePrev, whiteSpaceElement);
81 return true;
84 return false;
87 public boolean containsWhitespacesOnly(final ASTNode node) {
88 return (node.getElementType() == XmlTokenType.XML_DATA_CHARACTERS) &&
89 node.getText().trim().length() == 0;