Search in sources :

Example 1 with XmlTagChild

use of com.intellij.psi.xml.XmlTagChild in project intellij-community by JetBrains.

the class XmlSurroundDescriptor method getElementsToSurround.

@Override
@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    final Pair<XmlTagChild, XmlTagChild> childrenInRange = XmlUtil.findTagChildrenInRange(file, startOffset, endOffset);
    if (childrenInRange == null) {
        final PsiElement elementAt = file.findElementAt(startOffset);
        if (elementAt instanceof XmlToken && ((XmlToken) elementAt).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) {
            return new PsiElement[] { elementAt };
        }
        return PsiElement.EMPTY_ARRAY;
    }
    List<PsiElement> result = new ArrayList<>();
    PsiElement first = childrenInRange.getFirst();
    PsiElement last = childrenInRange.getSecond();
    while (true) {
        result.add(first);
        if (first == last)
            break;
        first = first.getNextSibling();
    }
    return PsiUtilCore.toPsiElementArray(result);
}
Also used : ArrayList(java.util.ArrayList) XmlTagChild(com.intellij.psi.xml.XmlTagChild) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XmlTagChild

use of com.intellij.psi.xml.XmlTagChild in project intellij-community by JetBrains.

the class DefaultXmlPsiPolicy method encodeXmlTextContents.

@Override
public ASTNode encodeXmlTextContents(String displayText, PsiElement text) {
    final PsiFile containingFile = text.getContainingFile();
    CharTable charTable = SharedImplUtil.findCharTableByTree(text.getNode());
    final FileElement dummyParent = DummyHolderFactory.createHolder(text.getManager(), null, charTable).getTreeElement();
    final XmlTag rootTag = ((XmlFile) PsiFileFactory.getInstance(containingFile.getProject()).createFileFromText("a.xml", "<a>" + displayText + "</a>")).getRootTag();
    assert rootTag != null;
    final XmlTagChild[] tagChildren = rootTag.getValue().getChildren();
    final XmlTagChild child = tagChildren.length > 0 ? tagChildren[0] : null;
    LOG.assertTrue(child != null, "Child is null for tag: " + rootTag.getText());
    final TreeElement element = (TreeElement) child.getNode();
    ((TreeElement) tagChildren[tagChildren.length - 1].getNode().getTreeNext()).rawRemoveUpToLast();
    dummyParent.rawAddChildren(element);
    TreeUtil.clearCaches(dummyParent);
    return element.getFirstChildNode();
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) FileElement(com.intellij.psi.impl.source.tree.FileElement) PsiFile(com.intellij.psi.PsiFile) CharTable(com.intellij.util.CharTable) XmlTagChild(com.intellij.psi.xml.XmlTagChild) XmlTag(com.intellij.psi.xml.XmlTag) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Aggregations

XmlTagChild (com.intellij.psi.xml.XmlTagChild)2 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 FileElement (com.intellij.psi.impl.source.tree.FileElement)1 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlToken (com.intellij.psi.xml.XmlToken)1 CharTable (com.intellij.util.CharTable)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1