Java Code Examples for com.intellij.openapi.util.Pair#createNonNull()

The following examples show how to use com.intellij.openapi.util.Pair#createNonNull() . 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: LoadTextUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static Pair.NonNull<Charset, byte[]> chooseMostlyHarmlessCharset(@Nonnull Charset existing, @Nonnull Charset specified, @Nonnull String text) {
  try {
    if (specified.equals(existing)) {
      return Pair.createNonNull(specified, text.getBytes(existing));
    }

    byte[] out = isSupported(specified, text);
    if (out != null) {
      return Pair.createNonNull(specified, out); //if explicitly specified encoding is safe, return it
    }
    out = isSupported(existing, text);
    if (out != null) {
      return Pair.createNonNull(existing, out);   //otherwise stick to the old encoding if it's ok
    }
    return Pair.createNonNull(specified, text.getBytes(specified)); //if both are bad there is no difference
  }
  catch (RuntimeException e) {
    Charset defaultCharset = Charset.defaultCharset();
    return Pair.createNonNull(defaultCharset, text.getBytes(defaultCharset)); //if both are bad and there is no hope, use the default charset
  }
}
 
Example 2
Source File: PersistentRangeMarker.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static Pair.NonNull<TextRange, LinesCols> applyChange(@Nonnull DocumentEvent event, @Nonnull Segment range,
                                                              int intervalStart, int intervalEnd,
                                                              boolean greedyLeft, boolean greedyRight, boolean stickingToRight,
                                                              @Nonnull LinesCols linesCols) {
  boolean shouldTranslateViaDiff = PersistentRangeMarkerUtil.shouldTranslateViaDiff(event, range.getStartOffset(), range.getEndOffset());
  Pair.NonNull<TextRange, LinesCols> translated = null;
  if (shouldTranslateViaDiff) {
    translated = translateViaDiff((DocumentEventImpl)event, linesCols);
  }
  if (translated == null) {
    TextRange fallback = applyChange(event, intervalStart, intervalEnd, greedyLeft, greedyRight, stickingToRight);
    if (fallback == null) return null;

    LinesCols lc = storeLinesAndCols(event.getDocument(), fallback.getStartOffset(), fallback.getEndOffset());
    if (lc == null) return null;

    translated = Pair.createNonNull(fallback, lc);
  }
  return translated;
}
 
Example 3
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Pair.NonNull<Charset, byte[]> getOverriddenCharsetByBOM(@Nonnull byte[] content, @Nonnull Charset charset) {
  if (charset.name().contains(CharsetToolkit.UTF8) && CharsetToolkit.hasUTF8Bom(content)) {
    return Pair.createNonNull(charset, CharsetToolkit.UTF8_BOM);
  }
  Charset charsetFromBOM = CharsetToolkit.guessFromBOM(content);
  if (charsetFromBOM != null) {
    byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charsetFromBOM), ArrayUtilRt.EMPTY_BYTE_ARRAY);
    return Pair.createNonNull(charsetFromBOM, bom);
  }

  return Pair.createNonNull(charset, ArrayUtilRt.EMPTY_BYTE_ARRAY);
}
 
Example 4
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Pair.NonNull<Charset, byte[]> charsetForWriting(@Nullable Project project, @Nonnull VirtualFile virtualFile, @Nonnull String text, @Nonnull Charset existing) {
  Charset specified = extractCharsetFromFileContent(project, virtualFile, text);
  Pair.NonNull<Charset, byte[]> chosen = chooseMostlyHarmlessCharset(existing, specified, text);
  Charset charset = chosen.first;

  // in case of "UTF-16", OutputStreamWriter sometimes adds BOM on it's own.
  // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6800103
  byte[] bom = virtualFile.getBOM();
  Charset fromBom = bom == null ? null : CharsetToolkit.guessFromBOM(bom);
  if (fromBom != null && !fromBom.equals(charset)) {
    chosen = Pair.createNonNull(fromBom, text.getBytes(fromBom));
  }
  return chosen;
}
 
Example 5
Source File: PersistentRangeMarker.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
static Pair.NonNull<TextRange, LinesCols> translateViaDiff(@Nonnull final DocumentEventImpl event, @Nonnull LinesCols linesCols) {
  try {
    int myStartLine = event.translateLineViaDiffStrict(linesCols.myStartLine);
    Document document = event.getDocument();
    if (myStartLine < 0 || myStartLine >= document.getLineCount()) {
      return null;
    }

    int start = document.getLineStartOffset(myStartLine) + linesCols.myStartColumn;
    if (start >= document.getTextLength()) return null;

    int myEndLine = event.translateLineViaDiffStrict(linesCols.myEndLine);
    if (myEndLine < 0 || myEndLine >= document.getLineCount()) {
      return null;
    }

    int end = document.getLineStartOffset(myEndLine) + linesCols.myEndColumn;
    if (end > document.getTextLength() || end < start) return null;

    if (end > event.getDocument().getTextLength() ||
        myEndLine < myStartLine ||
        myStartLine == myEndLine && linesCols.myEndColumn < linesCols.myStartColumn ||
        event.getDocument().getLineCount() < myEndLine) {
      return null;
    }

    return Pair.createNonNull(new TextRange(start, end), new LinesCols(myStartLine, linesCols.myStartColumn, myEndLine, linesCols.myEndColumn));
  }
  catch (FilesTooBigForDiffException e) {
    return null;
  }
}
 
Example 6
Source File: FakeWorkspaceFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Creates a {@code Workspace} for testing and a {@code MockVirtualFileSystem} with the expected Flutter script files.
 */
@NotNull
public static Pair.NonNull<MockVirtualFileSystem, Workspace> createWorkspaceAndFilesystem(
  @Nullable String daemonScript,
  @Nullable String doctorScript,
  @Nullable String launchScript,
  @Nullable String testScript,
  @Nullable String sdkHome,
  @Nullable String versionFile,
  @Nullable String devtoolsScript
) {
  MockVirtualFileSystem fs = new MockVirtualFileSystem();
  fs.file("/workspace/WORKSPACE", "");
  if (daemonScript != null) {
    fs.file("/workspace/" + daemonScript, "");
  }
  if (doctorScript != null) {
    fs.file("/workspace/" + doctorScript, "");
  }
  if (launchScript != null) {
    fs.file("/workspace/" + launchScript, "");
  }
  if (testScript != null) {
    fs.file("/workspace/" + testScript, "");
  }
  if (sdkHome != null) {
    fs.file("/workspace/" + sdkHome, "");
  }
  if (versionFile != null) {
    fs.file("/workspace/" + versionFile, "");
  }
  return Pair.createNonNull(
    fs,
    Workspace.forTest(
      fs.findFileByPath("/workspace/"),
      PluginConfig.forTest(
        daemonScript,
        doctorScript,
        launchScript,
        testScript,
        sdkHome,
        versionFile,
        devtoolsScript
      )
    )
  );
}
 
Example 7
Source File: FakeWorkspaceFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Creates a {@code Workspace} for testing and a {@code MockVirtualFileSystem} with the expected Flutter script files.
 */
@NotNull
public static Pair.NonNull<MockVirtualFileSystem, Workspace> createWorkspaceAndFilesystem(
  @Nullable String daemonScript,
  @Nullable String doctorScript,
  @Nullable String launchScript,
  @Nullable String testScript,
  @Nullable String sdkHome,
  @Nullable String versionFile,
  @Nullable String devtoolsScript
) {
  MockVirtualFileSystem fs = new MockVirtualFileSystem();
  fs.file("/workspace/WORKSPACE", "");
  if (daemonScript != null) {
    fs.file("/workspace/" + daemonScript, "");
  }
  if (doctorScript != null) {
    fs.file("/workspace/" + doctorScript, "");
  }
  if (launchScript != null) {
    fs.file("/workspace/" + launchScript, "");
  }
  if (testScript != null) {
    fs.file("/workspace/" + testScript, "");
  }
  if (sdkHome != null) {
    fs.file("/workspace/" + sdkHome, "");
  }
  if (versionFile != null) {
    fs.file("/workspace/" + versionFile, "");
  }
  return Pair.createNonNull(
    fs,
    Workspace.forTest(
      fs.findFileByPath("/workspace/"),
      PluginConfig.forTest(
        daemonScript,
        doctorScript,
        launchScript,
        testScript,
        sdkHome,
        versionFile,
        devtoolsScript
      )
    )
  );
}