Java Code Examples for com.intellij.openapi.fileEditor.impl.LoadTextUtil#AutoDetectionReason

The following examples show how to use com.intellij.openapi.fileEditor.impl.LoadTextUtil#AutoDetectionReason . 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: EncodingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
static FailReason checkCanReload(@Nonnull VirtualFile virtualFile, @Nullable Ref<? super Charset> current) {
  if (virtualFile.isDirectory()) {
    return FailReason.IS_DIRECTORY;
  }
  FileDocumentManager documentManager = FileDocumentManager.getInstance();
  Document document = documentManager.getDocument(virtualFile);
  if (document == null) return FailReason.IS_BINARY;
  Charset charsetFromContent = ((EncodingManagerImpl)EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
  Charset existing = virtualFile.getCharset();
  LoadTextUtil.AutoDetectionReason autoDetectedFrom = LoadTextUtil.getCharsetAutoDetectionReason(virtualFile);
  FailReason result;
  if (autoDetectedFrom != null) {
    // no point changing encoding if it was auto-detected
    result = autoDetectedFrom == LoadTextUtil.AutoDetectionReason.FROM_BOM ? FailReason.BY_BOM : FailReason.BY_BYTES;
  }
  else if (charsetFromContent != null) {
    result = FailReason.BY_FILE;
    existing = charsetFromContent;
  }
  else {
    result = fileTypeDescriptionError(virtualFile);
  }
  if (current != null) current.set(existing);
  return result;
}
 
Example 2
Source File: EncodingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static Magic8 isSafeToReloadIn(@Nonnull VirtualFile virtualFile, @Nonnull CharSequence text, @Nonnull byte[] bytes, @Nonnull Charset charset) {
  // file has BOM but the charset hasn't
  byte[] bom = virtualFile.getBOM();
  if (bom != null && !CharsetToolkit.canHaveBom(charset, bom)) return Magic8.NO_WAY;

  // the charset has mandatory BOM (e.g. UTF-xx) but the file hasn't or has wrong
  byte[] mandatoryBom = CharsetToolkit.getMandatoryBom(charset);
  if (mandatoryBom != null && !ArrayUtil.startsWith(bytes, mandatoryBom)) return Magic8.NO_WAY;

  String loaded = LoadTextUtil.getTextByBinaryPresentation(bytes, charset).toString();

  String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
  String toSave = StringUtil.convertLineSeparators(loaded, separator);

  LoadTextUtil.AutoDetectionReason failReason = LoadTextUtil.getCharsetAutoDetectionReason(virtualFile);
  if (failReason != null && StandardCharsets.UTF_8.equals(virtualFile.getCharset()) && !StandardCharsets.UTF_8.equals(charset)) {
    return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset
  }

  byte[] bytesToSave;
  try {
    bytesToSave = toSave.getBytes(charset);
  }
  // turned out some crazy charsets have incorrectly implemented .newEncoder() returning null
  catch (UnsupportedOperationException | NullPointerException e) {
    return Magic8.NO_WAY;
  }
  if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
    bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave); // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
  }

  return !Arrays.equals(bytesToSave, bytes) ? Magic8.NO_WAY : StringUtil.equals(loaded, text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}