Search in sources :

Example 71 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class DiffContentFactoryImpl method createPsiDocument.

@Nullable
private static Document createPsiDocument(@NotNull Project project, @NotNull String content, @NotNull FileType fileType, @NotNull String fileName, boolean readOnly) {
    return ReadAction.compute(() -> {
        LightVirtualFile file = new LightVirtualFile(fileName, fileType, content);
        file.setWritable(!readOnly);
        file.putUserData(DiffPsiFileSupport.KEY, true);
        Document document = FileDocumentManager.getInstance().getDocument(file);
        if (document == null)
            return null;
        PsiDocumentManager.getInstance(project).getPsiFile(document);
        return document;
    });
}
Also used : BinaryLightVirtualFile(com.intellij.testFramework.BinaryLightVirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Document(com.intellij.openapi.editor.Document) Nullable(org.jetbrains.annotations.Nullable)

Example 72 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class FileBasedIndexTest method testLargeFile.

public void testLargeFile() throws Exception {
    char[] text = new char[FileUtilRt.LARGE_FOR_CONTENT_LOADING + 42];
    final String clazz = "class Foo { String bar; }";
    for (int i = 0; i < text.length; i++) {
        text[i] = i < clazz.length() ? clazz.charAt(i) : ' ';
    }
    final LightVirtualFile file = new LightVirtualFile("Foo.java", new CharArrayCharSequence(text));
    assertFalse(((FileBasedIndexImpl) FileBasedIndex.getInstance()).isIndexingCandidate(file, StubUpdatingIndex.INDEX_ID));
}
Also used : CharArrayCharSequence(com.intellij.util.text.CharArrayCharSequence) LightVirtualFile(com.intellij.testFramework.LightVirtualFile)

Example 73 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class PsiUtilCore method getPsiFile.

/**
   * Tries to find PSI file for a virtual file and throws assertion error with debug info if it is null.
   */
@NotNull
public static PsiFile getPsiFile(@NotNull Project project, @NotNull VirtualFile file) {
    PsiManager psiManager = PsiManager.getInstance(project);
    PsiFile psi = psiManager.findFile(file);
    if (psi != null)
        return psi;
    FileType fileType = file.getFileType();
    FileViewProvider viewProvider = psiManager.findViewProvider(file);
    Document document = FileDocumentManager.getInstance().getDocument(file);
    boolean ignored = !(file instanceof LightVirtualFile) && FileTypeRegistry.getInstance().isFileIgnored(file);
    VirtualFile vDir = file.getParent();
    PsiDirectory psiDir = vDir == null ? null : PsiManager.getInstance(project).findDirectory(vDir);
    FileIndexFacade indexFacade = FileIndexFacade.getInstance(project);
    StringBuilder sb = new StringBuilder();
    sb.append("valid=").append(file.isValid()).append(" isDirectory=").append(file.isDirectory()).append(" hasDocument=").append(document != null).append(" length=").append(file.getLength());
    sb.append("\nproject=").append(project.getName()).append(" default=").append(project.isDefault()).append(" open=").append(project.isOpen());
    sb.append("\nfileType=").append(fileType.getName()).append("/").append(fileType.getClass().getName());
    sb.append("\nisIgnored=").append(ignored);
    sb.append(" underIgnored=").append(indexFacade.isUnderIgnored(file));
    sb.append(" inLibrary=").append(indexFacade.isInLibrarySource(file) || indexFacade.isInLibraryClasses(file));
    sb.append(" parentDir=").append(vDir == null ? "no-vfs" : vDir.isDirectory() ? "has-vfs-dir" : "has-vfs-file").append("/").append(psiDir == null ? "no-psi" : "has-psi");
    sb.append("\nviewProvider=").append(viewProvider == null ? "null" : viewProvider.getClass().getName());
    if (viewProvider != null) {
        List<PsiFile> files = viewProvider.getAllFiles();
        sb.append(" language=").append(viewProvider.getBaseLanguage().getID());
        sb.append(" physical=").append(viewProvider.isPhysical());
        sb.append(" rootCount=").append(files.size());
        for (PsiFile o : files) {
            sb.append("\n  root=").append(o.getLanguage().getID()).append("/").append(o.getClass().getName());
        }
    }
    LOG.error("no PSI for file '" + file.getName() + "'", new Attachment(file.getPresentableUrl(), sb.toString()));
    throw new AssertionError();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Attachment(com.intellij.openapi.diagnostic.Attachment) Document(com.intellij.openapi.editor.Document) FileType(com.intellij.openapi.fileTypes.FileType) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) NotNull(org.jetbrains.annotations.NotNull)

Example 74 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class PsiFileFactoryImpl method createFileFromText.

@Override
@NotNull
public PsiFile createFileFromText(@NotNull String name, @NotNull FileType fileType, @NotNull CharSequence text, long modificationStamp, final boolean eventSystemEnabled, boolean markAsCopy) {
    final LightVirtualFile virtualFile = new LightVirtualFile(name, fileType, text, modificationStamp);
    if (fileType instanceof LanguageFileType) {
        final Language language = LanguageSubstitutors.INSTANCE.substituteLanguage(((LanguageFileType) fileType).getLanguage(), virtualFile, myManager.getProject());
        final PsiFile file = trySetupPsiForFile(virtualFile, language, eventSystemEnabled, markAsCopy);
        if (file != null)
            return file;
    }
    final SingleRootFileViewProvider singleRootFileViewProvider = new SingleRootFileViewProvider(myManager, virtualFile, eventSystemEnabled);
    final PsiPlainTextFileImpl plainTextFile = new PsiPlainTextFileImpl(singleRootFileViewProvider);
    if (markAsCopy)
        CodeEditUtil.setNodeGenerated(plainTextFile.getNode(), true);
    return plainTextFile;
}
Also used : LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) PsiPlainTextFileImpl(com.intellij.psi.impl.source.PsiPlainTextFileImpl) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) NotNull(org.jetbrains.annotations.NotNull)

Example 75 with LightVirtualFile

use of com.intellij.testFramework.LightVirtualFile in project intellij-community by JetBrains.

the class PsiFileFactoryImpl method createFileFromText.

@NotNull
public PsiFile createFileFromText(@NotNull String name, @NotNull FileType fileType, final Language language, @NotNull Language targetLanguage, @NotNull CharSequence text, long modificationStamp, final boolean physical, boolean markAsCopy) {
    final LightVirtualFile virtualFile = new LightVirtualFile(name, fileType, text, modificationStamp);
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    final FileViewProviderFactory factory = LanguageFileViewProviders.INSTANCE.forLanguage(language);
    FileViewProvider viewProvider = factory != null ? factory.createFileViewProvider(virtualFile, language, myManager, physical) : null;
    if (viewProvider == null)
        viewProvider = new SingleRootFileViewProvider(myManager, virtualFile, physical);
    if (parserDefinition != null) {
        final PsiFile psiFile = viewProvider.getPsi(targetLanguage);
        if (psiFile != null) {
            if (markAsCopy) {
                markGenerated(psiFile);
            }
            return psiFile;
        }
    }
    final SingleRootFileViewProvider singleRootFileViewProvider = new SingleRootFileViewProvider(myManager, virtualFile, physical);
    final PsiPlainTextFileImpl plainTextFile = new PsiPlainTextFileImpl(singleRootFileViewProvider);
    if (markAsCopy)
        CodeEditUtil.setNodeGenerated(plainTextFile.getNode(), true);
    return plainTextFile;
}
Also used : PsiPlainTextFileImpl(com.intellij.psi.impl.source.PsiPlainTextFileImpl) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LightVirtualFile (com.intellij.testFramework.LightVirtualFile)102 VirtualFile (com.intellij.openapi.vfs.VirtualFile)26 NotNull (org.jetbrains.annotations.NotNull)14 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)13 Nullable (org.jetbrains.annotations.Nullable)13 PsiFile (com.intellij.psi.PsiFile)12 Project (com.intellij.openapi.project.Project)11 Document (com.intellij.openapi.editor.Document)9 FileEditor (com.intellij.openapi.fileEditor.FileEditor)8 FileType (com.intellij.openapi.fileTypes.FileType)8 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)7 FileManager (com.intellij.psi.impl.file.impl.FileManager)7 IpnbFile (org.jetbrains.plugins.ipnb.format.IpnbFile)7 BlobExplorerFileEditor (com.microsoft.intellij.helpers.storage.BlobExplorerFileEditor)6 QueueFileEditor (com.microsoft.intellij.helpers.storage.QueueFileEditor)6 TableFileEditor (com.microsoft.intellij.helpers.storage.TableFileEditor)6 Language (com.intellij.lang.Language)5 PsiFileFactoryImpl (com.intellij.psi.impl.PsiFileFactoryImpl)5 FileElement (com.intellij.psi.impl.source.tree.FileElement)5 FileViewProvider (com.intellij.psi.FileViewProvider)4