Search in sources :

Example 86 with IElementType

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

the class XmlBraceMatcher method isLBraceToken.

@Override
public boolean isLBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType) {
    final IElementType tokenType = iterator.getTokenType();
    PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(tokenType.getLanguage());
    if (matcher != null) {
        BracePair[] pairs = matcher.getPairs();
        for (BracePair pair : pairs) {
            if (pair.getLeftBraceType() == tokenType)
                return true;
        }
    }
    return tokenType == XmlTokenType.XML_START_TAG_START || tokenType == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER || tokenType == XmlTokenType.XML_CDATA_START;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PairedBraceMatcher(com.intellij.lang.PairedBraceMatcher) BracePair(com.intellij.lang.BracePair)

Example 87 with IElementType

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

the class XmlBraceMatcher method isRBraceToken.

@Override
public boolean isRBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType) {
    final IElementType tokenType = iterator.getTokenType();
    PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(tokenType.getLanguage());
    if (matcher != null) {
        BracePair[] pairs = matcher.getPairs();
        for (BracePair pair : pairs) {
            if (pair.getRightBraceType() == tokenType)
                return true;
        }
    }
    if (tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END || tokenType == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER || tokenType == XmlTokenType.XML_CDATA_END) {
        return true;
    } else if (tokenType == XmlTokenType.XML_TAG_END) {
        final boolean result = findEndTagStart(iterator);
        if (isFileTypeWithSingleHtmlTags(fileType)) {
            final String tagName = getTagName(fileText, iterator);
            if (tagName != null && HtmlUtil.isSingleHtmlTag(tagName)) {
                return !result;
            }
        }
        return result;
    } else {
        return false;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PairedBraceMatcher(com.intellij.lang.PairedBraceMatcher) BracePair(com.intellij.lang.BracePair)

Example 88 with IElementType

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

the class XmlBraceMatcher method findEndTagStart.

private static boolean findEndTagStart(HighlighterIterator iterator) {
    IElementType tokenType = iterator.getTokenType();
    int balance = 0;
    int count = 0;
    while (balance >= 0) {
        iterator.retreat();
        count++;
        if (iterator.atEnd())
            break;
        tokenType = iterator.getTokenType();
        if (tokenType == XmlTokenType.XML_TAG_END || tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END) {
            balance++;
        } else if (tokenType == XmlTokenType.XML_END_TAG_START || tokenType == XmlTokenType.XML_START_TAG_START) {
            balance--;
        }
    }
    while (count-- > 0) iterator.advance();
    return tokenType == XmlTokenType.XML_END_TAG_START;
}
Also used : IElementType(com.intellij.psi.tree.IElementType)

Example 89 with IElementType

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

the class SyntheticBlock method getSpacing.

@Override
public Spacing getSpacing(Block child1, @NotNull Block child2) {
    if (child1 instanceof ReadOnlyBlock || child2 instanceof ReadOnlyBlock) {
        return Spacing.getReadOnlySpacing();
    }
    if (!(child1 instanceof AbstractXmlBlock) || !(child2 instanceof AbstractXmlBlock)) {
        return null;
    }
    ASTNode node1 = ((AbstractBlock) child1).getNode();
    ASTNode node2 = ((AbstractBlock) child2).getNode();
    IElementType type1 = node1.getElementType();
    IElementType type2 = node2.getElementType();
    if (type2 == XmlElementType.XML_COMMENT) {
        // Do not remove any spaces except extra blank lines
        return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (type1 == XmlElementType.XML_COMMENT) {
        ASTNode prev = node1.getTreePrev();
        if (prev != null) {
            node1 = prev;
            type1 = prev.getElementType();
        }
    }
    boolean firstIsText = isTextFragment(node1);
    boolean secondIsText = isTextFragment(node2);
    if (((AbstractXmlBlock) child1).isPreserveSpace() && ((AbstractXmlBlock) child2).isPreserveSpace()) {
        return Spacing.getReadOnlySpacing();
    }
    if (type1 == XmlTokenType.XML_CDATA_START || type2 == XmlTokenType.XML_CDATA_END) {
        if (myXmlFormattingPolicy.getKeepWhiteSpacesInsideCDATA()) {
            return Spacing.getReadOnlySpacing();
        }
        if (type1 == XmlTokenType.XML_CDATA_START && type2 == XmlTokenType.XML_CDATA_END) {
            return Spacing.createSpacing(0, 0, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        }
        if (type1 == XmlTokenType.XML_CDATA_START && child2 instanceof AnotherLanguageBlockWrapper || type2 == XmlTokenType.XML_CDATA_END && child1 instanceof AnotherLanguageBlockWrapper) {
            return Spacing.createSpacing(0, 0, 1, myXmlFormattingPolicy.getShouldKeepLineBreaks(), 0);
        }
    }
    boolean firstIsTag = node1.getPsi() instanceof XmlTag && !firstIsText;
    boolean secondIsTag = node2.getPsi() instanceof XmlTag && !secondIsText;
    boolean firstIsEntityRef = isEntityRef(node1);
    boolean secondIsEntityRef = isEntityRef(node2);
    if ((secondIsText && isInlineTag(node1) || firstIsText && isInlineTag(node2)) && myXmlFormattingPolicy.isKeepSpacesAroundInlineTags()) {
        return Spacing.getReadOnlySpacing();
    }
    if (isSpaceInText(firstIsTag, secondIsTag, firstIsText, secondIsText) && keepWhiteSpaces()) {
        return Spacing.getReadOnlySpacing();
    }
    if (firstIsEntityRef || secondIsEntityRef) {
        return Spacing.createSafeSpacing(myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (type1 == XmlElementType.XML_ATTRIBUTE && (type2 == XmlTokenType.XML_TAG_END || type2 == XmlTokenType.XML_EMPTY_ELEMENT_END)) {
        final PsiElement psi1 = node1.getPsi();
        if (psi1 instanceof XmlAttribute && myXmlFormattingPolicy.insertLineBreakAfterLastAttribute((XmlAttribute) psi1)) {
            return Spacing.createSpacing(0, 0, 1, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    if (type2 == XmlTokenType.XML_EMPTY_ELEMENT_END && myXmlFormattingPolicy.addSpaceIntoEmptyTag()) {
        return Spacing.createSpacing(1, 1, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (isXmlTagName(type1, type2)) {
        final int spaces = shouldAddSpaceAroundTagName(node1, node2) ? 1 : 0;
        return Spacing.createSpacing(spaces, spaces, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (type2 == XmlElementType.XML_ATTRIBUTE) {
        int minLineFeeds = 0;
        if (type1 == XmlTokenType.XML_NAME) {
            final PsiElement psi2 = node2.getPsi();
            minLineFeeds = psi2 instanceof XmlAttribute && myXmlFormattingPolicy.insertLineBreakBeforeFirstAttribute((XmlAttribute) psi2) ? 1 : 0;
        }
        return Spacing.createSpacing(1, 1, minLineFeeds, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (((AbstractXmlBlock) child1).isTextElement() && ((AbstractXmlBlock) child2).isTextElement()) {
        return Spacing.createSafeSpacing(myXmlFormattingPolicy.getShouldKeepLineBreaksInText(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (firstIsTag && insertLineFeedAfter((XmlTag) node1.getPsi())) {
        return Spacing.createSpacing(0, 0, 1, true, myXmlFormattingPolicy.getKeepBlankLines());
    }
    if ((firstIsText || firstIsTag) && secondIsTag) {
        //<tag/>text <tag/></tag>
        if (((AbstractXmlBlock) child2).insertLineBreakBeforeTag()) {
            return Spacing.createSpacing(0, Integer.MAX_VALUE, ((AbstractXmlBlock) child2).getBlankLinesBeforeTag() + 1, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        } else if (((AbstractXmlBlock) child2).removeLineBreakBeforeTag()) {
            return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    final boolean saveSpacesBetweenTagAndText = myXmlFormattingPolicy.shouldSaveSpacesBetweenTagAndText() && child1.getTextRange().getEndOffset() < child2.getTextRange().getStartOffset();
    if (firstIsTag && secondIsText) {
        if (((AbstractXmlBlock) child1).isTextElement() || saveSpacesBetweenTagAndText) {
            return Spacing.createSafeSpacing(true, myXmlFormattingPolicy.getKeepBlankLines());
        } else {
            return Spacing.createSpacing(0, 0, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    if (firstIsText && secondIsTag) {
        //text-<tag/>
        if (((AbstractXmlBlock) child2).isTextElement() || saveSpacesBetweenTagAndText) {
            return Spacing.createSafeSpacing(true, myXmlFormattingPolicy.getKeepBlankLines());
        } else {
            return Spacing.createSpacing(0, 0, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    if (firstIsTag && secondIsTag) {
        //<tag/><tag/>
        return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
    }
    return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, myXmlFormattingPolicy.getShouldKeepLineBreaksInText(), myXmlFormattingPolicy.getKeepBlankLines());
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) XmlTag(com.intellij.psi.xml.XmlTag)

Example 90 with IElementType

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

the class CodegenAnnotatingVisitor method visitBinaryExpression.

@Override
public void visitBinaryExpression(@NotNull KtBinaryExpression expression) {
    super.visitBinaryExpression(expression);
    DeclarationDescriptor operationDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expression.getOperationReference());
    if (!(operationDescriptor instanceof FunctionDescriptor))
        return;
    FunctionDescriptor original = SamCodegenUtil.getOriginalIfSamAdapter((FunctionDescriptor) operationDescriptor);
    if (original == null)
        return;
    SamType samType = SamType.create(original.getValueParameters().get(0).getType());
    if (samType == null)
        return;
    IElementType token = expression.getOperationToken();
    if (BINARY_OPERATIONS.contains(token)) {
        bindingTrace.record(CodegenBinding.SAM_VALUE, expression.getRight(), samType);
    } else if (token == IN_KEYWORD || token == NOT_IN) {
        bindingTrace.record(CodegenBinding.SAM_VALUE, expression.getLeft(), samType);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType)

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