Search in sources :

Example 31 with PsiFileImpl

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

the class MultiHostRegistrarImpl method registerDocument.

// under com.intellij.psi.PsiLock.LOCK
private static PsiFile registerDocument(final DocumentWindowImpl documentWindow, final PsiFile injectedPsi, final Place shreds, final PsiFile hostPsiFile, final PsiDocumentManager documentManager) {
    List<DocumentWindow> injected = InjectedLanguageUtil.getCachedInjectedDocuments(hostPsiFile);
    for (int i = injected.size() - 1; i >= 0; i--) {
        DocumentWindowImpl oldDocument = (DocumentWindowImpl) injected.get(i);
        final PsiFileImpl oldFile = (PsiFileImpl) documentManager.getCachedPsiFile(oldDocument);
        FileViewProvider viewProvider;
        if (oldFile == null || !oldFile.isValid() || !((viewProvider = oldFile.getViewProvider()) instanceof InjectedFileViewProvider) || ((InjectedFileViewProvider) viewProvider).isDisposed()) {
            injected.remove(i);
            Disposer.dispose(oldDocument);
            continue;
        }
        InjectedFileViewProvider oldViewProvider = (InjectedFileViewProvider) viewProvider;
        final ASTNode injectedNode = injectedPsi.getNode();
        final ASTNode oldFileNode = oldFile.getNode();
        assert injectedNode != null : "New node is null";
        if (oldDocument.areRangesEqual(documentWindow)) {
            if (oldFile.getFileType() != injectedPsi.getFileType() || oldFile.getLanguage() != injectedPsi.getLanguage()) {
                injected.remove(i);
                Disposer.dispose(oldDocument);
                continue;
            }
            oldFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, injectedPsi.getUserData(FileContextUtil.INJECTED_IN_ELEMENT));
            assert shreds.isValid();
            if (!oldFile.textMatches(injectedPsi)) {
                oldViewProvider.performNonPhysically(() -> {
                    DebugUtil.startPsiModification("injected tree diff");
                    try {
                        final DiffLog diffLog = BlockSupportImpl.mergeTrees(oldFile, oldFileNode, injectedNode, new DaemonProgressIndicator(), oldFileNode.getText());
                        DocumentCommitThread.doActualPsiChange(oldFile, diffLog);
                    } finally {
                        DebugUtil.finishPsiModification();
                    }
                });
            }
            assert shreds.isValid();
            return oldFile;
        }
    }
    injected.add(documentWindow);
    return injectedPsi;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) ASTNode(com.intellij.lang.ASTNode) DiffLog(com.intellij.psi.impl.source.text.DiffLog)

Example 32 with PsiFileImpl

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

the class TemplateDataElementType method parseContents.

@Override
public ASTNode parseContents(ASTNode chameleon) {
    final CharTable charTable = SharedImplUtil.findCharTableByTree(chameleon);
    final FileElement fileElement = TreeUtil.getFileElement((TreeElement) chameleon);
    final PsiFile psiFile = (PsiFile) fileElement.getPsi();
    PsiFile originalPsiFile = psiFile.getOriginalFile();
    final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider) originalPsiFile.getViewProvider();
    final Language templateLanguage = getTemplateFileLanguage(viewProvider);
    final CharSequence sourceCode = chameleon.getChars();
    RangesCollector collector = new RangesCollector();
    final PsiFile templatePsiFile = createTemplateFile(psiFile, templateLanguage, sourceCode, viewProvider, collector);
    final FileElement templateFileElement = ((PsiFileImpl) templatePsiFile).calcTreeElement();
    DebugUtil.startPsiModification("template language parsing");
    try {
        prepareParsedTemplateFile(templateFileElement);
        insertOuters(templateFileElement, sourceCode, collector.myRanges, charTable);
        TreeElement childNode = templateFileElement.getFirstChildNode();
        DebugUtil.checkTreeStructure(templateFileElement);
        DebugUtil.checkTreeStructure(chameleon);
        if (fileElement != chameleon) {
            DebugUtil.checkTreeStructure(psiFile.getNode());
            DebugUtil.checkTreeStructure(originalPsiFile.getNode());
        }
        return childNode;
    } finally {
        DebugUtil.finishPsiModification();
    }
}
Also used : Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) CharTable(com.intellij.util.CharTable)

Example 33 with PsiFileImpl

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

the class PomModelImpl method startTransaction.

private void startTransaction(@NotNull PomTransaction transaction) {
    final ProgressIndicator progressIndicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
    if (progressIndicator != null)
        progressIndicator.startNonCancelableSection();
    final PsiDocumentManagerBase manager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(myProject);
    final PsiToDocumentSynchronizer synchronizer = manager.getSynchronizer();
    final PsiElement changeScope = transaction.getChangeScope();
    final PsiFile containingFileByTree = getContainingFileByTree(changeScope);
    if (containingFileByTree != null && !(containingFileByTree instanceof DummyHolder) && !manager.isCommitInProgress()) {
        PsiUtilCore.ensureValid(containingFileByTree);
    }
    boolean physical = changeScope.isPhysical();
    if (physical && synchronizer.toProcessPsiEvent()) {
        // so it's important to throw something outside event processing
        if (isDocumentUncommitted(containingFileByTree)) {
            throw new IllegalStateException("Attempt to modify PSI for non-committed Document!");
        }
        CommandProcessor commandProcessor = CommandProcessor.getInstance();
        if (!commandProcessor.isUndoTransparentActionInProgress() && commandProcessor.getCurrentCommand() == null) {
            throw new IncorrectOperationException("Must not change PSI outside command or undo-transparent action. See com.intellij.openapi.command.WriteCommandAction or com.intellij.openapi.command.CommandProcessor");
        }
    }
    if (containingFileByTree != null) {
        ((SmartPointerManagerImpl) SmartPointerManager.getInstance(myProject)).fastenBelts(containingFileByTree.getViewProvider().getVirtualFile());
        if (containingFileByTree instanceof PsiFileImpl) {
            ((PsiFileImpl) containingFileByTree).beforeAstChange();
        }
    }
    BlockSupportImpl.sendBeforeChildrenChangeEvent((PsiManagerImpl) PsiManager.getInstance(myProject), changeScope, true);
    Document document = containingFileByTree == null ? null : physical ? manager.getDocument(containingFileByTree) : manager.getCachedDocument(containingFileByTree);
    if (document != null) {
        synchronizer.startTransaction(myProject, document, changeScope);
    }
}
Also used : DummyHolder(com.intellij.psi.impl.source.DummyHolder) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CommandProcessor(com.intellij.openapi.command.CommandProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) SmartPointerManagerImpl(com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl)

Example 34 with PsiFileImpl

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

the class StubBasedPsiElementBase method getNode.

/**
   * Ensures this element is AST-based. This is an expensive operation that might take significant time and allocate lots of objects,
   * so it should be to be avoided if possible.
   *
   * @return an AST node corresponding to this element. If the element is currently operating via stubs,
   * this causes AST to be loaded for the whole file and all stub-based PSI elements in this file (including the current one)
   * to be switched from stub to AST. So, after this call {@link #getStub()} will return null.
   */
@Override
@NotNull
public ASTNode getNode() {
    if (mySubstrateRef instanceof SubstrateRef.StubRef) {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        PsiFileImpl file = (PsiFileImpl) getContainingFile();
        if (!file.isValid())
            throw new PsiInvalidElementAccessException(this);
        FileElement treeElement = file.getTreeElement();
        if (treeElement != null && mySubstrateRef instanceof SubstrateRef.StubRef) {
            return notBoundInExistingAst(file, treeElement);
        }
        treeElement = file.calcTreeElement();
        if (mySubstrateRef instanceof SubstrateRef.StubRef) {
            return failedToBindStubToAst(file, treeElement);
        }
    }
    return mySubstrateRef.getNode();
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) SubstrateRef(com.intellij.psi.impl.source.SubstrateRef) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileElement(com.intellij.psi.impl.source.tree.FileElement) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with PsiFileImpl

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

the class HbFileViewProvider method createFile.

@Override
protected PsiFile createFile(@NotNull Language lang) {
    ParserDefinition parserDefinition = getDefinition(lang);
    if (parserDefinition == null) {
        return null;
    }
    if (lang.is(getTemplateDataLanguage())) {
        PsiFileImpl file = (PsiFileImpl) parserDefinition.createFile(this);
        file.setContentElementType(getTemplateDataElementType(getBaseLanguage()));
        return file;
    } else if (lang.isKindOf(getBaseLanguage())) {
        return parserDefinition.createFile(this);
    } else {
        return null;
    }
}
Also used : ParserDefinition(com.intellij.lang.ParserDefinition) 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