Search in sources :

Example 31 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class InnerClassesScannerTest method mayContainClassesInside.

private static boolean mayContainClassesInside(PsiElement psiElement, final CharSequence fileBuffer) {
    PsiFile psiFile = psiElement.getContainingFile();
    boolean mayHaveClassesInside = false;
    if (psiFile instanceof PsiJavaFileImpl) {
        Lexer originalLexer = JavaParserDefinition.createLexer(((PsiJavaFileImpl) psiFile).getLanguageLevel());
        FilterLexer lexer = new FilterLexer(originalLexer, new FilterLexer.SetFilter(StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET));
        final TextRange range = psiElement.getTextRange();
        lexer.start(fileBuffer, range.getStartOffset(), range.getEndOffset());
        boolean isInNewExpression = false;
        boolean isRightAfterNewExpression = false;
        int angleLevel = 0;
        int parenLevel = 0;
        do {
            IElementType tokenType = lexer.getTokenType();
            if (tokenType == null)
                break;
            if (tokenType == JavaTokenType.NEW_KEYWORD) {
                isInNewExpression = true;
            } else if (tokenType == JavaTokenType.LPARENTH) {
                if (isInNewExpression)
                    parenLevel++;
            } else if (tokenType == JavaTokenType.LT) {
                if (isInNewExpression)
                    angleLevel++;
            } else if (tokenType == JavaTokenType.GT) {
                if (isInNewExpression)
                    angleLevel--;
            } else if (tokenType == JavaTokenType.RPARENTH) {
                if (isInNewExpression) {
                    parenLevel--;
                    if (parenLevel == 0) {
                        isRightAfterNewExpression = true;
                    }
                }
            } else if (tokenType == JavaTokenType.LBRACE) {
                if (isInNewExpression || isRightAfterNewExpression) {
                    mayHaveClassesInside = true;
                }
            } else if (tokenType == JavaTokenType.LBRACKET) {
                if (parenLevel == 0 && angleLevel == 0)
                    isInNewExpression = false;
            } else if (tokenType == JavaTokenType.INTERFACE_KEYWORD || tokenType == JavaTokenType.CLASS_KEYWORD || tokenType == JavaTokenType.ENUM_KEYWORD) {
                mayHaveClassesInside = true;
            }
            if (isInNewExpression && isRightAfterNewExpression) {
                isInNewExpression = false;
            } else {
                isRightAfterNewExpression = false;
            }
            lexer.advance();
        } while (!mayHaveClassesInside);
    }
    return mayHaveClassesInside;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Lexer(com.intellij.lexer.Lexer) FilterLexer(com.intellij.lexer.FilterLexer) TextRange(com.intellij.openapi.util.TextRange) PsiJavaFileImpl(com.intellij.psi.impl.source.PsiJavaFileImpl) FilterLexer(com.intellij.lexer.FilterLexer)

Example 32 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class IndexPatternSearcher method findCommentTokenRanges.

private static void findCommentTokenRanges(final PsiFile file, final CharSequence chars, final TextRange range, final TIntArrayList commentStarts, final TIntArrayList commentEnds) {
    if (file instanceof PsiPlainTextFile) {
        FileType fType = file.getFileType();
        if (fType instanceof CustomSyntaxTableFileType) {
            Lexer lexer = SyntaxHighlighterFactory.getSyntaxHighlighter(fType, file.getProject(), file.getVirtualFile()).getHighlightingLexer();
            findComments(lexer, chars, range, COMMENT_TOKENS, commentStarts, commentEnds, null);
        } else {
            commentStarts.add(0);
            commentEnds.add(file.getTextLength());
        }
    } else {
        final FileViewProvider viewProvider = file.getViewProvider();
        final Set<Language> relevantLanguages = viewProvider.getLanguages();
        for (Language lang : relevantLanguages) {
            final TIntArrayList commentStartsList = new TIntArrayList();
            final TIntArrayList commentEndsList = new TIntArrayList();
            final SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(lang, file.getProject(), file.getVirtualFile());
            Lexer lexer = syntaxHighlighter.getHighlightingLexer();
            TokenSet commentTokens = null;
            IndexPatternBuilder builderForFile = null;
            for (IndexPatternBuilder builder : Extensions.getExtensions(IndexPatternBuilder.EP_NAME)) {
                Lexer lexerFromBuilder = builder.getIndexingLexer(file);
                if (lexerFromBuilder != null) {
                    lexer = lexerFromBuilder;
                    commentTokens = builder.getCommentTokenSet(file);
                    builderForFile = builder;
                }
            }
            if (builderForFile == null) {
                final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
                if (parserDefinition != null) {
                    commentTokens = parserDefinition.getCommentTokens();
                }
            }
            if (commentTokens != null) {
                findComments(lexer, chars, range, commentTokens, commentStartsList, commentEndsList, builderForFile);
                mergeCommentLists(commentStarts, commentEnds, commentStartsList, commentEndsList);
            }
        }
    }
}
Also used : Lexer(com.intellij.lexer.Lexer) ParserDefinition(com.intellij.lang.ParserDefinition) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) TokenSet(com.intellij.psi.tree.TokenSet) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) TIntArrayList(gnu.trove.TIntArrayList) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)

Example 33 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class JavaParserDefinition method spaceExistanceTypeBetweenTokens.

@Override
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
    if (right.getElementType() == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN || left.getElementType() == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) {
        return SpaceRequirements.MUST_NOT;
    }
    PsiFile containingFile = left.getTreeParent().getPsi().getContainingFile();
    LanguageLevel level = containingFile instanceof PsiJavaFile ? ((PsiJavaFile) containingFile).getLanguageLevel() : LanguageLevel.HIGHEST;
    Lexer lexer = createLexer(level);
    SpaceRequirements spaceRequirements = LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer);
    if (left.getElementType() == JavaTokenType.END_OF_LINE_COMMENT) {
        return SpaceRequirements.MUST_LINE_BREAK;
    }
    if (left.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA) {
        String text = left.getText();
        if (text.length() > 0 && Character.isWhitespace(text.charAt(text.length() - 1))) {
            return SpaceRequirements.MAY;
        }
    }
    if (right.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA) {
        String text = right.getText();
        if (text.length() > 0 && Character.isWhitespace(text.charAt(0))) {
            return SpaceRequirements.MAY;
        }
    } else if (right.getElementType() == JavaDocTokenType.DOC_INLINE_TAG_END) {
        return SpaceRequirements.MAY;
    }
    return spaceRequirements;
}
Also used : Lexer(com.intellij.lexer.Lexer) JavaDocLexer(com.intellij.lang.java.lexer.JavaDocLexer) JavaLexer(com.intellij.lang.java.lexer.JavaLexer) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 34 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class JavaParserUtil method parseFragment.

@Nullable
public static ASTNode parseFragment(final ASTNode chameleon, final ParserWrapper wrapper, final boolean eatAll, final LanguageLevel level) {
    final PsiElement psi = (chameleon.getTreeParent() != null ? chameleon.getTreeParent().getPsi() : chameleon.getPsi());
    assert psi != null : chameleon;
    final Project project = psi.getProject();
    final PsiBuilderFactory factory = PsiBuilderFactory.getInstance();
    final Lexer lexer = chameleon.getElementType() == JavaDocElementType.DOC_COMMENT ? JavaParserDefinition.createDocLexer(level) : JavaParserDefinition.createLexer(level);
    final PsiBuilder builder = factory.createBuilder(project, chameleon, lexer, chameleon.getElementType().getLanguage(), chameleon.getChars());
    setLanguageLevel(builder, level);
    final PsiBuilder.Marker root = builder.mark();
    wrapper.parse(builder);
    if (!builder.eof()) {
        if (!eatAll)
            throw new AssertionError("Unexpected tokens");
        final PsiBuilder.Marker extras = builder.mark();
        while (!builder.eof()) builder.advanceLexer();
        extras.error(JavaErrorMessages.message("unexpected.tokens"));
    }
    root.done(chameleon.getElementType());
    return builder.getTreeBuilt().getFirstChildNode();
}
Also used : Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class MinifiedFilesUtil method isMinified.

/**
     * Finds out whether the file minified by using common (not language-specific) heuristics.
     * Can be used for checking of css/less/scss/sass and js files.
     *
     * @param fileContent              target file content
     * @param parserDefinition         Parser definition of target language
     * @param noWSRequireAfterTokenSet TokenSet of types that doesn't require whitespaces after them.
     * @param stringsTokenSet TokenSet of types considered as string elements
     */
public static boolean isMinified(@NotNull CharSequence fileContent, @NotNull ParserDefinition parserDefinition, @NotNull TokenSet noWSRequireBeforeTokenSet, @NotNull TokenSet noWSRequireAfterTokenSet, @NotNull TokenSet stringsTokenSet) {
    Lexer lexer = parserDefinition.createLexer(null);
    lexer.start(fileContent);
    if (!isMinified(lexer, parserDefinition, noWSRequireBeforeTokenSet, noWSRequireAfterTokenSet, stringsTokenSet)) {
        return false;
    } else if (lexer.getTokenType() == null) {
        // whole file had been considered
        return true;
    }
    int startOffset = fileContent.length() - COUNT_OF_CONSIDERING_CHARACTERS_FROM_END_OF_FILE;
    if (startOffset <= 0) {
        return true;
    }
    lexer.start(fileContent, startOffset, fileContent.length());
    return isMinified(lexer, parserDefinition, noWSRequireBeforeTokenSet, noWSRequireAfterTokenSet, stringsTokenSet);
}
Also used : Lexer(com.intellij.lexer.Lexer)

Aggregations

Lexer (com.intellij.lexer.Lexer)53 IElementType (com.intellij.psi.tree.IElementType)17 Project (com.intellij.openapi.project.Project)11 NotNull (org.jetbrains.annotations.NotNull)9 Nullable (org.jetbrains.annotations.Nullable)9 PsiElement (com.intellij.psi.PsiElement)6 TextRange (com.intellij.openapi.util.TextRange)5 LayeredLexer (com.intellij.lexer.LayeredLexer)4 CfmlLexer (com.intellij.coldFusion.model.lexer.CfmlLexer)3 JavaLexer (com.intellij.lang.java.lexer.JavaLexer)3 FilterLexer (com.intellij.lexer.FilterLexer)3 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)3 LanguageLevel (com.intellij.pom.java.LanguageLevel)3 Language (com.intellij.lang.Language)2 PropertiesLexer (com.intellij.lang.properties.parsing.PropertiesLexer)2 FileType (com.intellij.openapi.fileTypes.FileType)2 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)2 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)2 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)2 PsiFile (com.intellij.psi.PsiFile)2