Search in sources :

Example 1 with PsiJavaFileImpl

use of com.intellij.psi.impl.source.PsiJavaFileImpl in project intellij-community by JetBrains.

the class JavaParsingTestCase method createPsiFile.

private PsiFile createPsiFile(final String name, final String text, final TestParser parser) {
    if (TEST_FILE_ELEMENT_TYPE == null) {
        TEST_FILE_ELEMENT_TYPE = new MyIFileElementType();
    }
    TEST_PARSER = parser;
    final LightVirtualFile virtualFile = new LightVirtualFile(name + '.' + myFileExt, StdFileTypes.JAVA, text, -1);
    final FileViewProvider viewProvider = new SingleRootFileViewProvider(PsiManager.getInstance(getProject()), virtualFile, true);
    return new PsiJavaFileImpl(viewProvider) {

        @NotNull
        @Override
        protected FileElement createFileElement(@NotNull final CharSequence text) {
            return new FileElement(TEST_FILE_ELEMENT_TYPE, text);
        }
    };
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) SingleRootFileViewProvider(com.intellij.psi.SingleRootFileViewProvider) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileElement(com.intellij.psi.impl.source.tree.FileElement) SingleRootFileViewProvider(com.intellij.psi.SingleRootFileViewProvider) PsiJavaFileImpl(com.intellij.psi.impl.source.PsiJavaFileImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiJavaFileImpl

use of com.intellij.psi.impl.source.PsiJavaFileImpl 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)

Aggregations

PsiJavaFileImpl (com.intellij.psi.impl.source.PsiJavaFileImpl)2 FilterLexer (com.intellij.lexer.FilterLexer)1 Lexer (com.intellij.lexer.Lexer)1 TextRange (com.intellij.openapi.util.TextRange)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)1 FileElement (com.intellij.psi.impl.source.tree.FileElement)1 IElementType (com.intellij.psi.tree.IElementType)1 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)1 NotNull (org.jetbrains.annotations.NotNull)1