Search in sources :

Example 6 with PsiFileImpl

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

the class PyStubsTest method testStubIndexMismatch.

public void testStubIndexMismatch() {
    VirtualFile vFile = myFixture.getTempDirFixture().createFile("foo.py");
    final Project project = myFixture.getProject();
    PsiFileImpl fooPyFile = (PsiFileImpl) PsiManager.getInstance(project).findFile(vFile);
    assertNotNull(fooPyFile);
    final Document fooDocument = fooPyFile.getViewProvider().getDocument();
    assertNotNull(fooDocument);
    final Collection<PyClass> classes = PyClassNameIndex.find("Foo", project, GlobalSearchScope.allScope(project));
    assertEquals(0, classes.size());
    new WriteCommandAction.Simple(project, fooPyFile) {

        public void run() {
            fooDocument.setText("class Foo: pass");
        }
    }.execute();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    documentManager.commitDocument(fooDocument);
    documentManager.performForCommittedDocument(fooDocument, () -> {
        fooPyFile.setTreeElementPointer(null);
        //classes = PyClassNameIndex.find("Foo", project, GlobalSearchScope.allScope(project));
        //fooPyFile.unloadContent();
        DumbServiceImpl.getInstance(project).setDumb(true);
        try {
            assertEquals(1, ((PyFile) fooPyFile).getTopLevelClasses().size());
            assertFalse(fooPyFile.isContentsLoaded());
        } finally {
            DumbServiceImpl.getInstance(project).setDumb(false);
        }
        final Collection<PyClass> committedClasses = PyClassNameIndex.find("Foo", project, GlobalSearchScope.allScope(project));
        assertEquals(1, committedClasses.size());
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document)

Example 7 with PsiFileImpl

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

the class FileIncludeManagerImpl method doResolve.

@Nullable
private PsiFileSystemItem doResolve(@NotNull final FileIncludeInfo info, @NotNull final PsiFile context) {
    if (info instanceof FileIncludeInfoImpl) {
        String id = ((FileIncludeInfoImpl) info).providerId;
        FileIncludeProvider provider = id == null ? null : myProviderMap.get(id);
        final PsiFileSystemItem resolvedByProvider = provider == null ? null : provider.resolveIncludedFile(info, context);
        if (resolvedByProvider != null) {
            return resolvedByProvider;
        }
    }
    PsiFileImpl psiFile = (PsiFileImpl) myPsiFileFactory.createFileFromText("dummy.txt", FileTypes.PLAIN_TEXT, info.path);
    psiFile.setOriginalFile(context);
    return new FileReferenceSet(psiFile) {

        @Override
        protected boolean useIncludingFileAsContext() {
            return false;
        }
    }.resolve();
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with PsiFileImpl

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

the class CodeCompletionHandlerBase method createCompletionContext.

private static CompletionContext createCompletionContext(PsiFile originalFile, OffsetsInFile hostCopyOffsets) {
    CompletionAssertions.assertHostInfo(hostCopyOffsets.getFile(), hostCopyOffsets.getOffsets());
    int hostStartOffset = hostCopyOffsets.getOffsets().getOffset(CompletionInitializationContext.START_OFFSET);
    OffsetsInFile translatedOffsets = hostCopyOffsets.toInjectedIfAny(hostStartOffset);
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(originalFile.getProject());
    PsiFile injected = translatedOffsets == hostCopyOffsets ? null : translatedOffsets.getFile();
    if (injected != null) {
        if (injected instanceof PsiFileImpl) {
            ((PsiFileImpl) injected).setOriginalFile(originalFile);
        }
        DocumentWindow documentWindow = InjectedLanguageUtil.getDocumentWindow(injected);
        CompletionAssertions.assertInjectedOffsets(hostStartOffset, injectedLanguageManager, injected, documentWindow);
    }
    CompletionContext context = new CompletionContext(translatedOffsets.getFile(), translatedOffsets.getOffsets());
    CompletionAssertions.assertFinalOffsets(originalFile, context, injected);
    return context;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile)

Example 9 with PsiFileImpl

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

the class PostfixLiveTemplate method copyFile.

@NotNull
public static PsiFile copyFile(@NotNull PsiFile file, @NotNull StringBuilder fileContentWithoutKey) {
    final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(file.getProject());
    PsiFile copy = psiFileFactory.createFileFromText(file.getName(), file.getFileType(), fileContentWithoutKey);
    if (copy instanceof PsiFileImpl) {
        ((PsiFileImpl) copy).setOriginalFile(TemplateLanguageUtil.getBaseFile(file));
    }
    VirtualFile vFile = copy.getVirtualFile();
    if (vFile != null) {
        vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
    }
    return copy;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFileFactory(com.intellij.psi.PsiFileFactory) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 10 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)

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