Java Code Examples for com.intellij.util.LineSeparator#getSystemLineSeparator()

The following examples show how to use com.intellij.util.LineSeparator#getSystemLineSeparator() . 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: IoFileBasedStorage.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void doSave(@Nullable Element element) throws IOException {
  if (myLineSeparator == null) {
    myLineSeparator = isUseLfLineSeparatorByDefault() ? LineSeparator.LF : LineSeparator.getSystemLineSeparator();
  }

  byte[] content = element == null ? null : StorageUtil.writeToBytes(element, myLineSeparator.getSeparatorString());
  try {
    if (myStreamProvider != null && myStreamProvider.isEnabled()) {
      // stream provider always use LF separator
      saveForProvider(myLineSeparator == LineSeparator.LF ? content : null, element);
    }
  }
  catch (Throwable e) {
    LOG.error(e);
  }

  if (content == null) {
    StorageUtil.deleteFile(myFile);
  }
  else {
    FileUtil.createParentDirs(myFile);

    StorageUtil.writeFile(myFile, content, isUseXmlProlog() ? myLineSeparator : null);
  }
}
 
Example 2
Source File: ExternalDiffToolUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static File createTempFile(@Nonnull final DocumentContent content, @Nonnull FileNameInfo fileName) throws IOException {
  FileDocumentManager.getInstance().saveDocument(content.getDocument());

  LineSeparator separator = content.getLineSeparator();
  if (separator == null) separator = LineSeparator.getSystemLineSeparator();

  Charset charset = content.getCharset();
  if (charset == null) charset = Charset.defaultCharset();

  Boolean hasBom = content.hasBom();
  if (hasBom == null) hasBom = CharsetToolkit.getMandatoryBom(charset) != null;

  ThrowableComputable<String,RuntimeException> action = () -> {
    return content.getDocument().getText();
  };
  String contentData = AccessRule.read(action);
  if (separator != LineSeparator.LF) {
    contentData = StringUtil.convertLineSeparators(contentData, separator.getSeparatorString());
  }

  byte[] bytes = contentData.getBytes(charset);

  byte[] bom = hasBom ? CharsetToolkit.getPossibleBom(charset) : null;
  if (bom != null) {
    bytes = ArrayUtil.mergeArrays(bom, bytes);
  }

  return createFile(bytes, fileName);
}
 
Example 3
Source File: StorageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static LineSeparator detectLineSeparators(@Nonnull CharSequence chars, @Nullable LineSeparator defaultSeparator) {
  for (int i = 0, n = chars.length(); i < n; i++) {
    char c = chars.charAt(i);
    if (c == '\r') {
      return LineSeparator.CRLF;
    }
    else if (c == '\n') {
      // if we are here, there was no \r before
      return LineSeparator.LF;
    }
  }
  return defaultSeparator == null ? LineSeparator.getSystemLineSeparator() : defaultSeparator;
}
 
Example 4
Source File: VfsFileBasedStorage.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSave(@Nullable Element element) throws IOException {
  if (myLineSeparator == null) {
    myLineSeparator = isUseLfLineSeparatorByDefault() ? LineSeparator.LF : LineSeparator.getSystemLineSeparator();
  }

  byte[] content = element == null ? null : StorageUtil.writeToBytes(element, myLineSeparator.getSeparatorString());
  try {
    if (myStreamProvider != null && myStreamProvider.isEnabled()) {
      // stream provider always use LF separator
      saveForProvider(myLineSeparator == LineSeparator.LF ? content : null, element);
    }
  }
  catch (Throwable e) {
    LOG.error(e);
  }

  if (content == null) {
    StorageUtil.deleteFile(myFile, this, getVirtualFile());
    myCachedVirtualFile = null;
  }
  else {
    VirtualFile file = getVirtualFile();
    if (file == null || !file.exists()) {
      FileUtil.createParentDirs(myFile);
      file = null;
    }
    myCachedVirtualFile = StorageUtil.writeFile(myFile, this, file, content, isUseXmlProlog() ? myLineSeparator : null);
  }
}