Search in sources :

Example 26 with PsiFileImpl

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

the class MultiplePsiFilesPerDocumentFileViewProvider method getPsiInner.

@Override
protected PsiFile getPsiInner(@NotNull final Language target) {
    PsiFileImpl file = myRoots.get(target);
    if (file == null) {
        if (isPhysical()) {
            VirtualFile virtualFile = getVirtualFile();
            if (isIgnored())
                return null;
            VirtualFile parent = virtualFile.getParent();
            if (parent != null) {
                getManager().findDirectory(parent);
            }
        }
        if (target != getBaseLanguage() && !getLanguages().contains(target)) {
            return null;
        }
        file = (PsiFileImpl) createFile(target);
        if (file == null)
            return null;
        if (myOriginal != null) {
            final PsiFile originalFile = myOriginal.getPsi(target);
            if (originalFile != null) {
                file.setOriginalFile(originalFile);
            }
        }
        file = ConcurrencyUtil.cacheOrGet(myRoots, target, file);
    }
    return file;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl)

Example 27 with PsiFileImpl

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

the class MultiplePsiFilesPerDocumentFileViewProvider method contentsSynchronized.

@Override
public void contentsSynchronized() {
    Set<Language> languages = getLanguages();
    for (Iterator<Map.Entry<Language, PsiFileImpl>> iterator = myRoots.entrySet().iterator(); iterator.hasNext(); ) {
        Map.Entry<Language, PsiFileImpl> entry = iterator.next();
        if (!languages.contains(entry.getKey())) {
            PsiFileImpl file = entry.getValue();
            iterator.remove();
            file.markInvalidated();
        }
    }
    super.contentsSynchronized();
}
Also used : Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 28 with PsiFileImpl

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

the class PsiDirectoryImpl method add.

@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
    checkAdd(element);
    if (element instanceof PsiFile) {
        PsiFile originalFile = (PsiFile) element;
        try {
            VirtualFile newVFile;
            final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myManager.getProject());
            if (originalFile instanceof PsiFileImpl) {
                newVFile = myFile.createChildData(myManager, originalFile.getName());
                String text = originalFile.getText();
                final PsiFile psiFile = getManager().findFile(newVFile);
                final Document document = psiFile == null ? null : psiDocumentManager.getDocument(psiFile);
                final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
                if (document != null) {
                    document.setText(text);
                    fileDocumentManager.saveDocument(document);
                } else {
                    String lineSeparator = fileDocumentManager.getLineSeparator(newVFile, getProject());
                    if (!lineSeparator.equals("\n")) {
                        text = StringUtil.convertLineSeparators(text, lineSeparator);
                    }
                    LoadTextUtil.write(getProject(), newVFile, myManager, text, -1);
                }
            } else {
                byte[] storedContents = ((PsiBinaryFileImpl) originalFile).getStoredContents();
                if (storedContents != null) {
                    newVFile = myFile.createChildData(myManager, originalFile.getName());
                    newVFile.setBinaryContent(storedContents);
                } else {
                    newVFile = VfsUtilCore.copyFile(null, originalFile.getVirtualFile(), myFile);
                }
            }
            psiDocumentManager.commitAllDocuments();
            PsiFile newFile = myManager.findFile(newVFile);
            if (newFile == null)
                throw new IncorrectOperationException("Could not find file " + newVFile);
            updateAddedFile(newFile);
            return newFile;
        } catch (IOException e) {
            throw new IncorrectOperationException(e);
        }
    }
    throw new IncorrectOperationException(element + " (" + element.getClass() + ")");
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document)

Example 29 with PsiFileImpl

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

the class SingleRootFileViewProvider method getKnownTreeRoots.

@NotNull
public List<FileElement> getKnownTreeRoots() {
    PsiFile psiFile = getCachedPsi(myBaseLanguage);
    if (!(psiFile instanceof PsiFileImpl))
        return Collections.emptyList();
    FileElement element = ((PsiFileImpl) psiFile).getTreeElement();
    return ContainerUtil.createMaybeSingletonList(element);
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileElement(com.intellij.psi.impl.source.tree.FileElement) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with PsiFileImpl

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

the class UsageViewTest method testUsageViewDoesNotHoldPsiFilesOrDocuments.

public void testUsageViewDoesNotHoldPsiFilesOrDocuments() throws Exception {
    boolean[] foundLeaksBeforeTest = new boolean[1];
    LeakHunter.checkLeak(ApplicationManager.getApplication(), PsiFileImpl.class, file -> {
        if (!file.isPhysical())
            return false;
        System.err.println("Can't start the test: leaking PsiFiles found:");
        foundLeaksBeforeTest[0] = true;
        return true;
    });
    if (foundLeaksBeforeTest[0]) {
        fail("Can't start the test: leaking PsiFiles found");
    }
    PsiFile psiFile = myFixture.addFileToProject("X.java", "public class X{} //iuggjhfg");
    Usage[] usages = new Usage[100];
    for (int i = 0; i < usages.length; i++) {
        usages[i] = createUsage(psiFile, i);
    }
    UsageView usageView = UsageViewManager.getInstance(getProject()).createUsageView(UsageTarget.EMPTY_ARRAY, usages, new UsageViewPresentation(), null);
    Disposer.register(myFixture.getTestRootDisposable(), usageView);
    ((EncodingManagerImpl) EncodingManager.getInstance()).clearDocumentQueue();
    FileDocumentManager.getInstance().saveAllDocuments();
    UIUtil.dispatchAllInvocationEvents();
    LeakHunter.checkLeak(usageView, PsiFileImpl.class, PsiFileImpl::isPhysical);
    LeakHunter.checkLeak(usageView, Document.class);
}
Also used : EncodingManagerImpl(com.intellij.openapi.vfs.encoding.EncodingManagerImpl) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile)

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