Search in sources :

Example 41 with PsiFileImpl

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

the class BlockSupportImpl method reparseRange.

@Override
@NotNull
public DiffLog reparseRange(@NotNull final PsiFile file, @NotNull FileASTNode oldFileNode, @NotNull TextRange changedPsiRange, @NotNull final CharSequence newFileText, @NotNull final ProgressIndicator indicator, @NotNull CharSequence lastCommittedText) {
    final PsiFileImpl fileImpl = (PsiFileImpl) file;
    final Couple<ASTNode> reparseableRoots = findReparseableRoots(fileImpl, oldFileNode, changedPsiRange, newFileText);
    return reparseableRoots != null ? mergeTrees(fileImpl, reparseableRoots.first, reparseableRoots.second, indicator, lastCommittedText) : makeFullParse(fileImpl, oldFileNode, newFileText, indicator, lastCommittedText);
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileASTNode(com.intellij.lang.FileASTNode) ASTNode(com.intellij.lang.ASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with PsiFileImpl

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

the class StubProcessingHelperBase method processStubsInFile.

public <Psi extends PsiElement> boolean processStubsInFile(@NotNull final Project project, @NotNull final VirtualFile file, @NotNull StubIdList value, @NotNull final Processor<? super Psi> processor, @NotNull Class<Psi> requiredClass, final boolean skipOnErrors) {
    StubTree stubTree = null;
    PsiFile candidatePsiFile = PsiManager.getInstance(project).findFile(file);
    PsiFileWithStubSupport psiFile = null;
    boolean customStubs = false;
    if (candidatePsiFile != null && !(candidatePsiFile instanceof PsiPlainTextFile)) {
        final FileViewProvider viewProvider = candidatePsiFile.getViewProvider();
        final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
        if (stubBindingRoot instanceof PsiFileWithStubSupport) {
            psiFile = (PsiFileWithStubSupport) stubBindingRoot;
            stubTree = psiFile.getStubTree();
            if (stubTree == null && psiFile instanceof PsiFileImpl) {
                IStubFileElementType elementType = ((PsiFileImpl) psiFile).getElementTypeForStubBuilder();
                if (elementType != null) {
                    stubTree = ((PsiFileImpl) psiFile).calcStubTree();
                } else {
                    customStubs = true;
                    if (BinaryFileStubBuilders.INSTANCE.forFileType(psiFile.getFileType()) == null) {
                        LOG.error("unable to get stub builder for " + psiFile.getFileType() + ", " + StubTreeLoader.getFileViewProviderMismatchDiagnostics(viewProvider));
                    }
                }
            }
        }
    }
    if (stubTree == null && psiFile == null) {
        return true;
    }
    if (stubTree == null) {
        ObjectStubTree objectStubTree = StubTreeLoader.getInstance().readFromVFile(project, file);
        if (objectStubTree == null) {
            return true;
        }
        if (customStubs && !(objectStubTree instanceof StubTree)) {
            if (!skipOnErrors && !requiredClass.isInstance(psiFile)) {
                inconsistencyDetected(objectStubTree, psiFile);
                return true;
            }
            // e.g. dom indices
            return processor.process((Psi) psiFile);
        }
        stubTree = (StubTree) objectStubTree;
        final List<StubElement<?>> plained = stubTree.getPlainListFromAllRoots();
        for (int i = 0, size = value.size(); i < size; i++) {
            final int stubTreeIndex = value.get(i);
            if (stubTreeIndex >= plained.size()) {
                if (!skipOnErrors)
                    onInternalError(file);
                break;
            }
            final StubElement<?> stub = plained.get(stubTreeIndex);
            PsiUtilCore.ensureValid(psiFile);
            final ASTNode tree = psiFile.findTreeForStub(stubTree, stub);
            if (tree != null) {
                if (tree.getElementType() == stubType(stub)) {
                    Psi psi = (Psi) tree.getPsi();
                    PsiUtilCore.ensureValid(psi);
                    if (!skipOnErrors && !requiredClass.isInstance(psi)) {
                        inconsistencyDetected(stubTree, psiFile);
                        break;
                    }
                    if (!processor.process(psi))
                        return false;
                } else if (!skipOnErrors) {
                    String persistedStubTree = ((PsiFileStubImpl) stubTree.getRoot()).printTree();
                    String stubTreeJustBuilt = ((PsiFileStubImpl) ((PsiFileImpl) psiFile).getElementTypeForStubBuilder().getBuilder().buildStubTree(psiFile)).printTree();
                    StringBuilder builder = new StringBuilder();
                    builder.append("Oops\n");
                    builder.append("Recorded stub:-----------------------------------\n");
                    builder.append(persistedStubTree);
                    builder.append("\nAST built stub: ------------------------------------\n");
                    builder.append(stubTreeJustBuilt);
                    builder.append("\n");
                    LOG.info(builder.toString());
                    onInternalError(file);
                }
            }
        }
    } else {
        final List<StubElement<?>> plained = stubTree.getPlainListFromAllRoots();
        for (int i = 0, size = value.size(); i < size; i++) {
            final int stubTreeIndex = value.get(i);
            if (stubTreeIndex >= plained.size()) {
                if (!skipOnErrors) {
                    inconsistencyDetected(stubTree, psiFile);
                }
                break;
            }
            Psi psi = (Psi) plained.get(stubTreeIndex).getPsi();
            PsiUtilCore.ensureValid(psi);
            if (!skipOnErrors && !requiredClass.isInstance(psi)) {
                inconsistencyDetected(stubTree, psiFile);
                break;
            }
            if (!processor.process(psi))
                return false;
        }
    }
    return true;
}
Also used : IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFileWithStubSupport(com.intellij.psi.impl.source.PsiFileWithStubSupport) ASTNode(com.intellij.lang.ASTNode)

Example 43 with PsiFileImpl

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

the class LowLevelSearchUtil method diagnoseInvalidRange.

private static void diagnoseInvalidRange(@NotNull PsiElement scope, PsiFile file, FileViewProvider viewProvider, CharSequence buffer, TextRange range) {
    String msg = "Range for element: '" + scope + "' = " + range + " is out of file '" + file + "' range: " + file.getTextRange();
    msg += "; file contents length: " + buffer.length();
    msg += "\n file provider: " + viewProvider;
    Document document = viewProvider.getDocument();
    if (document != null) {
        msg += "\n committed=" + PsiDocumentManager.getInstance(file.getProject()).isCommitted(document);
    }
    for (Language language : viewProvider.getLanguages()) {
        final PsiFile root = viewProvider.getPsi(language);
        msg += "\n root " + language + " length=" + root.getTextLength() + (root instanceof PsiFileImpl ? "; contentsLoaded=" + ((PsiFileImpl) root).isContentsLoaded() : "");
    }
    LOG.error(msg);
}
Also used : Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document)

Example 44 with PsiFileImpl

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

the class PatternCompiler method compilePattern.

public static CompiledPattern compilePattern(final Project project, final MatchOptions options) throws MalformedPatternException, UnsupportedOperationException {
    FileType fileType = options.getFileType();
    assert fileType instanceof LanguageFileType;
    Language language = ((LanguageFileType) fileType).getLanguage();
    StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
    assert profile != null;
    CompiledPattern result = profile.createCompiledPattern();
    final String[] prefixes = result.getTypedVarPrefixes();
    assert prefixes.length > 0;
    final CompileContext context = new CompileContext();
    if (ApplicationManager.getApplication().isUnitTestMode())
        lastTestingContext = context;
    try {
        context.init(result, options, project, options.getScope() instanceof GlobalSearchScope);
        List<PsiElement> elements = compileByAllPrefixes(project, options, result, context, prefixes);
        final CompiledPattern pattern = context.getPattern();
        checkForUnknownVariables(pattern, elements);
        pattern.setNodes(elements);
        if (context.getSearchHelper().doOptimizing() && context.getSearchHelper().isScannedSomething()) {
            final Set<PsiFile> set = context.getSearchHelper().getFilesSetToScan();
            final List<PsiFile> filesToScan = new ArrayList<>(set.size());
            final GlobalSearchScope scope = (GlobalSearchScope) options.getScope();
            for (final PsiFile file : set) {
                if (!scope.contains(file.getVirtualFile())) {
                    continue;
                }
                if (file instanceof PsiFileImpl) {
                    ((PsiFileImpl) file).clearCaches();
                }
                filesToScan.add(file);
            }
            if (filesToScan.size() == 0) {
                throw new MalformedPatternException(SSRBundle.message("ssr.will.not.find.anything"));
            }
            result.setScope(new LocalSearchScope(PsiUtilCore.toPsiElementArray(filesToScan)));
        }
    } finally {
        context.clear();
    }
    return result;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) TIntArrayList(gnu.trove.TIntArrayList) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern)

Example 45 with PsiFileImpl

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

the class AnchorRepository method restoreFromStubIndex.

private static PsiElement restoreFromStubIndex(PsiFileWithStubSupport fileImpl, int index) {
    StubTree tree = fileImpl.getStubTree();
    if (tree == null) {
        if (fileImpl instanceof PsiFileImpl) {
            tree = ((PsiFileImpl) fileImpl).calcStubTree();
        } else {
            return null;
        }
    }
    List<StubElement<?>> list = tree.getPlainList();
    if (index >= list.size()) {
        return null;
    }
    return ((StubElement) list.get(index)).getPsi();
}
Also used : StubTree(com.intellij.psi.stubs.StubTree) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) StubElement(com.intellij.psi.stubs.StubElement)

Aggregations

PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)53 Document (com.intellij.openapi.editor.Document)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 NotNull (org.jetbrains.annotations.NotNull)8 PsiFile (com.intellij.psi.PsiFile)7 Language (com.intellij.lang.Language)6 Nullable (org.jetbrains.annotations.Nullable)6 FileType (com.intellij.openapi.fileTypes.FileType)5 StubTree (com.intellij.psi.stubs.StubTree)5 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)3 ASTNode (com.intellij.lang.ASTNode)3 ParserDefinition (com.intellij.lang.ParserDefinition)3 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)3 Project (com.intellij.openapi.project.Project)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 StubElement (com.intellij.psi.stubs.StubElement)3 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 FileASTNode (com.intellij.lang.FileASTNode)2