Search in sources :

Example 16 with PsiFileImpl

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

the class StubTreeBuilder method buildStubTree.

@Nullable
public static Stub buildStubTree(final FileContent inputData) {
    Stub data = inputData.getUserData(stubElementKey);
    if (data != null)
        return data;
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (inputData) {
        data = inputData.getUserData(stubElementKey);
        if (data != null)
            return data;
        final FileType fileType = inputData.getFileType();
        final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
        if (builder != null) {
            data = builder.buildStubTree(inputData);
            if (data instanceof PsiFileStubImpl && !((PsiFileStubImpl) data).rootsAreSet()) {
                ((PsiFileStubImpl) data).setStubRoots(new PsiFileStub[] { (PsiFileStubImpl) data });
            }
        } else {
            CharSequence contentAsText = inputData.getContentAsText();
            FileContentImpl fileContent = (FileContentImpl) inputData;
            PsiFile psi = fileContent.getPsiFileForPsiDependentIndex();
            final FileViewProvider viewProvider = psi.getViewProvider();
            psi = viewProvider.getStubBindingRoot();
            psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, contentAsText);
            // if we load AST, it should be easily gc-able. See PsiFileImpl.createTreeElementPointer()
            psi.getManager().startBatchFilesProcessingMode();
            try {
                IStubFileElementType stubFileElementType = ((PsiFileImpl) psi).getElementTypeForStubBuilder();
                if (stubFileElementType != null) {
                    final StubBuilder stubBuilder = stubFileElementType.getBuilder();
                    if (stubBuilder instanceof LightStubBuilder) {
                        LightStubBuilder.FORCED_AST.set(fileContent.getLighterASTForPsiDependentIndex());
                    }
                    data = stubBuilder.buildStubTree(psi);
                    final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = getStubbedRoots(viewProvider);
                    final List<PsiFileStub> stubs = new ArrayList<>(stubbedRoots.size());
                    stubs.add((PsiFileStub) data);
                    for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) {
                        final PsiFile secondaryPsi = stubbedRoot.second;
                        if (psi == secondaryPsi)
                            continue;
                        final StubBuilder stubbedRootBuilder = stubbedRoot.first.getBuilder();
                        if (stubbedRootBuilder instanceof LightStubBuilder) {
                            LightStubBuilder.FORCED_AST.set(new TreeBackedLighterAST(secondaryPsi.getNode()));
                        }
                        final StubElement element = stubbedRootBuilder.buildStubTree(secondaryPsi);
                        if (element instanceof PsiFileStub) {
                            stubs.add((PsiFileStub) element);
                        }
                    }
                    final PsiFileStub[] stubsArray = stubs.toArray(new PsiFileStub[stubs.size()]);
                    for (PsiFileStub stub : stubsArray) {
                        if (stub instanceof PsiFileStubImpl) {
                            ((PsiFileStubImpl) stub).setStubRoots(stubsArray);
                        }
                    }
                }
            } finally {
                psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, null);
                psi.getManager().finishBatchFilesProcessingMode();
            }
        }
        inputData.putUserData(stubElementKey, data);
        return data;
    }
}
Also used : IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) ArrayList(java.util.ArrayList) StubBuilder(com.intellij.psi.StubBuilder) FileViewProvider(com.intellij.psi.FileViewProvider) FileType(com.intellij.openapi.fileTypes.FileType) TreeBackedLighterAST(com.intellij.lang.TreeBackedLighterAST) PsiFile(com.intellij.psi.PsiFile) FileContentImpl(com.intellij.util.indexing.FileContentImpl) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with PsiFileImpl

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

the class BlockSupportImpl method makeFullParse.

@NotNull
private static DiffLog makeFullParse(@NotNull PsiFileImpl fileImpl, @NotNull FileASTNode oldFileNode, @NotNull CharSequence newFileText, @NotNull ProgressIndicator indicator, @NotNull CharSequence lastCommittedText) {
    if (fileImpl instanceof PsiCodeFragment) {
        FileElement parent = fileImpl.getTreeElement();
        final FileElement holderElement = new DummyHolder(fileImpl.getManager(), fileImpl.getContext()).getTreeElement();
        holderElement.rawAddChildren(fileImpl.createContentLeafElement(holderElement.getCharTable().intern(newFileText, 0, newFileText.length())));
        DiffLog diffLog = new DiffLog();
        diffLog.appendReplaceFileElement(parent, (FileElement) holderElement.getFirstChildNode());
        return diffLog;
    } else {
        FileViewProvider viewProvider = fileImpl.getViewProvider();
        viewProvider.getLanguages();
        FileType fileType = viewProvider.getVirtualFile().getFileType();
        String fileName = fileImpl.getName();
        final LightVirtualFile lightFile = new LightVirtualFile(fileName, fileType, newFileText, viewProvider.getVirtualFile().getCharset(), fileImpl.getViewProvider().getModificationStamp());
        lightFile.setOriginalFile(viewProvider.getVirtualFile());
        FileViewProvider copy = viewProvider.createCopy(lightFile);
        if (copy.isEventSystemEnabled()) {
            throw new AssertionError("Copied view provider must be non-physical for reparse to deliver correct events: " + viewProvider);
        }
        copy.getLanguages();
        // optimization: do not convert file contents to bytes to determine if we should codeinsight it
        SingleRootFileViewProvider.doNotCheckFileSizeLimit(lightFile);
        PsiFileImpl newFile = getFileCopy(fileImpl, copy);
        newFile.setOriginalFile(fileImpl);
        final FileElement newFileElement = (FileElement) newFile.getNode();
        final FileElement oldFileElement = (FileElement) oldFileNode;
        if (!lastCommittedText.toString().equals(oldFileElement.getText())) {
            throw new IncorrectOperationException(viewProvider.toString());
        }
        DiffLog diffLog = mergeTrees(fileImpl, oldFileElement, newFileElement, indicator, lastCommittedText);
        ((PsiManagerEx) fileImpl.getManager()).getFileManager().setViewProvider(lightFile, null);
        return diffLog;
    }
}
Also used : DummyHolder(com.intellij.psi.impl.source.DummyHolder) FileType(com.intellij.openapi.fileTypes.FileType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with PsiFileImpl

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

the class BlockSupportImpl method getFileCopy.

@NotNull
public static PsiFileImpl getFileCopy(@NotNull PsiFileImpl originalFile, @NotNull FileViewProvider providerCopy) {
    FileViewProvider viewProvider = originalFile.getViewProvider();
    Language language = originalFile.getLanguage();
    PsiFile file = providerCopy.getPsi(language);
    if (file != null && !(file instanceof PsiFileImpl)) {
        throw new RuntimeException("View provider " + viewProvider + " refused to provide PsiFileImpl for " + language + details(providerCopy, viewProvider));
    }
    PsiFileImpl newFile = (PsiFileImpl) file;
    if (newFile == null && language == PlainTextLanguage.INSTANCE && originalFile == viewProvider.getPsi(viewProvider.getBaseLanguage())) {
        newFile = (PsiFileImpl) providerCopy.getPsi(providerCopy.getBaseLanguage());
    }
    if (newFile == null) {
        throw new RuntimeException("View provider " + viewProvider + " refused to parse text with " + language + details(providerCopy, viewProvider));
    }
    return newFile;
}
Also used : Language(com.intellij.lang.Language) PlainTextLanguage(com.intellij.openapi.fileTypes.PlainTextLanguage) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with PsiFileImpl

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

the class AstPath method getNodePath.

@Nullable
public static AstPath getNodePath(@NotNull CompositeElement node) {
    if (node instanceof FileElement) {
        PsiElement psi = node.getCachedPsi();
        if (!(psi instanceof PsiFileImpl))
            return null;
        PsiFileImpl file = (PsiFileImpl) psi;
        if (!(file.getVirtualFile() instanceof VirtualFileWithId) || file.getElementTypeForStubBuilder() == null) {
            return null;
        }
        return new RootPath(file);
    }
    return node.getUserData(NODE_PATH);
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with PsiFileImpl

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

the class FileElement method copyElement.

@Override
public ASTNode copyElement() {
    PsiFileImpl psiElement = (PsiFileImpl) getPsi();
    PsiFileImpl psiElementCopy = (PsiFileImpl) psiElement.copy();
    return psiElementCopy.getTreeElement();
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

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