Search in sources :

Example 46 with IElementType

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

the class GroovySpacingProcessorBasic method getSpacing.

public static Spacing getSpacing(GroovyBlock child1, GroovyBlock child2, FormattingContext context) {
    ASTNode leftNode = child1.getNode();
    ASTNode rightNode = child2.getNode();
    final PsiElement left = leftNode.getPsi();
    final PsiElement right = rightNode.getPsi();
    IElementType leftType = leftNode.getElementType();
    IElementType rightType = rightNode.getElementType();
    final CommonCodeStyleSettings settings = context.getSettings();
    final GroovyCodeStyleSettings groovySettings = context.getGroovySettings();
    if (!(mirrorsAst(child1) && mirrorsAst(child2))) {
        return NO_SPACING;
    }
    if (child2 instanceof ClosureBodyBlock) {
        return settings.SPACE_WITHIN_BRACES ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (child1 instanceof ClosureBodyBlock) {
        return createDependentSpacingForClosure(settings, groovySettings, (GrClosableBlock) left.getParent(), false);
    }
    if (leftType == GroovyDocElementTypes.GROOVY_DOC_COMMENT) {
        return COMMON_SPACING_WITH_NL;
    }
    if (right instanceof GrTypeArgumentList) {
        return NO_SPACING_WITH_NEWLINE;
    }
    /********** punctuation marks ************/
    if (GroovyTokenTypes.mCOMMA == leftType) {
        return settings.SPACE_AFTER_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mCOMMA == rightType) {
        return settings.SPACE_BEFORE_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == leftType) {
        return settings.SPACE_AFTER_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == rightType) {
        return settings.SPACE_BEFORE_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    // For dots, commas etc.
    if ((TokenSets.DOTS.contains(rightType)) || (GroovyTokenTypes.mCOLON.equals(rightType) && !(right.getParent() instanceof GrConditionalExpression))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (TokenSets.DOTS.contains(leftType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    //todo:check it for multiple assignments
    if ((GroovyElementTypes.VARIABLE_DEFINITION.equals(leftType) || GroovyElementTypes.VARIABLE_DEFINITION.equals(rightType)) && !(leftNode.getTreeNext() instanceof PsiErrorElement)) {
        return Spacing.createSpacing(0, 0, 1, false, 100);
    }
    // For regexes
    if (leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mREGEX_LITERAL || leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        return NO_SPACING;
    }
    // For << and >> ...
    if ((GroovyTokenTypes.mLT.equals(leftType) && GroovyTokenTypes.mLT.equals(rightType)) || (GroovyTokenTypes.mGT.equals(leftType) && GroovyTokenTypes.mGT.equals(rightType))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    // Unary and postfix expressions
    if (SpacingTokens.PREFIXES.contains(leftType) || SpacingTokens.POSTFIXES.contains(rightType) || (SpacingTokens.PREFIXES_OPTIONAL.contains(leftType) && left.getParent() instanceof GrUnaryExpression)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (SpacingTokens.RANGES.contains(leftType) || SpacingTokens.RANGES.contains(rightType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyDocTokenTypes.mGDOC_ASTERISKS == leftType && GroovyDocTokenTypes.mGDOC_COMMENT_DATA == rightType) {
        String text = rightNode.getText();
        if (!text.isEmpty() && !StringUtil.startsWithChar(text, ' ')) {
            return COMMON_SPACING;
        }
        return NO_SPACING;
    }
    if (leftType == GroovyDocTokenTypes.mGDOC_TAG_VALUE_TOKEN && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) {
        return LAZY_SPACING;
    }
    if (left instanceof GrStatement && right instanceof GrStatement && left.getParent() instanceof GrStatementOwner && right.getParent() instanceof GrStatementOwner) {
        return COMMON_SPACING_WITH_NL;
    }
    if (rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END) {
        return NO_SPACING;
    }
    if ((leftType == GroovyDocElementTypes.GDOC_INLINED_TAG && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) || (leftType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA && rightType == GroovyDocElementTypes.GDOC_INLINED_TAG)) {
        // Keep formatting between groovy doc text and groovy doc reference tag as is.
        return NO_SPACING;
    }
    if (leftType == GroovyElementTypes.CLASS_TYPE_ELEMENT && rightType == GroovyTokenTypes.mTRIPLE_DOT) {
        return NO_SPACING;
    }
    // diamonds
    if (rightType == GroovyTokenTypes.mLT || rightType == GroovyTokenTypes.mGT) {
        if (right.getParent() instanceof GrCodeReferenceElement) {
            PsiElement p = right.getParent().getParent();
            if (p instanceof GrNewExpression || p instanceof GrAnonymousClassDefinition) {
                return NO_SPACING;
            }
        }
    }
    return COMMON_SPACING;
}
Also used : GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IElementType(com.intellij.psi.tree.IElementType) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) PsiErrorElement(com.intellij.psi.PsiErrorElement) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) ClosureBodyBlock(org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) PsiElement(com.intellij.psi.PsiElement)

Example 47 with IElementType

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

the class XPathParserDefinition method createElement.

@NotNull
public final PsiElement createElement(ASTNode node) {
    final IElementType type = node.getElementType();
    final PsiElement element = createElement(type, node);
    if (element != null) {
        return element;
    }
    return new XPathTokenImpl(node);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with IElementType

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

the class XPath2Parser method parseSequenceType.

private boolean parseSequenceType(PsiBuilder builder) {
    PsiBuilder.Marker mark = builder.mark();
    final IElementType type = parseItemOrEmptySequenceType(builder);
    if (type != null) {
        if (type == XPath2TokenTypes.ITEM) {
            if (XPath2TokenTypes.OCCURRENCE_OPS.contains(builder.getTokenType())) {
                makeToken(builder);
            }
        }
        mark.done(XPath2ElementTypes.SEQUENCE_TYPE);
        return true;
    } else if (parseNodeType(builder) || parseQName(builder)) {
        if (builder.getTokenType() == XPathTokenTypes.MULT) {
            builder.remapCurrentToken(XPathTokenTypes.STAR);
        }
        if (XPath2TokenTypes.OCCURRENCE_OPS.contains(builder.getTokenType())) {
            makeToken(builder);
        }
        mark.done(XPath2ElementTypes.SEQUENCE_TYPE);
        return true;
    }
    mark.drop();
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 49 with IElementType

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

the class XPathParser method parseAxisSpecifier.

/**
   * [5]    AxisSpecifier    ::=    AxisName '::' | AbbreviatedAxisSpecifier
   * <p/>
   * [13]   AbbreviatedAxisSpecifier    ::=    '@'?
   */
private boolean parseAxisSpecifier(PsiBuilder builder) {
    final PsiBuilder.Marker marker = builder.mark();
    final IElementType tokenType = builder.getTokenType();
    if (XPathTokenTypes.AXIS.contains(tokenType)) {
        builder.advanceLexer();
        checkMatches(builder, XPathTokenTypes.COLCOL, ":: expected");
    } else {
        if (tokenType == XPathTokenTypes.AT) {
            builder.advanceLexer();
        } else if (tokenType == XPathTokenTypes.DOT || tokenType == XPathTokenTypes.DOTDOT) {
            marker.drop();
            return false;
        } else {
            final PsiBuilder.Marker m = builder.mark();
            final boolean b = parseNodeTest(builder);
            m.rollbackTo();
            if (!b) {
                marker.drop();
                return false;
            }
        }
    }
    marker.done(XPathElementTypes.AXIS_SPECIFIER);
    return true;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 50 with IElementType

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

the class PyStringLiteralExpressionImpl method getType.

public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
    final List<ASTNode> nodes = getStringNodes();
    if (nodes.size() > 0) {
        String text = getStringNodes().get(0).getText();
        PyFile file = PsiTreeUtil.getParentOfType(this, PyFile.class);
        if (file != null) {
            IElementType type = PythonHighlightingLexer.convertStringType(getStringNodes().get(0).getElementType(), text, LanguageLevel.forElement(this), file.hasImportFromFuture(FutureFeature.UNICODE_LITERALS));
            if (PyTokenTypes.UNICODE_NODES.contains(type)) {
                return PyBuiltinCache.getInstance(this).getUnicodeType(LanguageLevel.forElement(this));
            }
        }
    }
    return PyBuiltinCache.getInstance(this).getBytesType(LanguageLevel.forElement(this));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode)

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