Search in sources :

Example 1 with ModuleFileType

use of com.intellij.ide.highlighter.ModuleFileType in project intellij-community by JetBrains.

the class FileTypesTest method testReDetectOnContentsChange.

public void testReDetectOnContentsChange() throws IOException {
    final FileTypeRegistry fileTypeManager = FileTypeRegistry.getInstance();
    assertTrue(fileTypeManager.getClass().getName(), fileTypeManager instanceof FileTypeManagerImpl);
    FileType fileType = fileTypeManager.getFileTypeByFileName("x" + ModuleFileType.DOT_DEFAULT_EXTENSION);
    assertTrue(fileType.toString(), fileType instanceof ModuleFileType);
    fileType = fileTypeManager.getFileTypeByFileName("x" + ProjectFileType.DOT_DEFAULT_EXTENSION);
    assertTrue(fileType.toString(), fileType instanceof ProjectFileType);
    FileType module = fileTypeManager.findFileTypeByName("IDEA_MODULE");
    assertNotNull(module);
    assertFalse(module.equals(PlainTextFileType.INSTANCE));
    FileType project = fileTypeManager.findFileTypeByName("IDEA_PROJECT");
    assertNotNull(project);
    assertFalse(project.equals(PlainTextFileType.INSTANCE));
    final Set<VirtualFile> detectorCalled = ContainerUtil.newConcurrentSet();
    FileTypeRegistry.FileTypeDetector detector = new FileTypeRegistry.FileTypeDetector() {

        @Nullable
        @Override
        public FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) {
            detectorCalled.add(file);
            String text = firstCharsIfText.toString();
            FileType result = text.startsWith("TYPE:") ? fileTypeManager.findFileTypeByName(StringUtil.trimStart(text, "TYPE:")) : null;
            log("T: my detector run for " + file.getName() + "; result: " + (result == null ? null : result.getName()));
            return result;
        }

        @Override
        public int getVersion() {
            return 0;
        }
    };
    Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).registerExtension(detector);
    myFileTypeManager.toLog = true;
    try {
        log("T: ------ akjdhfksdjgf");
        File f = createTempFile("xx.asfdasdfas", "akjdhfksdjgf");
        VirtualFile vFile = getVirtualFile(f);
        ensureRedetected(vFile, detectorCalled);
        assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof PlainTextFileType);
        log("T: ------ TYPE:IDEA_MODULE");
        setFileText(vFile, "TYPE:IDEA_MODULE");
        ensureRedetected(vFile, detectorCalled);
        assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ModuleFileType);
        log("T: ------ TYPE:IDEA_PROJECT");
        setFileText(vFile, "TYPE:IDEA_PROJECT");
        ensureRedetected(vFile, detectorCalled);
        assertTrue(vFile.getFileType().toString(), vFile.getFileType() instanceof ProjectFileType);
        log("T: ------");
    } finally {
        Extensions.getRootArea().getExtensionPoint(FileTypeRegistry.FileTypeDetector.EP_NAME).unregisterExtension(detector);
        myFileTypeManager.toLog = false;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) NotNull(org.jetbrains.annotations.NotNull) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) ArchiveFileType(com.intellij.ide.highlighter.ArchiveFileType) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPlainTextFile(com.intellij.psi.PsiPlainTextFile) PsiFile(com.intellij.psi.PsiFile) PsiBinaryFile(com.intellij.psi.PsiBinaryFile) File(java.io.File) ByteSequence(com.intellij.openapi.util.io.ByteSequence) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ArchiveFileType (com.intellij.ide.highlighter.ArchiveFileType)1 ModuleFileType (com.intellij.ide.highlighter.ModuleFileType)1 ProjectFileType (com.intellij.ide.highlighter.ProjectFileType)1 ByteSequence (com.intellij.openapi.util.io.ByteSequence)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiBinaryFile (com.intellij.psi.PsiBinaryFile)1 PsiFile (com.intellij.psi.PsiFile)1 PsiPlainTextFile (com.intellij.psi.PsiPlainTextFile)1 File (java.io.File)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1