Search in sources :

Example 11 with FileASTNode

use of com.intellij.lang.FileASTNode 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 12 with FileASTNode

use of com.intellij.lang.FileASTNode in project intellij-community by JetBrains.

the class SmartPsiElementPointersTest method testEqualPointerRangesWhenCreatedFromStubAndAST.

public void testEqualPointerRangesWhenCreatedFromStubAndAST() {
    final PsiFile file = configureByText(JavaFileType.INSTANCE, "class S {\n" + "}");
    PsiClass aClass = ((PsiJavaFile) file).getClasses()[0];
    assertNotNull(((PsiFileImpl) file).getStubTree());
    final SmartPointerManager manager = getPointerManager();
    final SmartPsiElementPointer<PsiClass> pointer1 = createPointer(aClass);
    Segment range1 = pointer1.getRange();
    manager.removePointer(pointer1);
    final FileASTNode node = file.getNode();
    final SmartPsiElementPointer<PsiClass> pointer2 = createPointer(aClass);
    assertEquals(range1, pointer2.getRange());
    assertNotNull(node);
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) Segment(com.intellij.openapi.util.Segment)

Example 13 with FileASTNode

use of com.intellij.lang.FileASTNode in project intellij-community by JetBrains.

the class PsiImplUtil method multiResolveImpl.

@NotNull
public static <T extends PsiJavaCodeReferenceElement> JavaResolveResult[] multiResolveImpl(@NotNull T element, boolean incompleteCode, @NotNull ResolveCache.PolyVariantContextResolver<? super T> resolver) {
    FileASTNode fileElement = SharedImplUtil.findFileElement(element.getNode());
    if (fileElement == null) {
        PsiUtilCore.ensureValid(element);
        LOG.error("fileElement == null!");
        return JavaResolveResult.EMPTY_ARRAY;
    }
    PsiFile psiFile = SharedImplUtil.getContainingFile(fileElement);
    PsiManager manager = psiFile == null ? null : psiFile.getManager();
    if (manager == null) {
        PsiUtilCore.ensureValid(element);
        LOG.error("getManager() == null!");
        return JavaResolveResult.EMPTY_ARRAY;
    }
    boolean valid = psiFile.isValid();
    if (!valid) {
        PsiUtilCore.ensureValid(element);
        LOG.error("psiFile.isValid() == false!");
        return JavaResolveResult.EMPTY_ARRAY;
    }
    if (element instanceof PsiMethodReferenceExpression) {
        // method refs: do not cache results during parent conflict resolving, acceptable checks, etc
        final Map<PsiElement, PsiType> map = LambdaUtil.ourFunctionTypes.get();
        if (map != null && map.containsKey(element)) {
            return (JavaResolveResult[]) resolver.resolve(element, psiFile, incompleteCode);
        }
    }
    return multiResolveImpl(manager.getProject(), psiFile, element, incompleteCode, resolver);
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with FileASTNode

use of com.intellij.lang.FileASTNode in project intellij-community by JetBrains.

the class Test method doTest.

private static void doTest(@NonNls final String source, @NonNls @Nullable final String tree) {
    final PsiJavaFile file = (PsiJavaFile) createLightFile("test.java", source);
    final FileASTNode fileNode = file.getNode();
    assertNotNull(fileNode);
    assertFalse(fileNode.isParsed());
    long t1 = System.nanoTime();
    final StubElement lighterTree = NEW_BUILDER.buildStubTree(file);
    t1 = Math.max((System.nanoTime() - t1) / 1000, 1);
    assertFalse(fileNode.isParsed());
    // force switch to AST
    file.getNode().getChildren(null);
    long t2 = System.nanoTime();
    // build over AST
    final StubElement lighterTree2 = NEW_BUILDER.buildStubTree(file);
    t2 = Math.max((System.nanoTime() - t2) / 1000, 1);
    file.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            assert !(element instanceof PsiErrorElement) : element;
            super.visitElement(element);
        }
    });
    final String lightStr = DebugUtil.stubTreeToString(lighterTree);
    final String lightStr2 = DebugUtil.stubTreeToString(lighterTree2);
    if (tree != null) {
        System.out.println("light=" + t1 + "mks, heavy=" + t2 + "mks");
        if (!tree.isEmpty()) {
            assertEquals("light tree differs", tree, lightStr);
            assertEquals("light tree (2nd) differs", tree, lightStr2);
        }
    }
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) StubElement(com.intellij.psi.stubs.StubElement)

Example 15 with FileASTNode

use of com.intellij.lang.FileASTNode in project intellij-community by JetBrains.

the class CompletionAssertions method assertCommitSuccessful.

static void assertCommitSuccessful(Editor editor, PsiFile psiFile) {
    Document document = editor.getDocument();
    int docLength = document.getTextLength();
    int psiLength = psiFile.getTextLength();
    PsiDocumentManager manager = PsiDocumentManager.getInstance(psiFile.getProject());
    boolean committed = !manager.isUncommited(document);
    if (docLength == psiLength && committed) {
        return;
    }
    FileViewProvider viewProvider = psiFile.getViewProvider();
    String message = "unsuccessful commit:";
    message += "\nmatching=" + (psiFile == manager.getPsiFile(document));
    message += "\ninjectedEditor=" + (editor instanceof EditorWindow);
    message += "\ninjectedFile=" + InjectedLanguageManager.getInstance(psiFile.getProject()).isInjectedFragment(psiFile);
    message += "\ncommitted=" + committed;
    message += "\nfile=" + psiFile.getName();
    message += "\nfile class=" + psiFile.getClass();
    message += "\nfile.valid=" + psiFile.isValid();
    message += "\nfile.physical=" + psiFile.isPhysical();
    message += "\nfile.eventSystemEnabled=" + viewProvider.isEventSystemEnabled();
    message += "\nlanguage=" + psiFile.getLanguage();
    message += "\ndoc.length=" + docLength;
    message += "\npsiFile.length=" + psiLength;
    String fileText = psiFile.getText();
    if (fileText != null) {
        message += "\npsiFile.text.length=" + fileText.length();
    }
    FileASTNode node = psiFile.getNode();
    if (node != null) {
        message += "\nnode.length=" + node.getTextLength();
        String nodeText = node.getText();
        if (nodeText != null) {
            message += "\nnode.text.length=" + nodeText.length();
        }
    }
    VirtualFile virtualFile = viewProvider.getVirtualFile();
    message += "\nvirtualFile=" + virtualFile;
    message += "\nvirtualFile.class=" + virtualFile.getClass();
    message += "\n" + DebugUtil.currentStackTrace();
    throw new LogEventException("Commit unsuccessful", message, new Attachment(virtualFile.getPath() + "_file.txt", fileText), createAstAttachment(psiFile, psiFile), new Attachment("docText.txt", document.getText()));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileASTNode(com.intellij.lang.FileASTNode) FileViewProvider(com.intellij.psi.FileViewProvider) Attachment(com.intellij.openapi.diagnostic.Attachment) Document(com.intellij.openapi.editor.Document) LogEventException(com.intellij.diagnostic.LogEventException) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) EditorWindow(com.intellij.injected.editor.EditorWindow)

Aggregations

FileASTNode (com.intellij.lang.FileASTNode)20 ASTNode (com.intellij.lang.ASTNode)6 Project (com.intellij.openapi.project.Project)5 NotNull (org.jetbrains.annotations.NotNull)5 Document (com.intellij.openapi.editor.Document)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiElement (com.intellij.psi.PsiElement)3 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)3 SmartList (com.intellij.util.SmartList)3 Nullable (org.jetbrains.annotations.Nullable)3 LighterAST (com.intellij.lang.LighterAST)2 TreeBackedLighterAST (com.intellij.lang.TreeBackedLighterAST)2 TextRange (com.intellij.openapi.util.TextRange)2 StubElement (com.intellij.psi.stubs.StubElement)2 Processor (com.intellij.util.Processor)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 LogEventException (com.intellij.diagnostic.LogEventException)1 DuplicatesProfile (com.intellij.dupLocator.DuplicatesProfile)1 DuplocatorState (com.intellij.dupLocator.DuplocatorState)1