Java Code Examples for com.intellij.openapi.util.Pair#NonNull

The following examples show how to use com.intellij.openapi.util.Pair#NonNull . 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: 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 2
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 3
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
FakeBazelFields(@NotNull BazelFields template) {
  super(template);
  Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory
    .createWorkspaceAndFilesystem();
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 4
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 5
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static CharSequence getTextByBinaryPresentation(@Nonnull byte[] bytes, @Nonnull Charset charset) {
  Pair.NonNull<Charset, byte[]> pair = getOverriddenCharsetByBOM(bytes, charset);
  byte[] bom = pair.getSecond();

  final ConvertResult result = convertBytes(bytes, Math.min(bom.length, bytes.length), bytes.length, pair.first);
  return result.text;
}
 
Example 6
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 7
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Overwrites file with text and sets modification stamp and time stamp to the specified values.
 * <p/>
 * Normally you should not use this method.
 *
 * @param requestor            any object to control who called this method. Note that
 *                             it is considered to be an external change if {@code requestor} is {@code null}.
 *                             See {@link VirtualFileEvent#getRequestor}
 * @param newModificationStamp new modification stamp or -1 if no special value should be set @return {@code Writer}
 * @throws IOException if an I/O error occurs
 * @see VirtualFile#getModificationStamp()
 */
public static void write(@Nullable Project project, @Nonnull VirtualFile virtualFile, @Nonnull Object requestor, @Nonnull String text, long newModificationStamp) throws IOException {
  Charset existing = virtualFile.getCharset();
  Pair.NonNull<Charset, byte[]> chosen = charsetForWriting(project, virtualFile, text, existing);
  Charset charset = chosen.first;
  byte[] buffer = chosen.second;
  if (!charset.equals(existing)) {
    virtualFile.setCharset(charset);
  }
  setDetectedFromBytesFlagBack(virtualFile, buffer);

  virtualFile.setBinaryContent(buffer, newModificationStamp, -1, requestor);
}
 
Example 8
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 9
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
FakeBazelTestFields(@NotNull BazelTestFields template,
                    @Nullable String daemonScript,
                    @Nullable String doctorScript,
                    @Nullable String launchScript,
                    @Nullable String testScript,
                    @Nullable String sdkHome,
                    @Nullable String versionFile) {
  super(template);
  final Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory
    .createWorkspaceAndFilesystem(daemonScript, doctorScript, launchScript, testScript, sdkHome, versionFile, null);
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 10
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
FakeBazelFields(@NotNull BazelFields template,
                @Nullable String daemonScript,
                @Nullable String doctorScript,
                @Nullable String launchScript,
                @Nullable String testScript,
                @Nullable String sdkHome,
                @Nullable String versionFile) {
  super(template);
  Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory
    .createWorkspaceAndFilesystem(daemonScript, doctorScript, launchScript, testScript, sdkHome, versionFile, null);
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 11
Source File: FakeWorkspaceFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a {@code Workspace} for testing and a {@code MockVirtualFileSystem} with the expected Flutter script files.
 * <p>
 * Uses default values for all fields.
 */
@NotNull
public static Pair.NonNull<MockVirtualFileSystem, Workspace> createWorkspaceAndFilesystem() {
  return createWorkspaceAndFilesystem(
    "scripts/flutter-daemon.sh",
    "scripts/flutter-doctor.sh",
    "scripts/bazel-run.sh",
    "scripts/flutter-test.sh",
    "scripts/",
    "flutter-version",
    "scripts/devtools:server"
  );
}
 
Example 12
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
FakeBazelTestFields(@NotNull BazelTestFields template,
                    @Nullable String daemonScript,
                    @Nullable String doctorScript,
                    @Nullable String launchScript,
                    @Nullable String testScript,
                    @Nullable String sdkHome,
                    @Nullable String versionFile) {
  super(template);
  final Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory
    .createWorkspaceAndFilesystem(daemonScript, doctorScript, launchScript, testScript, sdkHome, versionFile, null);
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 13
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
FakeBazelFields(@NotNull BazelFields template) {
  super(template);
  Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory
    .createWorkspaceAndFilesystem();
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 14
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
FakeBazelFields(@NotNull BazelFields template,
                @Nullable String daemonScript,
                @Nullable String doctorScript,
                @Nullable String launchScript,
                @Nullable String testScript,
                @Nullable String sdkHome,
                @Nullable String versionFile) {
  super(template);
  Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory
    .createWorkspaceAndFilesystem(daemonScript, doctorScript, launchScript, testScript, sdkHome, versionFile, null);
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 15
Source File: FakeWorkspaceFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a {@code Workspace} for testing and a {@code MockVirtualFileSystem} with the expected Flutter script files.
 * <p>
 * Uses default values for all fields.
 */
@NotNull
public static Pair.NonNull<MockVirtualFileSystem, Workspace> createWorkspaceAndFilesystem() {
  return createWorkspaceAndFilesystem(
    "scripts/flutter-daemon.sh",
    "scripts/flutter-doctor.sh",
    "scripts/bazel-run.sh",
    "scripts/flutter-test.sh",
    "scripts/",
    "flutter-version",
    "scripts/devtools:server"
  );
}
 
Example 16
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 17
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
FakeBazelTestFields(@NotNull BazelTestFields template) {
  super(template);
  final Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory.createWorkspaceAndFilesystem();
  fs = pair.first;
  fakeWorkspace = pair.second;
}
 
Example 18
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 19
Source File: LaunchCommandsTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
FakeBazelTestFields(@NotNull BazelTestFields template) {
  super(template);
  final Pair.NonNull<MockVirtualFileSystem, Workspace> pair = FakeWorkspaceFactory.createWorkspaceAndFilesystem();
  fs = pair.first;
  fakeWorkspace = pair.second;
}