Java Code Examples for com.intellij.util.containers.ContainerUtil#getLastItem()

The following examples show how to use com.intellij.util.containers.ContainerUtil#getLastItem() . 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: LineFragmentSplitter.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void addLineChunk(int end1, int end2) {
  if (last1 > end1 || last2 > end2) return;

  WordBlock block = createBlock(last1, last2, end1, end2);
  if (block.offsets.isEmpty()) return;

  WordBlock lastBlock = ContainerUtil.getLastItem(myResult);

  if (lastBlock != null && shouldMergeBlocks(lastBlock, block)) {
    myResult.remove(myResult.size() - 1);
    myResult.add(mergeBlocks(lastBlock, block));
    lastHasEqualWords = hasEqualWords || lastHasEqualWords;
  }
  else {
    myResult.add(block);
    lastHasEqualWords = hasEqualWords;
  }

  hasEqualWords = false;
  last1 = end1;
  last2 = end2;
}
 
Example 2
Source File: LabelPainter.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Pair<List<Pair<String, LabelIcon>>, Integer> calculateCompactPresentation(@Nonnull List<RefGroup> refGroups,
                                                                                         @Nonnull FontMetrics fontMetrics,
                                                                                         int height,
                                                                                         @Nonnull Color background,
                                                                                         int availableWidth) {
  int width = LEFT_PADDING + RIGHT_PADDING;

  List<Pair<String, LabelIcon>> labels = ContainerUtil.newArrayList();
  if (refGroups.isEmpty()) return Pair.create(labels, width);

  for (RefGroup group : refGroups) {
    List<Color> colors = group.getColors();
    LabelIcon labelIcon = new LabelIcon(height, background, colors.toArray(new Color[colors.size()]));
    int newWidth = width + labelIcon.getIconWidth() + (group != ContainerUtil.getLastItem(refGroups) ? COMPACT_MIDDLE_PADDING : 0);

    String text = shortenRefName(group.getName(), fontMetrics, availableWidth - newWidth);
    newWidth += fontMetrics.stringWidth(text);

    labels.add(Pair.create(text, labelIcon));
    width = newWidth;
  }

  return Pair.create(labels, width);
}
 
Example 3
Source File: LineStatusTrackerBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void doUpdateRanges(int beforeChangedLine1,
                            int beforeChangedLine2,
                            int linesShift,
                            int beforeTotalLines) {
  LOG.assertTrue(!myReleased);

  List<Range> rangesBeforeChange = new ArrayList<>();
  List<Range> rangesAfterChange = new ArrayList<>();
  List<Range> changedRanges = new ArrayList<>();

  sortRanges(beforeChangedLine1, beforeChangedLine2, linesShift, rangesBeforeChange, changedRanges, rangesAfterChange);

  Range firstChangedRange = ContainerUtil.getFirstItem(changedRanges);
  Range lastChangedRange = ContainerUtil.getLastItem(changedRanges);

  if (firstChangedRange != null && firstChangedRange.getLine1() < beforeChangedLine1) {
    beforeChangedLine1 = firstChangedRange.getLine1();
  }
  if (lastChangedRange != null && lastChangedRange.getLine2() > beforeChangedLine2) {
    beforeChangedLine2 = lastChangedRange.getLine2();
  }

  doUpdateRanges(beforeChangedLine1, beforeChangedLine2, linesShift, beforeTotalLines,
                 rangesBeforeChange, changedRanges, rangesAfterChange);
}
 
Example 4
Source File: BaseAlert.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Alert<V> asExitButton() {
  if (myButtons.isEmpty()) {
    throw new IllegalArgumentException();
  }
  myExitValue = ContainerUtil.getLastItem(myButtons).myValue;
  return this;
}
 
Example 5
Source File: BaseAlert.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Alert<V> asDefaultButton() {
  if (myButtons.isEmpty()) {
    throw new IllegalArgumentException();
  }
  ContainerUtil.getLastItem(myButtons).myDefault = true;
  return this;
}
 
Example 6
Source File: FileBreadcrumbsCollector.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static FileBreadcrumbsCollector findBreadcrumbsCollector(Project project, VirtualFile file) {
  if (file != null) {
    for (FileBreadcrumbsCollector extension : EP_NAME.getExtensions(project)) {
      if (extension.handlesFile(file)) {
        return extension;
      }
    }
  }
  return ContainerUtil.getLastItem(EP_NAME.getExtensionList(project));
}
 
Example 7
Source File: FileReferenceHelperRegistrar.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated this method is broken, please avoid using it, use getHelpers() instead
 */
@Deprecated
@Nonnull
public static <T extends PsiFileSystemItem> FileReferenceHelper getNotNullHelper(@Nonnull T psiFileSystemItem) {
  FileReferenceHelper helper = getHelper(psiFileSystemItem);
  if (helper != null) {
    return helper;
  }
  List<FileReferenceHelper> helpers = getHelpers();
  return ContainerUtil.getLastItem(helpers);
}
 
Example 8
Source File: DumbServiceImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void suspendIfRequested(ProgressSuspender suspender) {
  synchronized (myRequestedSuspensions) {
    String suspendedReason = ContainerUtil.getLastItem(myRequestedSuspensions);
    if (suspendedReason != null) {
      suspender.suspendProcess(suspendedReason);
    }
  }
}
 
Example 9
Source File: AnnotateStackTraceAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
//@CalledWithReadLock
private static VirtualFile getHyperlinkVirtualFile(@Nonnull List<RangeHighlighter> links) {
  RangeHighlighter key = ContainerUtil.getLastItem(links);
  if (key == null) return null;
  HyperlinkInfo info = EditorHyperlinkSupport.getHyperlinkInfo(key);
  if (!(info instanceof FileHyperlinkInfo)) return null;
  OpenFileDescriptor descriptor = ((FileHyperlinkInfo)info).getDescriptor();
  return descriptor != null ? descriptor.getFile() : null;
}
 
Example 10
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * calculates {@link #myOrder} for the given child bus
 */
@Nonnull
private int[] nextOrder() {
  MessageBusImpl lastChild = ContainerUtil.getLastItem(myChildBuses);

  int lastChildIndex = lastChild == null ? 0 : ArrayUtil.getLastElement(lastChild.myOrder, 0);
  if (lastChildIndex == Integer.MAX_VALUE) {
    LOG.error("Too many child buses");
  }

  return ArrayUtil.append(myOrder, lastChildIndex + 1);
}
 
Example 11
Source File: ToggleToolbarAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {
  ContentManager contentManager = myToolWindow.getContentManager();
  Content selectedContent = contentManager.getSelectedContent();
  JComponent contentComponent = selectedContent != null ? selectedContent.getComponent() : null;
  if (contentComponent == null) return EMPTY_ARRAY;
  List<AnAction> result = ContainerUtil.newSmartList();
  for (final ActionToolbar toolbar : iterateToolbars(contentComponent)) {
    JComponent c = toolbar.getComponent();
    if (c.isVisible() || !c.isValid()) continue;
    if (!result.isEmpty() && !(ContainerUtil.getLastItem(result) instanceof AnSeparator)) {
      result.add(AnSeparator.getInstance());
    }

    List<AnAction> actions = toolbar.getActions();
    for (AnAction action : actions) {
      if (action instanceof ToggleAction && !result.contains(action)) {
        result.add(action);
      }
      else if (action instanceof AnSeparator) {
        if (!result.isEmpty() && !(ContainerUtil.getLastItem(result) instanceof AnSeparator)) {
          result.add(AnSeparator.getInstance());
        }
      }
    }
  }
  boolean popup = result.size() > 3;
  setPopup(popup);
  if (!popup && !result.isEmpty()) result.add(AnSeparator.getInstance());
  return result.toArray(new AnAction[result.size()]);
}
 
Example 12
Source File: StatisticsUtilKt.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a proper UsageDescriptor for a counting value.
 * If one needs to know a number of some items in the project, there is no direct way to report usages per-project.
 * Therefore this workaround: create several keys representing interesting ranges, and report that key which correspond to the range
 * which the given value belongs to.
 * <p>
 * For example, to report a number of commits in Git repository, you can call this method like that:
 * ```
 * val usageDescriptor = getCountingUsage("git.commit.count", listOf(0, 1, 100, 10000, 100000), realCommitCount)
 * ```
 * and if there are e.g. 50000 commits in the repository, one usage of the following key will be reported: `git.commit.count.10K+`.
 * <p>
 * NB:
 * (1) the list of steps must be sorted ascendingly; If it is not, the result is undefined.
 * (2) the value should lay somewhere inside steps ranges. If it is below the first step, the following usage will be reported:
 * `git.commit.count.<1`.
 *
 * @key The key prefix which will be appended with "." and range code.
 * @steps Limits of the ranges. Each value represents the start of the next range. The list must be sorted ascendingly.
 * @value Value to be checked among the given ranges.
 */
public static UsageDescriptor getCountingUsage(String key, int value, List<Integer> steps) {
  if (steps.isEmpty()) return new UsageDescriptor(key + "." + value, 1);

  if (value < steps.get(0)) {
    return new UsageDescriptor(key + ".<" + steps.get(0), 1);
  }

  int index = Arrays.binarySearch(steps.toArray(), value);
  int stepIndex;

  if (index == steps.size()) {
    stepIndex = ContainerUtil.getLastItem(steps);
  }
  else if (index >= 0) {
    stepIndex = index;
  }
  else {
    stepIndex = -index - 2;
  }

  int step = steps.get(stepIndex);

  boolean addPlus = stepIndex == steps.size() - 1 || steps.get(stepIndex + 1) != step + 1;

  String maybePlus = addPlus ? "+" : "";
  return new UsageDescriptor(key + "." + humanize(step) + maybePlus, 1);
}
 
Example 13
Source File: FilePointerPartNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void addRecursiveDirectoryPtrTo(@Nullable List<? super FilePointerPartNode> dirs) {
  if (dirs != null && hasRecursiveDirectoryPointer() && ContainerUtil.getLastItem(dirs) != this) {
    dirs.add(this);
  }
}
 
Example 14
Source File: DesktopBalloonLayoutImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public Component getTopBalloonComponent() {
  BalloonImpl balloon = (BalloonImpl)ContainerUtil.getLastItem(myBalloons);
  return balloon == null ? null : balloon.getComponent();
}
 
Example 15
Source File: FilePointerPartNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
static FilePointerPartNode findOrCreateNodeByPath(@Nonnull FilePointerPartNode rootNode, @Nonnull String path, @Nonnull NewVirtualFileSystem fs) {
  List<String> names = splitNames(path);
  NewVirtualFile fsRoot = null;

  VirtualFile NEVER_TRIED_TO_FIND = NullVirtualFile.INSTANCE;
  // we try to never call file.findChild() because it's expensive
  VirtualFile currentFile = NEVER_TRIED_TO_FIND;
  FilePointerPartNode currentNode = rootNode;
  for (int i = names.size() - 1; i >= 0; i--) {
    String name = names.get(i);
    int index = currentNode.binarySearchChildByName(name);
    if (index >= 0) {
      currentNode = currentNode.children[index];
      currentFile = currentFile == NEVER_TRIED_TO_FIND || currentFile == null ? currentFile : currentFile.findChild(name);
      continue;
    }
    // create and insert new node
    // first, have to check if the file root/names(end)/.../names[i] exists
    // if yes, create nameId-based FilePinterPartNode (for faster search and memory efficiency),
    // if not, create temp UrlPartNode which will be replaced with FPPN when the real file is created
    if (currentFile == NEVER_TRIED_TO_FIND) {
      if (fsRoot == null) {
        String rootPath = ContainerUtil.getLastItem(names);
        fsRoot = ManagingFS.getInstance().findRoot(rootPath, fs instanceof ArchiveFileSystem ? LocalFileSystem.getInstance() : fs);
        if (fsRoot != null && !fsRoot.getName().equals(rootPath)) {
          // ignore really weird root names, like "/" under windows
          fsRoot = null;
        }
      }
      currentFile = fsRoot == null ? null : findFileFromRoot(fsRoot, fs, names, i);
    }
    else {
      currentFile = currentFile == null ? null : currentFile.findChild(name);
    }
    FilePointerPartNode child = currentFile == null ? new UrlPartNode(name, currentNode) : new FilePointerPartNode(getNameId(currentFile), currentNode);

    currentNode.children = ArrayUtil.insert(currentNode.children, -index - 1, child);
    currentNode = child;
    if (i != 0 && fs instanceof ArchiveFileSystem && currentFile != null && !currentFile.isDirectory()) {
      currentFile = ((ArchiveFileSystem)fs).getRootByLocal(currentFile);
    }
  }
  return currentNode;
}
 
Example 16
Source File: InfoAndProgressPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private ProgressIndicatorEx getLatestProgress() {
  return ContainerUtil.getLastItem(myOriginals);
}
 
Example 17
Source File: DiffUserDataKeysEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@javax.annotation.Nullable
public <T> T select(@Nonnull List<T> changes) {
  if (this == FIRST_CHANGE) return ContainerUtil.getFirstItem(changes);
  if (this == LAST_CHANGE) return ContainerUtil.getLastItem(changes);
  throw new IllegalStateException();
}
 
Example 18
Source File: LineStatusTrackerBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void doUpdateRanges(int beforeChangedLine1,
                            int beforeChangedLine2,
                            int linesShift, // before -> after
                            int beforeTotalLines,
                            @Nonnull List<Range> rangesBefore,
                            @Nonnull List<Range> changedRanges,
                            @Nonnull List<Range> rangesAfter) {
  try {
    int vcsTotalLines = getLineCount(myVcsDocument);

    Range lastRangeBefore = ContainerUtil.getLastItem(rangesBefore);
    Range firstRangeAfter = ContainerUtil.getFirstItem(rangesAfter);

    //noinspection UnnecessaryLocalVariable
    int afterChangedLine1 = beforeChangedLine1;
    int afterChangedLine2 = beforeChangedLine2 + linesShift;

    int vcsLine1 = getVcsLine1(lastRangeBefore, beforeChangedLine1);
    int vcsLine2 = getVcsLine2(firstRangeAfter, beforeChangedLine2, beforeTotalLines, vcsTotalLines);

    List<Range> newChangedRanges = getNewChangedRanges(afterChangedLine1, afterChangedLine2, vcsLine1, vcsLine2);

    shiftRanges(rangesAfter, linesShift);

    if (!changedRanges.equals(newChangedRanges)) {
      myRanges = new ArrayList<>(rangesBefore.size() + newChangedRanges.size() + rangesAfter.size());

      myRanges.addAll(rangesBefore);
      myRanges.addAll(newChangedRanges);
      myRanges.addAll(rangesAfter);

      for (Range range : changedRanges) {
        range.invalidate();
      }
      myToBeDestroyedRanges.addAll(changedRanges);
      myToBeInstalledRanges.addAll(newChangedRanges);

      if (myRanges.isEmpty()) {
        fireFileUnchanged();
      }
    }
  }
  catch (ProcessCanceledException ignore) {
  }
  catch (FilesTooBigForDiffException e1) {
    destroyRanges();
    installAnathema();
  }
}