Search in sources :

Example 21 with PsiFileImpl

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

the class TreeElement method setTreeParent.

final void setTreeParent(CompositeElement parent) {
    if (parent == myParent)
        return;
    PsiFileImpl file = getCachedFile(this);
    if (file != null) {
        file.beforeAstChange();
    }
    myParent = parent;
    if (parent != null && parent.getElementType() != TokenType.DUMMY_HOLDER) {
        DebugUtil.revalidateNode(this);
    }
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

Example 22 with PsiFileImpl

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

the class JavaFunctionalExpressionPresentationTest method doTest.

private void doTest(@Language("JAVA") @NotNull String funExprText, @NotNull String expectedPresentableString) {
    final PsiFileImpl file = assertInstanceOf(configureByText(JavaFileType.INSTANCE, funExprText), PsiFileImpl.class);
    //stub based test
    final FunctionalExpressionStub functionalExpressionStub = StreamEx.of(file.calcStubTree().getPlainList()).select(FunctionalExpressionStub.class).collect(MoreCollectors.onlyOne()).orElse(null);
    assertNotNull(functionalExpressionStub);
    assertEquals("Comparing with stub based rendering", expectedPresentableString, functionalExpressionStub.getPresentableText());
    //ast based test
    final PsiExpression psi = assertInstanceOf(functionalExpressionStub.getPsi(), PsiExpression.class);
    assertEquals("Comparing with AST based rendering", expectedPresentableString, PsiExpressionTrimRenderer.render(psi));
}
Also used : FunctionalExpressionStub(com.intellij.psi.impl.java.stubs.FunctionalExpressionStub) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

Example 23 with PsiFileImpl

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

the class DocumentCommitThread method commitUnderProgress.

// returns (finish commit Runnable (to be invoked later in EDT), null) on success or (null, failure reason) on failure
@NotNull
private Pair<Runnable, Object> commitUnderProgress(@NotNull final CommitTask task, final boolean synchronously) {
    if (synchronously) {
        assert !task.indicator.isCanceled();
    }
    final Document document = task.getDocument();
    final Project project = task.project;
    final PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(project);
    final List<Processor<Document>> finishProcessors = new SmartList<>();
    Runnable runnable = () -> {
        myApplication.assertReadAccessAllowed();
        if (project.isDisposed())
            return;
        Lock lock = getDocumentLock(document);
        if (!lock.tryLock()) {
            task.cancel("Can't obtain document lock", this);
            return;
        }
        boolean canceled = false;
        try {
            if (documentManager.isCommitted(document))
                return;
            if (!task.isStillValid()) {
                canceled = true;
                return;
            }
            FileViewProvider viewProvider = documentManager.getCachedViewProvider(document);
            if (viewProvider == null) {
                finishProcessors.add(handleCommitWithoutPsi(documentManager, task));
                return;
            }
            for (Pair<PsiFileImpl, FileASTNode> pair : task.myOldFileNodes) {
                PsiFileImpl file = pair.first;
                if (file.isValid()) {
                    FileASTNode oldFileNode = pair.second;
                    Processor<Document> finishProcessor = doCommit(task, file, oldFileNode);
                    if (finishProcessor != null) {
                        finishProcessors.add(finishProcessor);
                    }
                } else {
                    // file became invalid while sitting in the queue
                    if (task.reason.equals(SYNC_COMMIT_REASON)) {
                        throw new PsiInvalidElementAccessException(file, "File " + file + " invalidated during sync commit");
                    }
                    commitAsynchronously(project, document, "File " + file + " invalidated during background commit; task: " + task, task.myCreationContext);
                }
            }
        } finally {
            lock.unlock();
            if (canceled) {
                task.cancel("Task invalidated", this);
            }
        }
    };
    if (synchronously) {
        runnable.run();
    } else if (!myApplication.tryRunReadAction(runnable)) {
        log(project, "Could not start read action", task, myApplication.isReadAccessAllowed(), Thread.currentThread());
        return new Pair<>(null, "Could not start read action");
    }
    boolean canceled = task.indicator.isCanceled();
    assert !synchronously || !canceled;
    if (canceled) {
        return new Pair<>(null, "Indicator was canceled");
    }
    Runnable result = createEdtRunnable(task, synchronously, finishProcessors);
    return Pair.create(result, null);
}
Also used : Processor(com.intellij.util.Processor) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Project(com.intellij.openapi.project.Project) FileASTNode(com.intellij.lang.FileASTNode) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with PsiFileImpl

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

the class PsiDocumentManagerBase method documentChanged.

@Override
public void documentChanged(DocumentEvent event) {
    if (myStopTrackingDocuments || myProject.isDisposed())
        return;
    final Document document = event.getDocument();
    VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
    boolean isRelevant = virtualFile != null && isRelevant(virtualFile);
    final FileViewProvider viewProvider = getCachedViewProvider(document);
    if (viewProvider == null) {
        handleCommitWithoutPsi(document);
        return;
    }
    boolean inMyProject = viewProvider.getManager() == myPsiManager;
    if (!isRelevant || !inMyProject) {
        clearUncommittedInfo(document);
        return;
    }
    List<PsiFile> files = viewProvider.getAllFiles();
    boolean commitNecessary = files.stream().noneMatch(file -> PsiToDocumentSynchronizer.isInsideAtomicChange(file) || !(file instanceof PsiFileImpl));
    boolean forceCommit = ApplicationManager.getApplication().hasWriteAction(ExternalChangeAction.class) && (SystemProperties.getBooleanProperty("idea.force.commit.on.external.change", false) || ApplicationManager.getApplication().isHeadlessEnvironment() && !ApplicationManager.getApplication().isUnitTestMode());
    // for the huge file (that causes the whole document to be reloaded and 'merge' way takes a while to complete).
    if (event.isWholeTextReplaced() && document.getTextLength() > 100000) {
        document.putUserData(BlockSupport.DO_NOT_REPARSE_INCREMENTALLY, Boolean.TRUE);
    }
    if (commitNecessary) {
        assert !(document instanceof DocumentWindow);
        myUncommittedDocuments.add(document);
        if (forceCommit) {
            commitDocument(document);
        } else if (!((DocumentEx) document).isInBulkUpdate() && myPerformBackgroundCommit) {
            myDocumentCommitProcessor.commitAsynchronously(myProject, document, event, TransactionGuard.getInstance().getContextTransaction());
        }
    } else {
        clearUncommittedInfo(document);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentWindow(com.intellij.injected.editor.DocumentWindow) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument)

Example 25 with PsiFileImpl

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

the class SmartPsiElementPointerImpl method createAnchorInfo.

@Nullable
private static SmartPointerElementInfo createAnchorInfo(@NotNull PsiElement element, @NotNull PsiFile containingFile) {
    if (element instanceof StubBasedPsiElement && containingFile instanceof PsiFileWithStubSupport) {
        PsiFileWithStubSupport stubFile = (PsiFileWithStubSupport) containingFile;
        StubTree stubTree = stubFile.getStubTree();
        if (stubTree != null) {
            // use stubs when tree is not loaded
            StubBasedPsiElement stubPsi = (StubBasedPsiElement) element;
            int stubId = PsiAnchor.calcStubIndex(stubPsi);
            IStubElementType myStubElementType = stubPsi.getElementType();
            IStubFileElementType elementTypeForStubBuilder = ((PsiFileImpl) containingFile).getElementTypeForStubBuilder();
            if (stubId != -1 && elementTypeForStubBuilder != null) {
                // TemplateDataElementType is not IStubFileElementType
                return new AnchorElementInfo(element, stubFile, stubId, myStubElementType);
            }
        }
    }
    Pair<Identikit.ByAnchor, PsiElement> pair = Identikit.withAnchor(element, LanguageUtil.getRootLanguage(containingFile));
    if (pair != null) {
        return new AnchorElementInfo(pair.second, containingFile, pair.first);
    }
    return null;
}
Also used : PsiFileWithStubSupport(com.intellij.psi.impl.source.PsiFileWithStubSupport) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) StubTree(com.intellij.psi.stubs.StubTree) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) IStubElementType(com.intellij.psi.stubs.IStubElementType) ForeignLeafPsiElement(com.intellij.psi.impl.source.tree.ForeignLeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

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