Search in sources :

Example 1 with ProjectFileType

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

the class OpenFileAction method doOpenFile.

private static void doOpenFile(@Nullable Project project, @NotNull List<VirtualFile> result) {
    for (VirtualFile file : result) {
        if (file.isDirectory()) {
            Project openedProject;
            if (ProjectAttachProcessor.canAttachToProject()) {
                EnumSet<PlatformProjectOpenProcessor.Option> options = EnumSet.noneOf(PlatformProjectOpenProcessor.Option.class);
                openedProject = PlatformProjectOpenProcessor.doOpenProject(file, project, -1, null, options);
            } else {
                openedProject = ProjectUtil.openOrImport(file.getPath(), project, false);
            }
            FileChooserUtil.setLastOpenedFile(openedProject, file);
            return;
        }
        // try to open as a project - unless the file is an .ipr of the current one
        if ((project == null || !file.equals(project.getProjectFile())) && OpenProjectFileChooserDescriptor.isProjectFile(file)) {
            int answer = file.getFileType() instanceof ProjectFileType ? Messages.YES : Messages.showYesNoCancelDialog(project, IdeBundle.message("message.open.file.is.project", file.getName()), IdeBundle.message("title.open.project"), IdeBundle.message("message.open.file.is.project.open.as.project"), IdeBundle.message("message.open.file.is.project.open.as.file"), IdeBundle.message("button.cancel"), Messages.getQuestionIcon());
            if (answer == Messages.CANCEL)
                return;
            if (answer == Messages.YES) {
                Project openedProject = ProjectUtil.openOrImport(file.getPath(), project, false);
                if (openedProject != null) {
                    FileChooserUtil.setLastOpenedFile(openedProject, file);
                }
                return;
            }
        }
        FileType type = FileTypeChooser.getKnownFileTypeOrAssociate(file, project);
        if (type == null)
            return;
        if (project != null) {
            openFile(file, project);
        } else {
            PlatformProjectOpenProcessor processor = PlatformProjectOpenProcessor.getInstanceIfItExists();
            if (processor != null) {
                processor.doOpenProject(file, null, false);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PlatformProjectOpenProcessor(com.intellij.platform.PlatformProjectOpenProcessor) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) ProjectFileType(com.intellij.ide.highlighter.ProjectFileType) FileType(com.intellij.openapi.fileTypes.FileType)

Example 2 with ProjectFileType

use of com.intellij.ide.highlighter.ProjectFileType 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

ProjectFileType (com.intellij.ide.highlighter.ProjectFileType)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 ArchiveFileType (com.intellij.ide.highlighter.ArchiveFileType)1 ModuleFileType (com.intellij.ide.highlighter.ModuleFileType)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 ByteSequence (com.intellij.openapi.util.io.ByteSequence)1 PlatformProjectOpenProcessor (com.intellij.platform.PlatformProjectOpenProcessor)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