Search in sources :

Example 36 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class RestParser method parseMarkup.

private static void parseMarkup(PsiBuilder builder) {
    PsiBuilder.Marker marker = builder.mark();
    IElementType type = builder.getTokenType();
    if (type == RestTokenTypes.SUBSTITUTION) {
        builder.advanceLexer();
        marker.done(RestElementTypes.REFERENCE_TARGET);
        builder.advanceLexer();
        marker = builder.mark();
        type = builder.getTokenType();
    }
    if (type == RestTokenTypes.DIRECTIVE) {
        gotoNextWhiteSpaces(builder);
        if (builder.getTokenType() != RestTokenTypes.WHITESPACE) {
            builder.advanceLexer();
            marker.done(RestElementTypes.DIRECTIVE_BLOCK);
            return;
        }
        skipBlankLines(builder);
        if (builder.getTokenType() != RestTokenTypes.WHITESPACE || "\n".equals(builder.getTokenText())) {
            marker.done(RestElementTypes.DIRECTIVE_BLOCK);
            return;
        }
        String white = builder.getTokenText();
        parseDirective(builder, white, marker);
    } else if (type == RestTokenTypes.FOOTNOTE || type == RestTokenTypes.CITATION || type == RestTokenTypes.HYPERLINK || type == RestTokenTypes.ANONYMOUS_HYPERLINK) {
        builder.advanceLexer();
        marker.done(RestElementTypes.REFERENCE_TARGET);
    } else {
        builder.advanceLexer();
        marker.drop();
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 37 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class RestParser method parse.

@NotNull
public ASTNode parse(IElementType root, PsiBuilder builder) {
    final PsiBuilder.Marker rootMarker = builder.mark();
    while (!builder.eof()) {
        IElementType type = builder.getTokenType();
        if (type == RestTokenTypes.EXPLISIT_MARKUP_START) {
            builder.advanceLexer();
            parseMarkup(builder);
        } else if (type == RestTokenTypes.REFERENCE_NAME || type == RestTokenTypes.SUBSTITUTION) {
            PsiBuilder.Marker marker = builder.mark();
            builder.advanceLexer();
            marker.done(RestTokenTypes.REFERENCE_NAME);
        } else if (type == RestTokenTypes.TITLE) {
            PsiBuilder.Marker marker = builder.mark();
            builder.advanceLexer();
            marker.done(RestTokenTypes.TITLE);
        } else if (type == RestTokenTypes.FIELD) {
            parseFieldList(builder);
        } else if (type == RestTokenTypes.INLINE_LINE) {
            PsiBuilder.Marker marker = builder.mark();
            builder.advanceLexer();
            marker.done(RestElementTypes.INLINE_BLOCK);
        } else if (type == RestTokenTypes.ANONYMOUS_HYPERLINK) {
            PsiBuilder.Marker marker = builder.mark();
            builder.advanceLexer();
            marker.done(RestElementTypes.REFERENCE_TARGET);
        } else if (type == RestTokenTypes.LINE) {
            parseLineText(builder, type);
        } else
            builder.advanceLexer();
    }
    rootMarker.done(root);
    return builder.getTreeBuilt();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class XmlAttributeImpl method getDisplayValue.

@Override
public String getDisplayValue() {
    String displayText = myDisplayText;
    if (displayText != null)
        return displayText;
    XmlAttributeValue value = getValueElement();
    if (value == null)
        return null;
    PsiElement firstChild = value.getFirstChild();
    if (firstChild == null)
        return null;
    ASTNode child = firstChild.getNode();
    TextRange valueTextRange = new TextRange(0, value.getTextLength());
    if (child != null && child.getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER) {
        valueTextRange = new TextRange(child.getTextLength(), valueTextRange.getEndOffset());
        child = child.getTreeNext();
    }
    final TIntArrayList gapsStarts = new TIntArrayList();
    final TIntArrayList gapsShifts = new TIntArrayList();
    StringBuilder buffer = new StringBuilder(getTextLength());
    while (child != null) {
        final int start = buffer.length();
        IElementType elementType = child.getElementType();
        if (elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            valueTextRange = new TextRange(valueTextRange.getStartOffset(), child.getTextRange().getStartOffset() - value.getTextRange().getStartOffset());
            break;
        }
        if (elementType == XmlTokenType.XML_CHAR_ENTITY_REF) {
            buffer.append(XmlUtil.getCharFromEntityRef(child.getText()));
        } else if (elementType == XmlElementType.XML_ENTITY_REF) {
            buffer.append(XmlUtil.getEntityValue((XmlEntityRef) child));
        } else {
            appendChildToDisplayValue(buffer, child);
        }
        int end = buffer.length();
        int originalLength = child.getTextLength();
        if (end - start != originalLength) {
            gapsStarts.add(start);
            gapsShifts.add(originalLength - (end - start));
        }
        child = child.getTreeNext();
    }
    int[] gapDisplayStarts = ArrayUtil.newIntArray(gapsShifts.size());
    int[] gapPhysicalStarts = ArrayUtil.newIntArray(gapsShifts.size());
    int currentGapsSum = 0;
    for (int i = 0; i < gapDisplayStarts.length; i++) {
        currentGapsSum += gapsShifts.get(i);
        gapDisplayStarts[i] = gapsStarts.get(i);
        gapPhysicalStarts[i] = gapDisplayStarts[i] + currentGapsSum;
    }
    myGapDisplayStarts = gapDisplayStarts;
    myGapPhysicalStarts = gapPhysicalStarts;
    myValueTextRange = valueTextRange;
    return myDisplayText = buffer.toString();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) TIntArrayList(gnu.trove.TIntArrayList)

Example 39 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class XmlTextImpl method insertText.

@Override
public void insertText(String text, int displayOffset) throws IncorrectOperationException {
    if (text == null || text.isEmpty())
        return;
    final int physicalOffset = displayToPhysical(displayOffset);
    final PsiElement psiElement = findElementAt(physicalOffset);
    //if (!(psiElement instanceof XmlTokenImpl)) throw new IncorrectOperationException("Can't insert at offset: " + displayOffset);
    final IElementType elementType = psiElement != null ? psiElement.getNode().getElementType() : null;
    if (elementType == XmlTokenType.XML_DATA_CHARACTERS) {
        int insertOffset = physicalOffset - psiElement.getStartOffsetInParent();
        final String oldElementText = psiElement.getText();
        final String newElementText = oldElementText.substring(0, insertOffset) + text + oldElementText.substring(insertOffset);
        final PomModel model = PomManager.getModel(getProject());
        final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
        model.runTransaction(new PomTransactionBase(this, aspect) {

            @Override
            public PomModelEvent runInner() {
                final String oldText = getText();
                final ASTNode e = getPolicy().encodeXmlTextContents(newElementText, XmlTextImpl.this);
                final ASTNode node = psiElement.getNode();
                final ASTNode treeNext = node.getTreeNext();
                addChildren(e, null, treeNext);
                deleteChildInternal(node);
                clearCaches();
                return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
            }
        });
    } else {
        setValue(new StringBuffer(getValue()).insert(displayOffset, text).toString());
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent)

Example 40 with IElementType

use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.

the class XmlTextImpl method removeText.

@Override
public void removeText(int displayStart, int displayEnd) throws IncorrectOperationException {
    final String value = getValue();
    final int physicalStart = displayToPhysical(displayStart);
    final PsiElement psiElement = findElementAt(physicalStart);
    if (psiElement != null) {
        final IElementType elementType = psiElement.getNode().getElementType();
        final int elementDisplayEnd = physicalToDisplay(psiElement.getStartOffsetInParent() + psiElement.getTextLength());
        final int elementDisplayStart = physicalToDisplay(psiElement.getStartOffsetInParent());
        if (elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == TokenType.WHITE_SPACE) {
            if (elementDisplayEnd >= displayEnd && elementDisplayStart <= displayStart) {
                int physicalEnd = physicalStart;
                while (physicalEnd < getTextRange().getLength()) {
                    if (physicalToDisplay(physicalEnd) == displayEnd)
                        break;
                    physicalEnd++;
                }
                int removeStart = physicalStart - psiElement.getStartOffsetInParent();
                int removeEnd = physicalEnd - psiElement.getStartOffsetInParent();
                final String oldElementText = psiElement.getText();
                final String newElementText = oldElementText.substring(0, removeStart) + oldElementText.substring(removeEnd);
                final PomModel model = PomManager.getModel(getProject());
                final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
                model.runTransaction(new PomTransactionBase(this, aspect) {

                    @Override
                    public PomModelEvent runInner() throws IncorrectOperationException {
                        final String oldText = getText();
                        if (!newElementText.isEmpty()) {
                            final ASTNode e = getPolicy().encodeXmlTextContents(newElementText, XmlTextImpl.this);
                            replaceChild(psiElement.getNode(), e);
                        } else {
                            psiElement.delete();
                        }
                        clearCaches();
                        return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
                    }
                });
                return;
            }
        }
    }
    if (displayStart == 0 && displayEnd == value.length()) {
        delete();
    } else {
        setValue(new StringBuffer(getValue()).replace(displayStart, displayEnd, "").toString());
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PomModelEvent(com.intellij.pom.event.PomModelEvent)

Aggregations

IElementType (com.intellij.psi.tree.IElementType)843 ASTNode (com.intellij.lang.ASTNode)127 PsiBuilder (com.intellij.lang.PsiBuilder)121 Nullable (org.jetbrains.annotations.Nullable)100 NotNull (org.jetbrains.annotations.NotNull)78 PsiElement (com.intellij.psi.PsiElement)77 TextRange (com.intellij.openapi.util.TextRange)43 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)26 ArrayList (java.util.ArrayList)22 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 Lexer (com.intellij.lexer.Lexer)17 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)15 NonNls (org.jetbrains.annotations.NonNls)14 Document (com.intellij.openapi.editor.Document)13 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)13 LighterASTNode (com.intellij.lang.LighterASTNode)12 BracePair (com.intellij.lang.BracePair)10 Project (com.intellij.openapi.project.Project)9 PsiFile (com.intellij.psi.PsiFile)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9