Java Code Examples for com.intellij.openapi.fileTypes.UnknownFileType#INSTANCE

The following examples show how to use com.intellij.openapi.fileTypes.UnknownFileType#INSTANCE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorDiffTool.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private JComponent createComponent(@Nonnull DiffRequest request) {
  if (request instanceof MessageDiffRequest) {
    // TODO: explain some of ErrorDiffRequest exceptions ?
    String message = ((MessageDiffRequest)request).getMessage();
    return DiffUtil.createMessagePanel(message);
  }
  if (request instanceof ComponentDiffRequest) {
    return ((ComponentDiffRequest)request).getComponent(myContext);
  }
  if (request instanceof ContentDiffRequest) {
    List<DiffContent> contents = ((ContentDiffRequest)request).getContents();
    for (final DiffContent content : contents) {
      if (content instanceof FileContent && UnknownFileType.INSTANCE == content.getContentType()) {
        final VirtualFile file = ((FileContent)content).getFile();

        UnknownFileTypeDiffRequest unknownFileTypeRequest = new UnknownFileTypeDiffRequest(file, myRequest.getTitle());
        return unknownFileTypeRequest.getComponent(myContext);
      }
    }
  }

  return DiffUtil.createMessagePanel("Can't show diff");
}
 
Example 2
Source File: ErrorDiffTool.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
@Nonnull
@Override
public ToolbarComponents init() {
  if (myRequest instanceof UnknownFileTypeDiffRequest) {
    String fileName = ((UnknownFileTypeDiffRequest)myRequest).getFileName();
    if (fileName != null && FileTypeManager.getInstance().getFileTypeByFileName(fileName) != UnknownFileType.INSTANCE) {
      // FileType was assigned elsewhere (ex: by other UnknownFileTypeDiffRequest). We should reload request.
      if (myContext instanceof DiffContextEx) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          @Override
          public void run() {
            ((DiffContextEx)myContext).reloadDiffRequest();
          }
        }, ModalityState.current());
      }
    }
  }

  return new ToolbarComponents();
}
 
Example 3
Source File: DiffContentFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static DocumentContent createImpl(@javax.annotation.Nullable Project project,
                                          @Nonnull String text,
                                          @Nullable FileType fileType,
                                          @Nullable String fileName,
                                          @Nullable VirtualFile highlightFile,
                                          @javax.annotation.Nullable Charset charset,
                                          @javax.annotation.Nullable Boolean bom,
                                          boolean respectLineSeparators,
                                          boolean readOnly) {
  if (UnknownFileType.INSTANCE == fileType) fileType = PlainTextFileType.INSTANCE;

  // TODO: detect invalid (different across the file) separators ?
  LineSeparator separator = respectLineSeparators ? StringUtil.detectSeparators(text) : null;
  String correctedContent = StringUtil.convertLineSeparators(text);

  Document document = createDocument(project, correctedContent, fileType, fileName, readOnly);
  DocumentContent content = new DocumentContentImpl(project, document, fileType, highlightFile, separator, charset, bom);

  if (fileName != null) content.putUserData(DiffUserDataKeysEx.FILE_NAME, fileName);

  return content;
}
 
Example 4
Source File: GotoFileAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int compare(final FileType o1, final FileType o2) {
  if (o1 == o2) {
    return 0;
  }
  if (o1 == UnknownFileType.INSTANCE) {
    return 1;
  }
  if (o2 == UnknownFileType.INSTANCE) {
    return -1;
  }
  if (o1.isBinary() && !o2.isBinary()) {
    return 1;
  }
  if (!o1.isBinary() && o2.isBinary()) {
    return -1;
  }
  return o1.getName().compareToIgnoreCase(o2.getName());
}
 
Example 5
Source File: NavigationUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean openFileWithPsiElement(PsiElement element, boolean searchForOpen, boolean requestFocus) {
  boolean openAsNative = false;
  if (element instanceof PsiFile) {
    VirtualFile virtualFile = ((PsiFile)element).getVirtualFile();
    if (virtualFile != null) {
      openAsNative = virtualFile.getFileType() instanceof INativeFileType || virtualFile.getFileType() == UnknownFileType.INSTANCE;
    }
  }

  if (searchForOpen) {
    element.putUserData(FileEditorManager.USE_CURRENT_WINDOW, null);
  }
  else {
    element.putUserData(FileEditorManager.USE_CURRENT_WINDOW, true);
  }

  if (openAsNative || !activatePsiElementIfOpen(element, searchForOpen, requestFocus)) {
    final NavigationItem navigationItem = (NavigationItem)element;
    if (!navigationItem.canNavigate()) return false;
    navigationItem.navigate(requestFocus);
    return true;
  }

  element.putUserData(FileEditorManager.USE_CURRENT_WINDOW, null);
  return false;
}
 
Example 6
Source File: AssociateFileType.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void update(AnActionEvent e) {
  Presentation presentation = e.getPresentation();
  VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE);
  Project project = e.getData(CommonDataKeys.PROJECT);
  boolean haveSmthToDo;
  if (project == null || file == null || file.isDirectory()) {
    haveSmthToDo = false;
  }
  else {
    // the action should also be available for files which have been auto-detected as text or as a particular language (IDEA-79574)
    haveSmthToDo = FileTypeManager.getInstance().getFileTypeByFileName(file.getName()) == UnknownFileType.INSTANCE;
  }
  presentation.setVisible(haveSmthToDo || ActionPlaces.MAIN_MENU.equals(e.getPlace()));
  presentation.setEnabled(haveSmthToDo);
}
 
Example 7
Source File: PathsVerifier.java    From consulo with Apache License 2.0 6 votes vote down vote up
private boolean isFileTypeOk(@Nonnull VirtualFile file) {
  FileType fileType = file.getFileType();
  if (fileType == UnknownFileType.INSTANCE) {
    fileType = FileTypeChooser.associateFileType(file.getName());
    if (fileType == null) {
      PatchApplier
              .showError(myProject, "Cannot apply content for " + file.getPresentableName() + " file from patch because its type not defined.",
                         true);
      return false;
    }
  }
  if (fileType.isBinary()) {
    PatchApplier.showError(myProject, "Cannot apply file " + file.getPresentableName() + " from patch because it is binary.", true);
    return false;
  }
  return true;
}
 
Example 8
Source File: ChooseByNameBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isFileName(@Nonnull String name) {
  final int index = name.lastIndexOf('.');
  if (index > 0) {
    String ext = name.substring(index + 1);
    if (ext.contains(":")) {
      ext = ext.substring(0, ext.indexOf(':'));
    }
    if (FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(ext) != UnknownFileType.INSTANCE) {
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: CompareClipboardWithSelectionAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static FileType getEditorFileType(@Nonnull AnActionEvent e) {
  DiffContent content = e.getData(DiffDataKeys.CURRENT_CONTENT);
  if (content != null && content.getContentType() != null) return content.getContentType();

  DiffRequest request = e.getData(DiffDataKeys.DIFF_REQUEST);
  if (request instanceof ContentDiffRequest) {
    for (DiffContent diffContent : ((ContentDiffRequest)request).getContents()) {
      FileType type = diffContent.getContentType();
      if (type != null && type != UnknownFileType.INSTANCE) return type;
    }
  }

  return null;
}
 
Example 10
Source File: IdeConsoleRootType.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Image substituteIcon(@Nonnull Project project, @Nonnull VirtualFile file) {
  if (file.isDirectory()) return null;
  FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(file.getNameSequence());
  Image icon = fileType == UnknownFileType.INSTANCE || fileType == PlainTextFileType.INSTANCE ? AllIcons.Debugger.Console : ObjectUtils.notNull(fileType.getIcon(), AllIcons.Debugger.Console);
  return ImageEffects.layered(icon, AllIcons.Nodes.RunnableMark);
}
 
Example 11
Source File: DocumentationManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String generateFileDoc(@Nonnull PsiFile psiFile, boolean withUrl) {
  VirtualFile file = PsiUtilCore.getVirtualFile(psiFile);
  File ioFile = file == null || !file.isInLocalFileSystem() ? null : VfsUtilCore.virtualToIoFile(file);
  BasicFileAttributes attr = null;
  try {
    attr = ioFile == null ? null : Files.readAttributes(Paths.get(ioFile.toURI()), BasicFileAttributes.class);
  }
  catch (Exception ignored) {
  }
  if (attr == null) return null;
  FileType type = file.getFileType();
  String typeName = type == UnknownFileType.INSTANCE ? "Unknown" : type == PlainTextFileType.INSTANCE ? "Text" : type instanceof ArchiveFileType ? "Archive" : type.getId();
  String languageName = type.isBinary() ? "" : psiFile.getLanguage().getDisplayName();
  return (withUrl ? DocumentationMarkup.DEFINITION_START + file.getPresentableUrl() + DocumentationMarkup.DEFINITION_END + DocumentationMarkup.CONTENT_START : "") +
         getVcsStatus(psiFile.getProject(), file) +
         getScope(psiFile.getProject(), file) +
         "<p><span class='grayed'>Size:</span> " +
         StringUtil.formatFileSize(attr.size()) +
         "<p><span class='grayed'>Type:</span> " +
         typeName +
         (type.isBinary() || typeName.equals(languageName) ? "" : " (" + languageName + ")") +
         "<p><span class='grayed'>Modified:</span> " +
         DateFormatUtil.formatDateTime(attr.lastModifiedTime().toMillis()) +
         "<p><span class='grayed'>Created:</span> " +
         DateFormatUtil.formatDateTime(attr.creationTime().toMillis()) +
         (withUrl ? DocumentationMarkup.CONTENT_END : "");
}
 
Example 12
Source File: ChangeDiffRequestPresentable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean checkContentRevision(ContentRevision rev, final DiffChainContext context, final List<String> errSb) {
  if (rev == null) return true;
  if (rev.getFile().isDirectory()) return false;
  if (! hasContents(rev, errSb)) {
    return false;
  }
  final FileType type = rev.getFile().getFileType();
  if (! type.isBinary()) return true;
  if (type == UnknownFileType.INSTANCE) {
    final boolean associatedToText = checkAssociate(myProject, rev.getFile().getName(), context);
  }
  return true;
}
 
Example 13
Source File: FileReference.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
public String getNewFileTemplateName() {
  FileType fileType = FileTypeRegistry.getInstance().getFileTypeByFileName(myText);
  if (fileType != UnknownFileType.INSTANCE) {
    return fileType.getName() + " File." + fileType.getDefaultExtension();
  }
  return null;
}
 
Example 14
Source File: PlatformTestUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void assertFilesEqual(VirtualFile fileAfter, VirtualFile fileBefore) throws IOException {
  try {
    assertJarFilesEqual(VfsUtilCore.virtualToIoFile(fileAfter), VfsUtilCore.virtualToIoFile(fileBefore));
  }
  catch (IOException e) {
    FileDocumentManager manager = FileDocumentManager.getInstance();

    Document docBefore = manager.getDocument(fileBefore);
    boolean canLoadBeforeText = !fileBefore.getFileType().isBinary() || fileBefore.getFileType() == UnknownFileType.INSTANCE;
    String textB = docBefore != null
                   ? docBefore.getText()
                   : !canLoadBeforeText
                     ? null
                     : LoadTextUtil.getTextByBinaryPresentation(fileBefore.contentsToByteArray(false), fileBefore).toString();

    Document docAfter = manager.getDocument(fileAfter);
    boolean canLoadAfterText = !fileBefore.getFileType().isBinary() || fileBefore.getFileType() == UnknownFileType.INSTANCE;
    String textA = docAfter != null
                   ? docAfter.getText()
                   : !canLoadAfterText
                     ? null
                     : LoadTextUtil.getTextByBinaryPresentation(fileAfter.contentsToByteArray(false), fileAfter).toString();

    if (textA != null && textB != null) {
      assertEquals(fileAfter.getPath(), textA, textB);
    }
    else {
      Assert.assertArrayEquals(fileAfter.getPath(), fileAfter.contentsToByteArray(), fileBefore.contentsToByteArray());
    }
  }
}
 
Example 15
Source File: TextFilePatchInProgress.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public DiffRequestProducer getDiffRequestProducers(final Project project, final PatchReader patchReader) {
  final PatchChange change = getChange();
  final FilePatch patch = getPatch();
  final String path = patch.getBeforeName() == null ? patch.getAfterName() : patch.getBeforeName();
  final Getter<CharSequence> baseContentGetter = new Getter<CharSequence>() {
    @Override
    public CharSequence get() {
      return patchReader.getBaseRevision(project, path);
    }
  };
  return new DiffRequestProducer() {
    @Nonnull
    @Override
    public DiffRequest process(@Nonnull UserDataHolder context, @Nonnull ProgressIndicator indicator)
            throws DiffRequestProducerException, ProcessCanceledException {
      if (myCurrentBase != null && myCurrentBase.getFileType() == UnknownFileType.INSTANCE) {
        return new UnknownFileTypeDiffRequest(myCurrentBase, getName());
      }

      if (isConflictingChange()) {
        final VirtualFile file = getCurrentBase();

        Getter<ApplyPatchForBaseRevisionTexts> getter = new Getter<ApplyPatchForBaseRevisionTexts>() {
          @Override
          public ApplyPatchForBaseRevisionTexts get() {
            return ApplyPatchForBaseRevisionTexts.create(project, file, VcsUtil.getFilePath(file), getPatch(), baseContentGetter);
          }
        };

        String afterTitle = getPatch().getAfterVersionId();
        if (afterTitle == null) afterTitle = "Patched Version";
        return PatchDiffRequestFactory.createConflictDiffRequest(project, file, getPatch(), afterTitle, getter, getName(), context, indicator);
      }
      else {
        return PatchDiffRequestFactory.createDiffRequest(project, change, getName(), context, indicator);
      }
    }

    @Nonnull
    @Override
    public String getName() {
      final File ioCurrentBase = getIoCurrentBase();
      return ioCurrentBase == null ? getCurrentPath() : ioCurrentBase.getPath();
    }
  };
}
 
Example 16
Source File: CoreFileTypeRegistry.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public FileType getFileTypeByExtension(@NonNls @Nonnull String extension) {
  final FileType result = myExtensionsMap.get(extension);
  return result == null ? UnknownFileType.INSTANCE : result;
}
 
Example 17
Source File: DummyHolderViewProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DummyHolderViewProvider(@Nonnull PsiManager manager) {
  super(manager, new LightVirtualFile("DummyHolder", UnknownFileType.INSTANCE, ""), false);
  myModificationStamp = LocalTimeCounter.currentTime();
}
 
Example 18
Source File: LocalFileExternalizer.java    From consulo with Apache License 2.0 4 votes vote down vote up
static boolean canExternalizeAsFile(VirtualFile file) {
  if (file == null || file.isDirectory()) return false;
  FileType fileType = file.getFileType();
  if (fileType.isBinary() && fileType != UnknownFileType.INSTANCE) return false;
  return true;
}
 
Example 19
Source File: LightFileTypeRegistry.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public FileType getFileTypeByExtension(@NonNls @Nonnull String extension) {
  final FileType result = myExtensionsMap.get(extension);
  return result == null ? UnknownFileType.INSTANCE : result;
}
 
Example 20
Source File: UnknownFileTypeDiffRequest.java    From consulo with Apache License 2.0 4 votes vote down vote up
public UnknownFileTypeDiffRequest(@Nonnull String fileName, @Nullable String title) {
  boolean knownFileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName) != UnknownFileType.INSTANCE;
  myFileName = knownFileType ? null : fileName;
  myTitle = title;
}