Search in sources :

Example 21 with TokenSet

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

the class SpacingBuilder method aroundInside.

public RuleBuilder aroundInside(IElementType token, IElementType parent) {
    final TokenSet tokenSet = TokenSet.create(token);
    RuleCondition before = new RuleCondition(TokenSet.create(parent), null, tokenSet);
    RuleCondition after = new RuleCondition(TokenSet.create(parent), tokenSet, null);
    return new RuleBuilder(before, after);
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet)

Example 22 with TokenSet

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

the class AbstractXmlBlock method isComment.

private static boolean isComment(final ASTNode node) {
    final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(node);
    if (psiElement instanceof PsiComment)
        return true;
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(psiElement.getLanguage());
    if (parserDefinition == null)
        return false;
    final TokenSet commentTokens = parserDefinition.getCommentTokens();
    return commentTokens.contains(node.getElementType());
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet)

Example 23 with TokenSet

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

the class PyUnconditionalStatementPartFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyElement psiElement) throws IncorrectOperationException {
    if (PyUtil.instanceOf(psiElement, PyElsePart.class, PyTryPart.class, PyFinallyPart.class)) {
        final PsiElement colon = PyPsiUtils.getFirstChildOfType(psiElement, PyTokenTypes.COLON);
        if (colon == null) {
            final TokenSet keywords = TokenSet.create(PyTokenTypes.ELSE_KEYWORD, PyTokenTypes.TRY_KEYWORD, PyTokenTypes.FINALLY_KEYWORD);
            final PsiElement keywordToken = PyPsiUtils.getChildByFilter(psiElement, keywords, 0);
            editor.getDocument().insertString(sure(keywordToken).getTextRange().getEndOffset(), ":");
        }
    }
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet) PsiElement(com.intellij.psi.PsiElement)

Example 24 with TokenSet

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

the class KotlinParsing method parsePropertyGetterOrSetter.

/*
     * getterOrSetter
     *   : modifiers ("get" | "set")
     *   :
     *        (     "get" "(" ")"
     *           |
     *              "set" "(" modifiers parameter ")"
     *        ) functionBody
     *   ;
     */
@Nullable
private AccessorKind parsePropertyGetterOrSetter(@Nullable AccessorKind notAllowedKind) {
    PsiBuilder.Marker getterOrSetter = mark();
    parseModifierList(DEFAULT, TokenSet.EMPTY);
    AccessorKind accessorKind;
    if (at(GET_KEYWORD)) {
        accessorKind = AccessorKind.GET;
    } else if (at(SET_KEYWORD)) {
        accessorKind = AccessorKind.SET;
    } else {
        getterOrSetter.rollbackTo();
        return null;
    }
    if (accessorKind == notAllowedKind) {
        getterOrSetter.rollbackTo();
        return null;
    }
    // GET_KEYWORD or SET_KEYWORD
    advance();
    if (!at(LPAR)) {
        // Account for Jet-114 (val a : int get {...})
        TokenSet ACCESSOR_FIRST_OR_PROPERTY_END = TokenSet.orSet(MODIFIER_KEYWORDS, TokenSet.create(AT, GET_KEYWORD, SET_KEYWORD, EOL_OR_SEMICOLON, RBRACE));
        if (!atSet(ACCESSOR_FIRST_OR_PROPERTY_END)) {
            errorUntil("Accessor body expected", TokenSet.orSet(ACCESSOR_FIRST_OR_PROPERTY_END, TokenSet.create(LBRACE, LPAR, EQ)));
        } else {
            closeDeclarationWithCommentBinders(getterOrSetter, PROPERTY_ACCESSOR, false);
            return accessorKind;
        }
    }
    myBuilder.disableNewlines();
    expect(LPAR, "Expecting '('", TokenSet.create(RPAR, IDENTIFIER, COLON, LBRACE, EQ));
    if (accessorKind == AccessorKind.SET) {
        PsiBuilder.Marker parameterList = mark();
        PsiBuilder.Marker setterParameter = mark();
        parseModifierList(DEFAULT, TokenSet.create(COMMA, COLON, RPAR));
        expect(IDENTIFIER, "Expecting parameter name", TokenSet.create(RPAR, COLON, LBRACE, EQ));
        if (at(COLON)) {
            // COLON
            advance();
            parseTypeRef();
        }
        setterParameter.done(VALUE_PARAMETER);
        parameterList.done(VALUE_PARAMETER_LIST);
    }
    if (!at(RPAR)) {
        errorUntil("Expecting ')'", TokenSet.create(RPAR, COLON, LBRACE, RBRACE, EQ, EOL_OR_SEMICOLON));
    }
    if (at(RPAR)) {
        advance();
    }
    myBuilder.restoreNewlinesState();
    if (at(COLON)) {
        advance();
        parseTypeRef();
    }
    parseFunctionBody();
    closeDeclarationWithCommentBinders(getterOrSetter, PROPERTY_ACCESSOR, false);
    return accessorKind;
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with TokenSet

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

the class KotlinExpressionParsing method parseTry.

/*
     * try
     *   : "try" block catchBlock* finallyBlock?
     *   ;
     * catchBlock
     *   : "catch" "(" annotations SimpleName ":" userType ")" block
     *   ;
     *
     * finallyBlock
     *   : "finally" block
     *   ;
     */
private void parseTry() {
    assert _at(TRY_KEYWORD);
    PsiBuilder.Marker tryExpression = mark();
    // TRY_KEYWORD
    advance();
    myKotlinParsing.parseBlock();
    boolean catchOrFinally = false;
    while (at(CATCH_KEYWORD)) {
        catchOrFinally = true;
        PsiBuilder.Marker catchBlock = mark();
        // CATCH_KEYWORD
        advance();
        TokenSet recoverySet = TokenSet.create(LBRACE, RBRACE, FINALLY_KEYWORD, CATCH_KEYWORD);
        if (atSet(recoverySet)) {
            error("Expecting exception variable declaration");
        } else {
            PsiBuilder.Marker parameters = mark();
            expect(LPAR, "Expecting '('", recoverySet);
            if (!atSet(recoverySet)) {
                myKotlinParsing.parseValueParameter(/*typeRequired = */
                true);
                expect(RPAR, "Expecting ')'", recoverySet);
            } else {
                error("Expecting exception variable declaration");
            }
            parameters.done(VALUE_PARAMETER_LIST);
        }
        if (at(LBRACE)) {
            myKotlinParsing.parseBlock();
        } else {
            error("Expecting a block: { ... }");
        }
        catchBlock.done(CATCH);
    }
    if (at(FINALLY_KEYWORD)) {
        catchOrFinally = true;
        PsiBuilder.Marker finallyBlock = mark();
        // FINALLY_KEYWORD
        advance();
        myKotlinParsing.parseBlock();
        finallyBlock.done(FINALLY);
    }
    if (!catchOrFinally) {
        error("Expecting 'catch' or 'finally'");
    }
    tryExpression.done(TRY);
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet) PsiBuilder(com.intellij.lang.PsiBuilder)

Aggregations

TokenSet (com.intellij.psi.tree.TokenSet)27 ParserDefinition (com.intellij.lang.ParserDefinition)6 PsiBuilder (com.intellij.lang.PsiBuilder)6 IElementType (com.intellij.psi.tree.IElementType)6 Language (com.intellij.lang.Language)4 ASTNode (com.intellij.lang.ASTNode)3 PsiElement (com.intellij.psi.PsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 Lexer (com.intellij.lexer.Lexer)2 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)2 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)2 Document (com.intellij.openapi.editor.Document)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 FileType (com.intellij.openapi.fileTypes.FileType)1 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)1 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1 Computable (com.intellij.openapi.util.Computable)1 TextRange (com.intellij.openapi.util.TextRange)1