Search in sources :

Example 31 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class XmlTextImpl method _splitText.

@Nullable
private XmlText _splitText(final int displayOffset) throws IncorrectOperationException {
    final XmlTag xmlTag = (XmlTag) getParent();
    if (displayOffset == 0)
        return this;
    final int length = getValue().length();
    if (displayOffset >= length) {
        return null;
    }
    final PomModel model = PomManager.getModel(xmlTag.getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    class MyTransaction extends PomTransactionBase {

        private XmlTextImpl myRight;

        MyTransaction() {
            super(xmlTag, aspect);
        }

        @Override
        @Nullable
        public PomModelEvent runInner() throws IncorrectOperationException {
            final String oldText = getValue();
            final int physicalOffset = displayToPhysical(displayOffset);
            PsiElement childElement = findElementAt(physicalOffset);
            if (childElement != null && childElement.getNode().getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
                FileElement holder = DummyHolderFactory.createHolder(getManager(), null).getTreeElement();
                int splitOffset = physicalOffset - childElement.getStartOffsetInParent();
                myRight = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
                CodeEditUtil.setNodeGenerated(myRight, true);
                holder.rawAddChildren(myRight);
                PsiElement e = childElement;
                while (e != null) {
                    CodeEditUtil.setNodeGenerated(e.getNode(), true);
                    e = e.getNextSibling();
                }
                String leftText = childElement.getText().substring(0, splitOffset);
                String rightText = childElement.getText().substring(splitOffset);
                LeafElement rightElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(rightText));
                CodeEditUtil.setNodeGenerated(rightElement, true);
                LeafElement leftElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(leftText));
                CodeEditUtil.setNodeGenerated(leftElement, true);
                rawInsertAfterMe(myRight);
                myRight.rawAddChildren(rightElement);
                if (childElement.getNextSibling() != null) {
                    myRight.rawAddChildren((TreeElement) childElement.getNextSibling());
                }
                ((TreeElement) childElement).rawRemove();
                XmlTextImpl.this.rawAddChildren(leftElement);
            } else {
                final PsiFile containingFile = xmlTag.getContainingFile();
                final FileElement holder = DummyHolderFactory.createHolder(containingFile.getManager(), null, ((PsiFileImpl) containingFile).getTreeElement().getCharTable()).getTreeElement();
                final XmlTextImpl rightText = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
                CodeEditUtil.setNodeGenerated(rightText, true);
                holder.rawAddChildren(rightText);
                ((ASTNode) xmlTag).addChild(rightText, getTreeNext());
                final String value = getValue();
                setValue(value.substring(0, displayOffset));
                rightText.setValue(value.substring(displayOffset));
                CodeEditUtil.setNodeGenerated(rightText, true);
                myRight = rightText;
            }
            clearCaches();
            myRight.clearCaches();
            return createEvent(new XmlTextChangedImpl(XmlTextImpl.this, oldText), new XmlTagChildAddImpl(xmlTag, myRight));
        }

        public XmlText getResult() {
            return myRight;
        }
    }
    final MyTransaction transaction = new MyTransaction();
    model.runTransaction(transaction);
    return transaction.getResult();
}
Also used : PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) TreeElement(com.intellij.psi.impl.source.tree.TreeElement) XmlTagChildAddImpl(com.intellij.pom.xml.impl.events.XmlTagChildAddImpl) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) FileElement(com.intellij.psi.impl.source.tree.FileElement) XmlTextChangedImpl(com.intellij.pom.xml.impl.events.XmlTextChangedImpl) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class XmlTokenManipulator method handleContentChange.

@Override
public XmlToken handleContentChange(@NotNull XmlToken xmlToken, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    String oldText = xmlToken.getText();
    String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
    IElementType tokenType = xmlToken.getTokenType();
    FileElement holder = DummyHolderFactory.createHolder(xmlToken.getManager(), null).getTreeElement();
    LeafElement leaf = ASTFactory.leaf(tokenType, holder.getCharTable().intern(newText));
    holder.rawAddChildren(leaf);
    return (XmlToken) xmlToken.replace(leaf.getPsi());
}
Also used : IElementType(com.intellij.psi.tree.IElementType) FileElement(com.intellij.psi.impl.source.tree.FileElement) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 33 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class RestLine method updateText.

@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
    ASTNode valueNode = getNode().getFirstChildNode();
    assert valueNode instanceof LeafElement;
    ((LeafElement) valueNode).replaceWithText(text);
    return this;
}
Also used : ASTNode(com.intellij.lang.ASTNode) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 34 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class GenerateXmlTagAction method generateRaw.

private static void generateRaw(@NotNull final XmlTag newTag) {
    XmlElementDescriptor selected = newTag.getDescriptor();
    if (selected == null)
        return;
    switch(selected.getContentType()) {
        case XmlElementDescriptor.CONTENT_TYPE_EMPTY:
            newTag.collapseIfEmpty();
            ASTNode node = newTag.getNode();
            assert node != null;
            ASTNode elementEnd = node.findChildByType(XmlTokenType.XML_EMPTY_ELEMENT_END);
            if (elementEnd == null) {
                LeafElement emptyTagEnd = Factory.createSingleLeafElement(XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, newTag.getManager());
                node.addChild(emptyTagEnd);
            }
            break;
        case XmlElementDescriptor.CONTENT_TYPE_MIXED:
            newTag.getValue().setText("");
    }
    for (XmlAttributeDescriptor descriptor : selected.getAttributesDescriptors(newTag)) {
        if (descriptor.isRequired()) {
            newTag.setAttribute(descriptor.getName(), "");
        }
    }
    List<XmlElementDescriptor> tags = getRequiredSubTags(selected);
    for (XmlElementDescriptor descriptor : tags) {
        if (descriptor == null) {
            XmlTag tag = XmlElementFactory.getInstance(newTag.getProject()).createTagFromText("<", newTag.getLanguage());
            newTag.addSubTag(tag, false);
        } else {
            XmlTag subTag = newTag.addSubTag(createTag(newTag, descriptor), false);
            generateRaw(subTag);
        }
    }
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ASTNode(com.intellij.lang.ASTNode) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 35 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.

the class AbstractXmlBlock method findChildAfter.

@Nullable
protected static ASTNode findChildAfter(@NotNull final ASTNode child, final int endOffset) {
    TreeElement fileNode = TreeUtil.getFileElement((TreeElement) child);
    final LeafElement leaf = fileNode.findLeafElementAt(endOffset);
    if (leaf != null && leaf.getStartOffset() == endOffset && endOffset > 0) {
        return fileNode.findLeafElementAt(endOffset - 1);
    }
    return leaf;
}
Also used : LeafElement(com.intellij.psi.impl.source.tree.LeafElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LeafElement (com.intellij.psi.impl.source.tree.LeafElement)41 ASTNode (com.intellij.lang.ASTNode)15 PsiElement (com.intellij.psi.PsiElement)9 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)6 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)4 IElementType (com.intellij.psi.tree.IElementType)4 PsiFragment (com.intellij.dupLocator.util.PsiFragment)3 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 CharTable (com.intellij.util.CharTable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1