Java Code Examples for com.intellij.openapi.fileTypes.FileType#isBinary()

The following examples show how to use com.intellij.openapi.fileTypes.FileType#isBinary() . 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
public static boolean checkAssociate(final Project project, String fileName, DiffChainContext context) {
  final String pattern = FileUtilRt.getExtension(fileName).toLowerCase();
  if (context.contains(pattern)) return false;
  int rc = Messages.showOkCancelDialog(project,
                                       VcsBundle.message("diff.unknown.file.type.prompt", fileName),
                                       VcsBundle.message("diff.unknown.file.type.title"),
                                       VcsBundle.message("diff.unknown.file.type.associate"),
                                       CommonBundle.getCancelButtonText(),
                                       Messages.getQuestionIcon());
  if (rc == Messages.OK) {
    FileType fileType = FileTypeChooser.associateFileType(fileName);
    return fileType != null && !fileType.isBinary();
  } else {
    context.add(pattern);
  }
  return false;
}
 
Example 2
Source Project: consulo   File: AnnotateRevisionAction.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
protected VirtualFile getFile(@Nonnull AnActionEvent e) {
  VcsFileRevision revision = getFileRevision(e);
  if (revision == null) return null;

  final FileType currentFileType = myAnnotation.getFile().getFileType();
  FilePath filePath = (revision instanceof VcsFileRevisionEx ? ((VcsFileRevisionEx)revision).getPath() : VcsUtil.getFilePath(myAnnotation.getFile()));
  return new VcsVirtualFile(filePath.getPath(), revision, VcsFileSystem.getInstance()) {
    @Nonnull
    @Override
    public FileType getFileType() {
      FileType type = super.getFileType();
      if (!type.isBinary()) return type;
      if (!currentFileType.isBinary()) return currentFileType;
      return PlainTextFileType.INSTANCE;
    }
  };
}
 
Example 3
Source Project: consulo   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 4
Source Project: consulo   File: FileTextRule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String getData(@Nonnull DataProvider dataProvider) {
  final VirtualFile virtualFile = dataProvider.getDataUnchecked(PlatformDataKeys.VIRTUAL_FILE);
  if (virtualFile == null) {
    return null;
  }

  final FileType fileType = virtualFile.getFileType();
  if (fileType.isBinary() || fileType.isReadOnly()) {
    return null;
  }

  final Project project = dataProvider.getDataUnchecked(CommonDataKeys.PROJECT);
  if (project == null) {
    return null;
  }

  final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
  if (document == null) {
    return null;
  }

  return document.getText();
}
 
Example 5
Source Project: consulo   File: LoadTextUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static CharSequence loadText(@Nonnull final VirtualFile file) {
  FileType type = file.getFileType();
  if (type.isBinary()) {
    final BinaryFileDecompiler decompiler = BinaryFileTypeDecompilers.INSTANCE.forFileType(type);
    if (decompiler != null) {
      CharSequence text = decompiler.decompile(file);
      try {
        StringUtil.assertValidSeparators(text);
      }
      catch (AssertionError e) {
        LOG.error(e);
      }
      return text;
    }

    throw new IllegalArgumentException("Attempt to load text for binary file which doesn't have a decompiler plugged in: " + file.getPresentableUrl() + ". File type: " + type.getName());
  }
  return loadText(file, UNLIMITED);
}
 
Example 6
Source Project: consulo   File: LoadTextUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Loads content of given virtual file. If limit is {@value UNLIMITED} then full CharSequence will be returned. Else CharSequence
 * will be truncated by limit if it has bigger length.
 *
 * @param file  Virtual file for content loading
 * @param limit Maximum characters count or {@value UNLIMITED}
 * @return Full or truncated CharSequence with file content
 * @throws IllegalArgumentException for binary files
 */
@Nonnull
public static CharSequence loadText(@Nonnull final VirtualFile file, int limit) {
  FileType type = file.getFileType();
  if (type.isBinary()) throw new IllegalArgumentException("Attempt to load truncated text for binary file: " + file.getPresentableUrl() + ". File type: " + type.getName());

  if (file instanceof LightVirtualFile) {
    return limitCharSequence(((LightVirtualFile)file).getContent(), limit);
  }

  if (file.isDirectory()) {
    throw new AssertionError("'" + file.getPresentableUrl() + "' is a directory");
  }
  try {
    byte[] bytes = limit == UNLIMITED ? file.contentsToByteArray() : FileUtil.loadFirstAndClose(file.getInputStream(), limit);
    return getTextByBinaryPresentation(bytes, file);
  }
  catch (IOException e) {
    return ArrayUtil.EMPTY_CHAR_SEQUENCE;
  }
}
 
Example 7
@Nonnull
protected PsiFile createFile(@Nonnull VirtualFile file, @Nonnull FileType fileType, @Nonnull Language language) {
  if (fileType.isBinary() || file.is(VFileProperty.SPECIAL)) {
    return SingleRootFileViewProvider.isTooLargeForContentLoading(file) ? new PsiLargeBinaryFileImpl((PsiManagerImpl)getManager(), this) : new PsiBinaryFileImpl((PsiManagerImpl)getManager(), this);
  }
  if (!SingleRootFileViewProvider.isTooLargeForIntelligence(file)) {
    final PsiFile psiFile = createFile(language);
    if (psiFile != null) return psiFile;
  }

  if (SingleRootFileViewProvider.isTooLargeForContentLoading(file)) {
    return new PsiLargeTextFileImpl(this);
  }

  return new PsiPlainTextFileImpl(this);
}
 
Example 8
Source Project: consulo   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 9
public static boolean isBPMN(@NotNull VirtualFile virtualFile) {
    if (BPMN_EXTENSION.equals(virtualFile.getExtension())) {
        final FileType fileType = virtualFile.getFileType();
        if (fileType == getFileType() && !fileType.isBinary()) {
            return virtualFile.getName().endsWith(DOT_BPMN_EXTENSION);
        }
    }
    return false;
}
 
Example 10
Source Project: p4ic4idea   File: P4FileType.java    License: Apache License 2.0 5 votes vote down vote up
public static P4FileType getFileType(FilePath fp) {
    FileType ft = fp.getFileType();
    if (ft.isBinary()) {
        return P4FileType.convert("binary");
    }
    return P4FileType.convert("text");
}
 
Example 11
Source Project: consulo   File: BinaryContent.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param charset use to convert bytes to String. null means bytes can't be converted to text. Has no sense if fileType.isBinary()
 * @param fileType type of content
 */
public BinaryContent(@Nonnull Project project, byte[] bytes, @Nullable Charset charset, @Nonnull FileType fileType,
                     @Nullable String filePath) {
  myProject = project;
  myFileType = fileType;
  myBytes = bytes;
  if (fileType.isBinary()) {
    myCharset = null;
  }
  else {
    myCharset = charset;
  }
  myFilePath = filePath;
}
 
Example 12
Source Project: consulo   File: TextEditorProvider.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isTextFile(@Nonnull VirtualFile file) {
  if (file.isDirectory() || !file.isValid()) {
    return false;
  }

  final FileType ft = file.getFileType();
  return !ft.isBinary() || BinaryFileTypeDecompilers.INSTANCE.forFileType(ft) != null;
}
 
Example 13
@Override
protected boolean isValueEditableForFile(final VirtualFile virtualFile) {
  boolean enabled = true;
  if (virtualFile != null) {
    if (!virtualFile.isDirectory()) {
      final FileType fileType = virtualFile.getFileType();
      if (fileType.isBinary()) {
        enabled = false;
      }
    }
  }
  return enabled;
}
 
Example 14
Source Project: consulo   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 15
Source Project: consulo   File: DaemonCodeAnalyzerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean isHighlightingAvailable(@Nullable PsiFile file) {
  if (file == null || !file.isPhysical()) return false;
  if (myDisabledHighlightingFiles.contains(PsiUtilCore.getVirtualFile(file))) return false;

  if (file instanceof PsiCompiledElement) return false;
  final FileType fileType = file.getFileType();

  // To enable T.O.D.O. highlighting
  return !fileType.isBinary();
}
 
Example 16
private static boolean isBinaryWithoutDecompiler(VirtualFile file) {
  final FileType ft = file.getFileType();
  return ft.isBinary() && BinaryFileTypeDecompilers.INSTANCE.forFileType(ft) == null;
}
 
Example 17
Source Project: consulo   File: FileDocumentManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isBinaryWithoutDecompiler(@Nonnull VirtualFile file) {
  final FileType fileType = file.getFileType();
  return fileType.isBinary() && BinaryFileTypeDecompilers.INSTANCE.forFileType(fileType) == null;
}
 
Example 18
Source Project: consulo   File: TrigramIndex.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isIndexable(FileType fileType) {
  return ENABLED && !fileType.isBinary();
}
 
Example 19
Source Project: consulo   File: RangeMarkerImpl.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isBinaryWithoutDecompiler(@Nonnull VirtualFile file) {
  final FileType fileType = file.getFileType();
  return fileType.isBinary() && BinaryFileTypeDecompilers.INSTANCE.forFileType(fileType) == null;
}
 
Example 20
Source Project: consulo   File: TrafficLightRenderer.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
protected DaemonCodeAnalyzerStatus getDaemonCodeAnalyzerStatus(@Nonnull SeverityRegistrar severityRegistrar) {
  DaemonCodeAnalyzerStatus status = new DaemonCodeAnalyzerStatus();
  PsiFile psiFile = getPsiFile();
  if (psiFile == null) {
    status.reasonWhyDisabled = DaemonBundle.message("process.title.no.file");
    status.errorAnalyzingFinished = true;
    return status;
  }
  if (myProject.isDisposed()) {
    status.reasonWhyDisabled = DaemonBundle.message("process.title.project.is.disposed");
    status.errorAnalyzingFinished = true;
    return status;
  }
  if (!myDaemonCodeAnalyzer.isHighlightingAvailable(psiFile)) {
    if (!psiFile.isPhysical()) {
      status.reasonWhyDisabled = DaemonBundle.message("process.title.file.is.generated");
      status.errorAnalyzingFinished = true;
      return status;
    }
    if (psiFile instanceof PsiCompiledElement) {
      status.reasonWhyDisabled = DaemonBundle.message("process.title.file.is.decompiled");
      status.errorAnalyzingFinished = true;
      return status;
    }
    final FileType fileType = psiFile.getFileType();
    if (fileType.isBinary()) {
      status.reasonWhyDisabled = DaemonBundle.message("process.title.file.is.binary");
      status.errorAnalyzingFinished = true;
      return status;
    }
    status.reasonWhyDisabled = DaemonBundle.message("process.title.highlighting.is.disabled.for.this.file");
    status.errorAnalyzingFinished = true;
    return status;
  }

  FileViewProvider provider = psiFile.getViewProvider();
  Set<Language> languages = provider.getLanguages();
  HighlightingSettingsPerFile levelSettings = HighlightingSettingsPerFile.getInstance(myProject);
  boolean shouldHighlight = languages.isEmpty();
  for (Language language : languages) {
    PsiFile root = provider.getPsi(language);
    FileHighlightingSetting level = levelSettings.getHighlightingSettingForRoot(root);
    shouldHighlight |= level != FileHighlightingSetting.SKIP_HIGHLIGHTING;
  }
  if (!shouldHighlight) {
    status.reasonWhyDisabled = DaemonBundle.message("process.title.highlighting.level.is.none");
    status.errorAnalyzingFinished = true;
    return status;
  }

  if (HeavyProcessLatch.INSTANCE.isRunning()) {
    Map.Entry<String, HeavyProcessLatch.Type> processEntry = HeavyProcessLatch.INSTANCE.getRunningOperation();
    if (processEntry != null) {
      status.reasonWhySuspended = processEntry.getKey();
      status.heavyProcessType = processEntry.getValue();
    }
    else {
      status.reasonWhySuspended = DaemonBundle.message("process.title.heavy.operation.is.running");
      status.heavyProcessType = HeavyProcessLatch.Type.Processing;
    }
    status.errorAnalyzingFinished = true;
    return status;
  }

  status.errorCount = errorCount.clone();

  List<ProgressableTextEditorHighlightingPass> passesToShowProgressFor = myDaemonCodeAnalyzer.getPassesToShowProgressFor(myDocument);
  status.passes = ContainerUtil.filter(passesToShowProgressFor, p -> !StringUtil.isEmpty(p.getPresentableName()) && p.getProgress() >= 0);

  status.errorAnalyzingFinished = myDaemonCodeAnalyzer.isAllAnalysisFinished(psiFile);
  status.reasonWhySuspended = myDaemonCodeAnalyzer.isUpdateByTimerEnabled() ? null : DaemonBundle.message("process.title.highlighting.is.paused.temporarily");
  fillDaemonCodeAnalyzerErrorsStatus(status, severityRegistrar);

  return status;
}