com.intellij.openapi.fileEditor.impl.LoadTextUtil Java Examples

The following examples show how to use com.intellij.openapi.fileEditor.impl.LoadTextUtil. 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: HaxeCompilerServices.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private int recalculateFileOffset(@NotNull PsiFile file, @NotNull PsiElement position, Editor editor) {
    int offset = position.getTextOffset();;

    VirtualFile virtualFile = file.getVirtualFile();
    if (null == virtualFile) {  // In memory file, the position doesn't change.
        return offset;
    }

    // Get the separator, checking the file if we don't know yet.  May still return null.
    String separator = LoadTextUtil.detectLineSeparator(virtualFile, true);

    // IntelliJ IDEA normalizes file line endings, so if file line endings is
    // CRLF - then we have to shift an offset so Haxe compiler could get proper offset
    if (LineSeparator.CRLF.getSeparatorString().equals(separator)) {
        int lineNumber =
            com.intellij.openapi.util.text.StringUtil.offsetToLineNumber(editor.getDocument().getText(), offset);
        offset += lineNumber;
    }
    return offset;
}
 
Example #2
Source File: BaseCppTestCase.java    From CppTools with Apache License 2.0 6 votes vote down vote up
protected void doParseTest(final String fileName, @NotNull @NonNls final String ext) throws Throwable {
  Runnable action = new Runnable() {
    public void run() {
      try {
        myFixture.testHighlighting(fileName + (ext.length() > 0 ? "." + ext : ""));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      String s = DebugUtil.psiToString(myFixture.getFile(), true);
      final String expected = LoadTextUtil.loadText(
        LocalFileSystem.getInstance().findFileByIoFile(new File(getTestDataPath() + File.separator + fileName + ".txt"))
      ).toString();

      assertEquals(
        expected,
        s
      );
    }
  };
  BuildState.invokeOnEDTSynchroneously(action);
}
 
Example #3
Source File: LossyEncodingInspection.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) {
  if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) return null;
  if (!file.isPhysical()) return null;
  FileViewProvider viewProvider = file.getViewProvider();
  if (viewProvider.getBaseLanguage() != file.getLanguage()) return null;
  VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile == null) return null;
  if (!virtualFile.isInLocalFileSystem()) return null;
  CharSequence text = viewProvider.getContents();
  Charset charset = LoadTextUtil.extractCharsetFromFileContent(file.getProject(), virtualFile, text);

  // no sense in checking transparently decoded file: all characters there are already safely encoded
  if (charset instanceof Native2AsciiCharset) return null;

  List<ProblemDescriptor> descriptors = new SmartList<ProblemDescriptor>();
  boolean ok = checkFileLoadedInWrongEncoding(file, manager, isOnTheFly, virtualFile, charset, descriptors);
  if (ok) {
    checkIfCharactersWillBeLostAfterSave(file, manager, isOnTheFly, text, charset, descriptors);
  }

  return descriptors.toArray(new ProblemDescriptor[descriptors.size()]);
}
 
Example #4
Source File: FileContentImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public CharSequence getContentAsText() {
  if (myFileType.isBinary()) {
    throw new IllegalDataException("Cannot obtain text for binary file type : " + myFileType.getDescription());
  }
  final CharSequence content = getUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY);
  if (content != null) {
    return content;
  }
  CharSequence contentAsText = myContentAsText;
  if (contentAsText == null) {
    myContentAsText = contentAsText = LoadTextUtil.getTextByBinaryPresentation(myContent, myFile);
    myContent = null; // help gc, indices are expected to use bytes or chars but not both
  }
  return contentAsText;
}
 
Example #5
Source File: AbstractFileViewProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public CharSequence getText() {
  final VirtualFile virtualFile = getVirtualFile();
  if (virtualFile instanceof LightVirtualFile) {
    Document doc = getCachedDocument();
    if (doc != null) return getLastCommittedText(doc);
    return ((LightVirtualFile)virtualFile).getContent();
  }

  final Document document = getDocument();
  if (document == null) {
    return LoadTextUtil.loadText(virtualFile);
  }
  return getLastCommittedText(document);
}
 
Example #6
Source File: AbstractConvertLineSeparatorsAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
@Override
public void update(@Nonnull AnActionEvent e) {
  final Project project = e.getData(CommonDataKeys.PROJECT);
  if (project != null) {
    final VirtualFile[] virtualFiles = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
    final Presentation presentation = e.getPresentation();
    if (virtualFiles != null) {
      if (virtualFiles.length == 1) {
        presentation.setEnabled(!mySeparator.equals(LoadTextUtil.detectLineSeparator(virtualFiles[0], false)));
      }
      else {
        presentation.setEnabled(true);
      }
    }
    else {
      presentation.setEnabled(false);
    }
  }
}
 
Example #7
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 #8
Source File: EncodingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
static void saveIn(@Nonnull final Document document, final Editor editor, @Nonnull final VirtualFile virtualFile, @Nonnull final Charset charset) {
  FileDocumentManager documentManager = FileDocumentManager.getInstance();
  documentManager.saveDocument(document);
  final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
  boolean writable = project == null ? virtualFile.isWritable() : ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile);
  if (!writable) {
    CommonRefactoringUtil.showErrorHint(project, editor, "Cannot save the file " + virtualFile.getPresentableUrl(), "Unable to Save", null);
    return;
  }

  EncodingProjectManagerImpl.suppressReloadDuring(() -> {
    EncodingManager.getInstance().setEncoding(virtualFile, charset);
    try {
      ApplicationManager.getApplication().runWriteAction((ThrowableComputable<Object, IOException>)() -> {
        virtualFile.setCharset(charset);
        LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp());
        return null;
      });
    }
    catch (IOException io) {
      Messages.showErrorDialog(project, io.getMessage(), "Error Writing File");
    }
  });
}
 
Example #9
Source File: EncodingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
static Magic8 isSafeToConvertTo(@Nonnull VirtualFile virtualFile, @Nonnull CharSequence text, @Nonnull byte[] bytesOnDisk, @Nonnull Charset charset) {
  try {
    String lineSeparator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
    CharSequence textToSave = lineSeparator.equals("\n") ? text : StringUtilRt.convertLineSeparators(text, lineSeparator);

    Pair<Charset, byte[]> chosen = LoadTextUtil.chooseMostlyHarmlessCharset(virtualFile.getCharset(), charset, textToSave.toString());

    byte[] saved = chosen.second;

    CharSequence textLoadedBack = LoadTextUtil.getTextByBinaryPresentation(saved, charset);

    return !StringUtil.equals(text, textLoadedBack) ? Magic8.NO_WAY : Arrays.equals(saved, bytesOnDisk) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
  }
  catch (UnsupportedOperationException e) { // unsupported encoding
    return Magic8.NO_WAY;
  }
}
 
Example #10
Source File: EncodingManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @param virtualFile
 * @return returns null if charset set cannot be determined from content
 */
@Nullable
Charset computeCharsetFromContent(@Nonnull final VirtualFile virtualFile) {
  final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
  if (document == null) {
    return null;
  }
  Charset cached = EncodingManager.getInstance().getCachedCharsetFromContent(document);
  if (cached != null) {
    return cached;
  }

  final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
  return ReadAction.compute(() -> {
    Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
    if (charsetFromContent != null) {
      setCachedCharsetFromContent(charsetFromContent, null, document);
    }
    return charsetFromContent;
  });
}
 
Example #11
Source File: VirtualFileSystemEntry.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private Charset computeCharset() {
  Charset charset;
  if (isDirectory()) {
    Charset configured = EncodingManager.getInstance().getEncoding(this, true);
    charset = configured == null ? Charset.defaultCharset() : configured;
    setCharset(charset);
  }
  else {
    FileType fileType = getFileType();
    if (isCharsetSet()) {
      // file type detection may have cached the charset, no need to re-detect
      return super.getCharset();
    }
    try {
      final byte[] content = VfsUtilCore.loadBytes(this);
      charset = LoadTextUtil.detectCharsetAndSetBOM(this, content, fileType);
    }
    catch (IOException e) {
      return super.getCharset();
    }
  }
  return charset;
}
 
Example #12
Source File: ParsingTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void doTest(boolean checkResult) {
  String name = getTestName(false);
  try {
    String text = loadFile(name + "." + myExtension);
    myFile = createPsiFile(name, text);
    ensureParsed(myFile);
    FileViewProvider viewProvider = myFile.getViewProvider();
    VirtualFile virtualFile = viewProvider.getVirtualFile();
    if (virtualFile instanceof LightVirtualFile) {
      assertEquals("light virtual file text mismatch", text, ((LightVirtualFile)virtualFile).getContent());
    }
    assertEquals("virtual file text mismatch", text, LoadTextUtil.loadText(virtualFile));
    assertEquals("doc text mismatch", text, viewProvider.getDocument().getText());
    assertEquals("psi text mismatch", text, myFile.getText());
    if (checkResult) {
      checkResult(name, myFile);
    }
    else {
      toParseTreeText(myFile, skipSpaces(), includeRanges());
    }
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #13
Source File: EncodingManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void handleDocument(@Nonnull final Document document) {
  VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
  if (virtualFile == null) return;
  Project project = guessProject(virtualFile);
  while (true) {
    if (project != null && project.isDisposed()) break;
    int nRequests = addNumberOfRequestedRedetects(document, 0);
    Charset charset = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
    Charset oldCached = getCachedCharsetFromContent(document);
    if (!Comparing.equal(charset, oldCached)) {
      setCachedCharsetFromContent(charset, oldCached, document);
    }
    if (addNumberOfRequestedRedetects(document, -nRequests) == 0) break;
  }
}
 
Example #14
Source File: ApplyTextFilePatch.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
protected Result applyChange(final Project project, final VirtualFile fileToPatch, final FilePath pathBeforeRename, final Getter<CharSequence> baseContents) throws IOException {
  byte[] fileContents = fileToPatch.contentsToByteArray();
  CharSequence text = LoadTextUtil.getTextByBinaryPresentation(fileContents, fileToPatch);
  final GenericPatchApplier applier = new GenericPatchApplier(text, myPatch.getHunks());
  if (applier.execute()) {
    final Document document = FileDocumentManager.getInstance().getDocument(fileToPatch);
    if (document == null) {
      throw new IOException("Failed to set contents for updated file " + fileToPatch.getPath());
    }
    document.setText(applier.getAfter());
    FileDocumentManager.getInstance().saveDocument(document);
    return new Result(applier.getStatus()) {
      @Override
      public ApplyPatchForBaseRevisionTexts getMergeData() {
        return null;
      }
    };
  }
  applier.trySolveSomehow();
  return new Result(ApplyPatchStatus.FAILURE) {
    @Override
    public ApplyPatchForBaseRevisionTexts getMergeData() {
      return ApplyPatchForBaseRevisionTexts.create(project, fileToPatch, pathBeforeRename, myPatch, baseContents);
    }
  };
}
 
Example #15
Source File: LightFileDocumentManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Document getDocument(@Nonnull VirtualFile file) {
  Document document = file.getUserData(MOCK_DOC_KEY);
  if (document == null) {
    if (file.isDirectory() || isBinaryWithoutDecompiler(file)) return null;

    CharSequence text = LoadTextUtil.loadText(file);
    document = myFactory.apply(text);
    document.putUserData(MOCK_VIRTUAL_FILE_KEY, file);
    document = file.putUserDataIfAbsent(MOCK_DOC_KEY, document);
  }
  return document;
}
 
Example #16
Source File: MockFileDocumentManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Document getDocument(@Nonnull VirtualFile file) {
  Document document = file.getUserData(MOCK_DOC_KEY);
  if (document == null) {
    if (file.isDirectory() || isBinaryWithoutDecompiler(file)) return null;

    CharSequence text = LoadTextUtil.loadText(file);
    document = myFactory.fun(text);
    document.putUserData(MOCK_VIRTUAL_FILE_KEY, file);
    document = file.putUserDataIfAbsent(MOCK_DOC_KEY, document);
  }
  return document;
}
 
Example #17
Source File: InconsistentLineSeparatorsInspection.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public PsiElementVisitor buildVisitor(@Nonnull final ProblemsHolder holder, boolean isOnTheFly) {
  return new PsiElementVisitor() {
    @Override
    public void visitFile(PsiFile file) {
      if (!file.getLanguage().equals(file.getViewProvider().getBaseLanguage())) {
        // There is a possible case that more than a single virtual file/editor contains more than one language (e.g. php and html).
        // We want to process a virtual file once than, hence, ignore all non-base psi files.
        return;
      }

      final Project project = holder.getProject();
      final String projectLineSeparator = CodeStyleFacade.getInstance(project).getLineSeparator();
      if (projectLineSeparator == null) {
        return;
      }

      final VirtualFile virtualFile = file.getVirtualFile();
      if (virtualFile == null || !AbstractConvertLineSeparatorsAction.shouldProcess(virtualFile, project)) {
        return;
      }

      final String curLineSeparator = LoadTextUtil.detectLineSeparator(virtualFile, true);
      if (curLineSeparator != null && !curLineSeparator.equals(projectLineSeparator)) {
        holder.registerProblem(
          file,
          "Line separators in the current file (" + StringUtil.escapeStringCharacters(curLineSeparator) + ") " +
          "differ from the project defaults (" + StringUtil.escapeStringCharacters(projectLineSeparator) + ")",
          SET_PROJECT_LINE_SEPARATORS);
      }
    }
  };
}
 
Example #18
Source File: AbstractConvertLineSeparatorsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void changeLineSeparators(@Nonnull final Project project,
                                        @Nonnull final VirtualFile virtualFile,
                                        @Nonnull final String newSeparator) {
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  Document document = fileDocumentManager.getCachedDocument(virtualFile);
  if (document != null) {
    fileDocumentManager.saveDocument(document);
  }

  String currentSeparator = LoadTextUtil.detectLineSeparator(virtualFile, false);
  final String commandText;
  if (StringUtil.isEmpty(currentSeparator)) {
    commandText = "Changed line separators to " + LineSeparator.fromString(newSeparator);
  }
  else {
    commandText = String.format("Changed line separators from %s to %s", LineSeparator.fromString(currentSeparator),
                                LineSeparator.fromString(newSeparator));
  }

  new WriteCommandAction(project, commandText) {
    @Override
    protected void run(@Nonnull Result result) throws Throwable {
      try {
        LoadTextUtil.changeLineSeparators(project, virtualFile, newSeparator, this);
      }
      catch (IOException e) {
        LOG.info(e);
      }
    }
  }.execute();
}
 
Example #19
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 #20
Source File: LoadTextUtilTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void doTest(String source, String expected, String expectedSeparator) {
  final LightVirtualFile vFile = new LightVirtualFile("test.txt");
  final CharSequence real = LoadTextUtil.getTextByBinaryPresentation(source.getBytes(CharsetToolkit.US_ASCII_CHARSET), vFile);
  assertTrue("content", Comparing.equal(expected, real));
  if (expectedSeparator != null) {
    assertEquals("detected line separator", expectedSeparator, FileDocumentManager.getInstance().getLineSeparator(vFile, null));
  }
}
 
Example #21
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;
}
 
Example #22
Source File: LineSeparatorPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
protected WidgetState getWidgetState(@Nullable VirtualFile file) {
  if (file == null) {
    return WidgetState.HIDDEN;
  }
  String lineSeparator = LoadTextUtil.detectLineSeparator(file, true);
  if (lineSeparator == null) {
    return WidgetState.HIDDEN;
  }
  String toolTipText = String.format("Line Separator: %s", StringUtil.escapeLineBreak(lineSeparator));
  String panelText = LineSeparator.fromString(lineSeparator).toString();
  return new WidgetState(toolTipText, panelText, true);
}
 
Example #23
Source File: LightFileDocumentManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public String getLineSeparator(VirtualFile file, Project project) {
  return LoadTextUtil.getDetectedLineSeparator(file);
}
 
Example #24
Source File: AttachmentFactory.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static Attachment createAttachment(@Nonnull VirtualFile file) {
  return consulo.logging.attachment.AttachmentFactory.get().create(file.getPresentableUrl(), getBytes(file),
                        file.getFileType().isBinary() ? "File is binary" : LoadTextUtil.loadText(file).toString());
}
 
Example #25
Source File: SrcFileAnnotator.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static
@Nonnull
String[] getCoveredLines(@Nonnull byte[] oldContent, VirtualFile vFile) {
  final String text = LoadTextUtil.getTextByBinaryPresentation(oldContent, vFile, false, false).toString();
  return LineTokenizer.tokenize(text, false);
}
 
Example #26
Source File: FileTypeManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private FileType detect(@Nonnull VirtualFile file, @Nonnull byte[] bytes, int length, @Nonnull Iterable<? extends FileTypeDetector> detectors) {
  if (length <= 0) return UnknownFileType.INSTANCE;

  // use PlainTextFileType because it doesn't supply its own charset detector
  // help set charset in the process to avoid double charset detection from content
  return LoadTextUtil.processTextFromBinaryPresentationOrNull(bytes, length, file, true, true, PlainTextFileType.INSTANCE, (@Nullable CharSequence text) -> {
    if (toLog()) {
      log("F: detectFromContentAndCache.processFirstBytes(" +
          file.getName() +
          "): bytes length=" +
          length +
          "; isText=" +
          (text != null) +
          "; text='" +
          (text == null ? null : StringUtil.first(text, 100, true)) +
          "'" +
          ", detectors=" +
          detectors);
    }
    FileType detected = null;
    ByteSequence firstBytes = new ByteArraySequence(bytes, 0, length);
    for (FileTypeDetector detector : detectors) {
      try {
        detected = detector.detect(file, firstBytes, text);
      }
      catch (Exception e) {
        LOG.error("Detector " + detector + " (" + detector.getClass() + ") exception occurred:", e);
      }
      if (detected != null) {
        if (toLog()) {
          log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): detector " + detector + " type as " + detected.getName());
        }
        break;
      }
    }

    if (detected == null) {
      detected = text == null ? UnknownFileType.INSTANCE : PlainTextFileType.INSTANCE;
      if (toLog()) {
        log("F: detectFromContentAndCache.processFirstBytes(" + file.getName() + "): " + "no detector was able to detect. assigned " + detected.getName());
      }
    }
    return detected;
  });
}
 
Example #27
Source File: PatchVirtualFileReader.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PatchReader create(final VirtualFile virtualFile) throws IOException {
  final byte[] patchContents = virtualFile.contentsToByteArray();
  final CharSequence patchText = LoadTextUtil.getTextByBinaryPresentation(patchContents, virtualFile);
  return new PatchReader(patchText);
}
 
Example #28
Source File: FileDocumentContentImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@javax.annotation.Nullable
private static LineSeparator getSeparator(@Nonnull VirtualFile file) {
  String s = LoadTextUtil.detectLineSeparator(file, true);
  if (s == null) return null;
  return LineSeparator.fromString(s);
}
 
Example #29
Source File: SwaggerFileService.java    From intellij-swagger with MIT License 4 votes vote down vote up
private String convertToJsonIfNecessary(@NotNull final VirtualFile virtualFile) throws Exception {
  final JsonNode root = mapper.readTree(LoadTextUtil.loadText(virtualFile).toString());
  final JsonNode result = jsonBuilderService.buildFromSpec(root, virtualFile);

  return new ObjectMapper().writeValueAsString(result);
}