Search in sources :

Example 46 with PsiFileImpl

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

the class SmartPsiElementPointersTest method testNoAstLoadingWithoutDocumentChanges.

public void testNoAstLoadingWithoutDocumentChanges() {
    PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.allScope(getProject()));
    assertNotNull(aClass);
    PsiFileImpl file = (PsiFileImpl) aClass.getContainingFile();
    createEditor(file.getVirtualFile());
    assertFalse(file.isContentsLoaded());
    SmartPointerEx pointer = (SmartPointerEx) createPointer(aClass);
    assertFalse(file.isContentsLoaded());
    //noinspection UnusedAssignment
    aClass = null;
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    assertNull(pointer.getCachedElement());
    assertNotNull(pointer.getElement());
    assertFalse(file.isContentsLoaded());
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

Example 47 with PsiFileImpl

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

the class SmartPsiElementPointersTest method testNonAnchoredStubbedElement.

public void testNonAnchoredStubbedElement() {
    PsiFile file = configureByText(JavaFileType.INSTANCE, "class Foo { { @NotNull String foo; } }");
    StubTree stubTree = ((PsiFileImpl) file).getStubTree();
    assertNotNull(stubTree);
    PsiElement anno = stubTree.getPlainList().stream().map(StubElement::getPsi).filter(psiElement -> psiElement instanceof PsiAnnotation).findFirst().get();
    SmartPsiElementPointer<PsiElement> pointer = createPointer(anno);
    assertNotNull(((PsiFileImpl) file).getStubTree());
    stubTree = null;
    anno = null;
    PlatformTestUtil.tryGcSoftlyReachableObjects();
    assertNull(((SmartPointerEx) pointer).getCachedElement());
    insertString(file.getViewProvider().getDocument(), 0, " ");
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    assertNotNull(pointer.getElement());
}
Also used : StubTree(com.intellij.psi.stubs.StubTree) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

Example 48 with PsiFileImpl

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

the class ClsFileImpl method getMirror.

@Override
public PsiElement getMirror() {
    TreeElement mirrorTreeElement = SoftReference.dereference(myMirrorFileElement);
    if (mirrorTreeElement == null) {
        synchronized (myMirrorLock) {
            mirrorTreeElement = SoftReference.dereference(myMirrorFileElement);
            if (mirrorTreeElement == null) {
                VirtualFile file = getVirtualFile();
                PsiClass[] classes = getClasses();
                String fileName = (classes.length > 0 ? classes[0].getName() : file.getNameWithoutExtension()) + JavaFileType.DOT_DEFAULT_EXTENSION;
                final Document document = FileDocumentManager.getInstance().getDocument(file);
                assert document != null : file.getUrl();
                CharSequence mirrorText = document.getImmutableCharSequence();
                boolean internalDecompiler = StringUtil.startsWith(mirrorText, BANNER);
                PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject());
                PsiFile mirror = factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false);
                mirror.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, getLanguageLevel());
                mirrorTreeElement = SourceTreeToPsiMap.psiToTreeNotNull(mirror);
                try {
                    final TreeElement finalMirrorTreeElement = mirrorTreeElement;
                    ProgressManager.getInstance().executeNonCancelableSection(() -> {
                        setMirror(finalMirrorTreeElement);
                        putUserData(CLS_DOCUMENT_LINK_KEY, document);
                    });
                } catch (InvalidMirrorException e) {
                    //noinspection ThrowableResultOfMethodCallIgnored
                    LOG.error(file.getUrl(), internalDecompiler ? e : wrapException(e, file));
                }
                ((PsiFileImpl) mirror).setOriginalFile(this);
                myMirrorFileElement = new SoftReference<>(mirrorTreeElement);
            }
        }
    }
    return mirrorTreeElement.getPsi();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 49 with PsiFileImpl

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

the class MultiplePsiFilesPerDocumentFileViewProvider method checkAllTreesEqual.

@TestOnly
public void checkAllTreesEqual() {
    Collection<PsiFileImpl> roots = myRoots.values();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getManager().getProject());
    documentManager.commitAllDocuments();
    for (PsiFile root : roots) {
        Document document = documentManager.getDocument(root);
        PsiDocumentManagerBase.checkConsistency(root, document);
        assert root.getText().equals(document.getText());
    }
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) TestOnly(org.jetbrains.annotations.TestOnly)

Example 50 with PsiFileImpl

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

the class PsiAnchor method calcStubIndex.

public static int calcStubIndex(@NotNull StubBasedPsiElement psi) {
    if (psi instanceof PsiFile) {
        return 0;
    }
    final StubElement liveStub = psi.getStub();
    if (liveStub != null) {
        return ((StubBase) liveStub).id;
    }
    PsiFileImpl file = (PsiFileImpl) psi.getContainingFile();
    final StubTree stubTree = file.calcStubTree();
    for (StubElement<?> stb : stubTree.getPlainList()) {
        if (stb.getPsi() == psi) {
            return ((StubBase) stb).id;
        }
    }
    // it is possible via custom stub builder intentionally not producing stubs for stubbed elements
    return -1;
}
Also used : StubBase(com.intellij.psi.stubs.StubBase) 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