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

The following examples show how to use com.intellij.openapi.fileTypes.UnknownFileType. These examples are extracted from open source projects. 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 Project: consulo   Source File: ErrorDiffTool.java    License: 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 Project: consulo   Source File: ErrorDiffTool.java    License: 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 Project: consulo   Source File: DiffContentFactoryImpl.java    License: 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 Project: consulo   Source File: AssociateFileType.java    License: 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 5
Source Project: consulo   Source File: PsiFileImplUtil.java    License: Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
public static PsiFile setName(final PsiFile file, String newName) throws IncorrectOperationException {
  VirtualFile vFile = file.getViewProvider().getVirtualFile();
  PsiManagerImpl manager = (PsiManagerImpl)file.getManager();

  try{
    final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName);
    if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) {
      // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss
      final FileDocumentManager fdm = FileDocumentManager.getInstance();
      final Document doc = fdm.getCachedDocument(vFile);
      if (doc != null) {
        fdm.saveDocumentAsIs(doc);
      }
    }

    vFile.rename(manager, newName);
  }
  catch(IOException e){
    throw new IncorrectOperationException(e);
  }

  return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file;
}
 
Example 6
Source Project: consulo   Source File: PathsVerifier.java    License: 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 7
Source Project: consulo   Source File: NavigationUtil.java    License: 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 8
Source Project: consulo   Source File: GotoFileAction.java    License: 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 9
Source Project: intellij-demandware   Source File: DSFileTypeAssociator.java    License: MIT License 5 votes vote down vote up
@Override
public void initComponent() {
    FileType javaScriptFileType = fileTypeManager.getFileTypeByExtension("js");
    if (!(javaScriptFileType instanceof UnknownFileType)) {
        fileTypeManager.associateExtension(javaScriptFileType, "ds");
    }
}
 
Example 10
@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 11
Source Project: consulo   Source File: PlatformTestUtil.java    License: 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 12
Source Project: consulo   Source File: AutoScrollToSourceHandler.java    License: Apache License 2.0 5 votes vote down vote up
protected void scrollToSource(final Component tree) {
  DataContext dataContext=DataManager.getInstance().getDataContext(tree);
  getReady(dataContext).doWhenDone(new Runnable() {
    @Override
    public void run() {
      DataContext context = DataManager.getInstance().getDataContext(tree);
      final VirtualFile vFile = context.getData(PlatformDataKeys.VIRTUAL_FILE);
      if (vFile != null) {
        // Attempt to navigate to the virtual file with unknown file type will show a modal dialog
        // asking to register some file type for this file. This behaviour is undesirable when autoscrolling.
        if (vFile.getFileType() == UnknownFileType.INSTANCE || vFile.getFileType() instanceof INativeFileType) return;

        //IDEA-84881 Don't autoscroll to very large files
        if (vFile.getLength() > PersistentFSConstants.getMaxIntellisenseFileSize()) return;
      }
      Navigatable[] navigatables = context.getData(PlatformDataKeys.NAVIGATABLE_ARRAY);
      if (navigatables != null) {
        if (navigatables.length > 1) {
          return;
        }
        for (Navigatable navigatable : navigatables) {
          // we are not going to open modal dialog during autoscrolling
          if (!navigatable.canNavigateToSource()) return;
        }
      }
      OpenSourceUtil.openSourcesFrom(context, false);
    }
  });
}
 
Example 13
Source Project: consulo   Source File: FileReference.java    License: 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 Project: consulo   Source File: VcsUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Collects all files which are located in the passed directory.
 *
 * @throws IllegalArgumentException if <code>dir</code> isn't a directory.
 */
public static void collectFiles(final VirtualFile dir,
                                final List<VirtualFile> files,
                                final boolean recursive,
                                final boolean addDirectories) {
  if (!dir.isDirectory()) {
    throw new IllegalArgumentException(VcsBundle.message("exception.text.file.should.be.directory", dir.getPresentableUrl()));
  }

  final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
  VfsUtilCore.visitChildrenRecursively(dir, new VirtualFileVisitor() {
    @Override
    public boolean visitFile(@Nonnull VirtualFile file) {
      if (file.isDirectory()) {
        if (addDirectories) {
          files.add(file);
        }
        if (!recursive && !Comparing.equal(file, dir)) {
          return false;
        }
      }
      else if (fileTypeManager == null || file.getFileType() != UnknownFileType.INSTANCE) {
        files.add(file);
      }
      return true;
    }
  });
}
 
Example 15
Source Project: consulo   Source File: ChangeDiffRequestPresentable.java    License: 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 16
Source Project: consulo   Source File: DocumentationManager.java    License: 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 17
Source Project: consulo   Source File: IdeConsoleRootType.java    License: 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 18
@Override
@Nonnull
public StructureViewModel createStructureViewModel(@Nullable Editor editor) {
  List<StructureViewComposite.StructureViewDescriptor> viewDescriptors = new ArrayList<>();
  PsiFile psiFile = ObjectUtils.notNull(PsiManager.getInstance(myProject).findFile(myVirtualFile));
  for (Language language : getLanguages(psiFile)) {
    StructureViewBuilder builder = getBuilder(psiFile, language);
    if (!(builder instanceof TreeBasedStructureViewBuilder)) continue;
    StructureViewModel model = ((TreeBasedStructureViewBuilder)builder).createStructureViewModel(editor);
    String title = language.getDisplayName();
    Image icon = ObjectUtils.notNull(LanguageUtil.getLanguageFileType(language), UnknownFileType.INSTANCE).getIcon();
    viewDescriptors.add(new StructureViewComposite.StructureViewDescriptor(title, model, icon));
  }
  return new StructureViewCompositeModel(psiFile, editor, viewDescriptors);
}
 
Example 19
Source Project: consulo   Source File: ChooseByNameBase.java    License: 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 20
Source Project: reasonml-idea-plugin   Source File: CmtFileType.java    License: MIT License 4 votes vote down vote up
@Nullable
@Override
public Icon getIcon() {
    return UnknownFileType.INSTANCE.getIcon();
}
 
Example 21
Source Project: consulo   Source File: UnknownFileTypeDiffRequest.java    License: 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;
}
 
Example 22
Source Project: consulo   Source File: LightFileTypeRegistry.java    License: Apache License 2.0 4 votes vote down vote up
public LightFileTypeRegistry() {
  myAllFileTypes.add(UnknownFileType.INSTANCE);
}
 
Example 23
Source Project: consulo   Source File: LightFileTypeRegistry.java    License: 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 24
Source Project: consulo   Source File: DiffManagerTest.java    License: Apache License 2.0 4 votes vote down vote up
public void addContent() {
  addContent(new BinaryContent(ArrayUtil.EMPTY_BYTE_ARRAY, null, UnknownFileType.INSTANCE), "");
}
 
Example 25
Source Project: consulo   Source File: FileContent.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isUnknown(@Nonnull FileType type) {
  return type.equals(UnknownFileType.INSTANCE);
}
 
Example 26
Source Project: consulo   Source File: DesktopEditorWindow.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return icon which represents file's type and modification status
 */
@Nullable
private consulo.ui.image.Image getFileIcon(@Nonnull final VirtualFile file) {
  if (!file.isValid()) {
    return UnknownFileType.INSTANCE.getIcon();
  }

  final Image baseIcon = VfsIconUtil.getIcon(file, Iconable.ICON_FLAG_READ_STATUS, getManager().getProject());

  int count = 1;

  final Image pinIcon;
  final DesktopEditorComposite composite = findFileComposite(file);
  if (composite != null && composite.isPinned()) {
    count++;
    pinIcon = AllIcons.Nodes.TabPin;
  }
  else {
    pinIcon = null;
  }

  // FIXME [VISTALL] not supported for now
  Icon modifiedIcon = null;
  //UISettings settings = UISettings.getInstance();
  //if (settings.getMarkModifiedTabsWithAsterisk() || !settings.getHideTabsIfNeed()) {
  //  modifiedIcon = settings.getMarkModifiedTabsWithAsterisk() && composite != null && composite.isModified() ? MODIFIED_ICON : GAP_ICON;
  //  count++;
  //}
  //else {
  //  modifiedIcon = null;
  //}
  //
  //if (count == 1) return baseIcon;

  if(pinIcon != null && modifiedIcon == null) {
    return ImageEffects.layered(baseIcon, pinIcon);
  }

  // FIXME [VISTALL] not supported for now
  //int i = 0;
  //final LayeredIcon result = new LayeredIcon(count);
  //int xShift = !settings.getHideTabsIfNeed() ? 4 : 0;
  //result.setIcon(baseIcon, i++, xShift, 0);
  //if (pinIcon != null) result.setIcon(pinIcon, i++, xShift, 0);
  //if (modifiedIcon != null) result.setIcon(modifiedIcon, i++);
  //
  //return JBUI.scale(result);
  return baseIcon;
}
 
Example 27
Source Project: consulo   Source File: DiffUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isUnknownFileType(@Nonnull DiffContent diffContent) {
  return UnknownFileType.INSTANCE.equals(diffContent.getContentType());
}
 
Example 28
Source Project: consulo   Source File: LocalFileExternalizer.java    License: 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 29
Source Project: consulo   Source File: DummyHolderViewProvider.java    License: 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 30
Source Project: consulo   Source File: CoreFileTypeRegistry.java    License: Apache License 2.0 4 votes vote down vote up
public CoreFileTypeRegistry() {
  myAllFileTypes.add(UnknownFileType.INSTANCE);
}