Java Code Examples for com.intellij.openapi.util.Condition#value()

The following examples show how to use com.intellij.openapi.util.Condition#value() . 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: UsefulPsiTreeUtil.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
public static PsiElement getSiblingSkippingCondition(@Nullable PsiElement sibling,
		Function<PsiElement, PsiElement> nextSibling,
		Condition<PsiElement> condition,
		boolean strictly)
{
	if(sibling == null)
	{
		return null;
	}
	if(sibling instanceof PsiFile)
	{
		return sibling;
	}
	PsiElement result = strictly ? nextSibling.fun(sibling) : sibling;
	while(result != null && !(result instanceof PsiFile) && condition.value(result))
	{
		result = nextSibling.fun(result);
	}
	return result;
}
 
Example 2
Source File: CSharpCompletionUtil.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
public static void elementToLookup(@Nonnull CompletionResultSet resultSet,
		@Nonnull IElementType elementType,
		@Nullable NotNullPairFunction<LookupElementBuilder, IElementType, LookupElement> decorator,
		@Nullable Condition<IElementType> condition)
{
	if(condition != null && !condition.value(elementType))
	{
		return;
	}

	String keyword = ourCache.get(elementType);
	LookupElementBuilder builder = LookupElementBuilder.create(elementType, keyword);
	builder = builder.bold();
	LookupElement item = builder;
	if(decorator != null)
	{
		item = decorator.fun(builder, elementType);
	}

	item.putUserData(KEYWORD_ELEMENT_TYPE, elementType);
	resultSet.addElement(item);
}
 
Example 3
Source File: ArtifactUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void removeChildrenRecursively(@Nonnull CompositePackagingElement<?> element,
                                             @Nonnull Condition<PackagingElement<?>> condition) {
  List<PackagingElement<?>> toRemove = new ArrayList<PackagingElement<?>>();
  for (PackagingElement<?> child : element.getChildren()) {
    if (child instanceof CompositePackagingElement<?>) {
      final CompositePackagingElement<?> compositeChild = (CompositePackagingElement<?>)child;
      removeChildrenRecursively(compositeChild, condition);
      if (compositeChild.getChildren().isEmpty()) {
        toRemove.add(child);
      }
    }
    else if (condition.value(child)) {
      toRemove.add(child);
    }
  }

  element.removeChildren(toRemove);
}
 
Example 4
Source File: ActionsTreeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void addEditorActions(final Condition<AnAction> filtered,
                                     final DefaultActionGroup editorGroup,
                                     final ArrayList<String> ids) {
  AnAction[] editorActions = editorGroup.getChildActionsOrStubs();
  final ActionManager actionManager = ActionManager.getInstance();
  for (AnAction editorAction : editorActions) {
    if (editorAction instanceof DefaultActionGroup) {
      addEditorActions(filtered, (DefaultActionGroup) editorAction, ids);
    }
    else {
      String actionId = editorAction instanceof ActionStub ? ((ActionStub)editorAction).getId() : actionManager.getId(editorAction);
      if (actionId == null) continue;
      if (actionId.startsWith(EDITOR_PREFIX)) {
        AnAction action = actionManager.getActionOrStub('$' + actionId.substring(6));
        if (action != null) continue;
      }
      if (filtered == null || filtered.value(editorAction)) {
        ids.add(actionId);
      }
    }
  }
}
 
Example 5
Source File: DebuggerKeymapExtension.java    From consulo with Apache License 2.0 6 votes vote down vote up
public KeymapGroup createGroup(final Condition<AnAction> filtered, final Project project) {
  ActionManager actionManager = ActionManager.getInstance();
  DefaultActionGroup debuggerGroup = (DefaultActionGroup)actionManager.getActionOrStub(IdeActions.GROUP_DEBUGGER);
  AnAction[] debuggerActions = debuggerGroup.getChildActionsOrStubs();

  ArrayList<String> ids = new ArrayList<String>();
  for (AnAction debuggerAction : debuggerActions) {
    String actionId = debuggerAction instanceof ActionStub ? ((ActionStub)debuggerAction).getId() : actionManager.getId(debuggerAction);
    if (filtered == null || filtered.value(debuggerAction)) {
      ids.add(actionId);
    }
  }

  Collections.sort(ids);
  Group group = new Group(KeyMapBundle.message("debugger.actions.group.title"), IdeActions.GROUP_DEBUGGER, null);
  for (String id : ids) {
    group.addActionId(id);
  }

  return group;
}
 
Example 6
Source File: ObjectUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static <T> T nullizeByCondition(@Nullable final T obj, @Nonnull final Condition<T> condition) {
  if (condition.value(obj)) {
    return null;
  }
  return obj;
}
 
Example 7
Source File: PsiEquivalenceUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static PsiElement[] getFilteredChildren(@Nonnull final PsiElement element,
                                               @Nullable Condition<PsiElement> isElementSignificantCondition,
                                               boolean areCommentsSignificant) {
  ASTNode[] children1 = element.getNode().getChildren(null);
  ArrayList<PsiElement> array = new ArrayList<PsiElement>();
  for (ASTNode node : children1) {
    final PsiElement child = node.getPsi();
    if (!(child instanceof PsiWhiteSpace) && (areCommentsSignificant || !(child instanceof PsiComment)) &&
        (isElementSignificantCondition == null || isElementSignificantCondition.value(child))) {
      array.add(child);
    }
  }
  return PsiUtilCore.toPsiElementArray(array);
}
 
Example 8
Source File: PsiTreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiElement findFirstParent(@Nullable PsiElement element, boolean strict, Condition<PsiElement> condition) {
  if (strict && element != null) {
    element = element.getParent();
  }

  while (element != null) {
    if (condition.value(element)) {
      return element;
    }
    element = element.getParent();
  }
  return null;
}
 
Example 9
Source File: ActionsTreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static Group createMacrosGroup(Condition<AnAction> filtered) {
  final ActionManagerEx actionManager = ActionManagerEx.getInstanceEx();
  String[] ids = actionManager.getActionIds(ActionMacro.MACRO_ACTION_PREFIX);
  Arrays.sort(ids);
  Group group = new Group(KeyMapBundle.message("macros.group.title"), null, null);
  for (String id : ids) {
    if (filtered == null || filtered.value(actionManager.getActionOrStub(id))) {
      group.addActionId(id);
    }
  }
  return group;
}
 
Example 10
Source File: FileFilterPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
SearchScope getSearchScope() {
  if (!myUseFileMask.isSelected()) return null;
  String text = (String)myFileMask.getSelectedItem();
  if (text == null) return null;

  final Condition<CharSequence> patternCondition = FindInProjectUtil.createFileMaskCondition(text);
  return new GlobalSearchScope() {
    @Override
    public boolean contains(@Nonnull VirtualFile file) {
      return patternCondition.value(file.getNameSequence());
    }

    @Override
    public int compare(@Nonnull VirtualFile file1, @Nonnull VirtualFile file2) {
      return 0;
    }

    @Override
    public boolean isSearchInModuleContent(@Nonnull Module aModule) {
      return true;
    }

    @Override
    public boolean isSearchInLibraries() {
      return true;
    }
  };
}
 
Example 11
Source File: PsiElementRenameHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isVetoed(PsiElement element) {
  if (element == null || element instanceof SyntheticElement) return true;
  for (Condition<PsiElement> condition : Extensions.getExtensions(VETO_RENAME_CONDITION_EP)) {
    if (condition.value(element)) return true;
  }
  return false;
}
 
Example 12
Source File: TreeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static DefaultMutableTreeNode findNode(@Nonnull final DefaultMutableTreeNode aRoot, @Nonnull final Condition<? super DefaultMutableTreeNode> condition) {
  if (condition.value(aRoot)) {
    return aRoot;
  }
  else {
    for (int i = 0; i < aRoot.getChildCount(); i++) {
      final DefaultMutableTreeNode candidate = findNode((DefaultMutableTreeNode)aRoot.getChildAt(i), condition);
      if (null != candidate) {
        return candidate;
      }
    }
    return null;
  }
}
 
Example 13
Source File: WordSelectioner.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canSelect(PsiElement e) {
  if (e instanceof PsiComment || e instanceof PsiWhiteSpace) {
    return false;
  }
  for (Condition<PsiElement> filter : Extensions.getExtensions(EP_NAME)) {
    if (!filter.value(e)) {
      return false;
    }
  }
  return true;
}
 
Example 14
Source File: ListUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static <T> List<T> removeIndices(@Nonnull JList<T> list, @Nonnull int[] indices, @Nullable Condition<? super T> condition) {
  if (indices.length == 0) {
    return new ArrayList<>(0);
  }
  ListModel<T> model = list.getModel();
  ListModelExtension<T, ListModel<T>> extension = getExtension(model);
  int firstSelectedIndex = indices[0];
  ArrayList<T> removedItems = new ArrayList<>();
  int deletedCount = 0;
  for (int idx1 : indices) {
    int index = idx1 - deletedCount;
    if (index < 0 || index >= model.getSize()) continue;
    T obj = extension.get(model, index);
    if (condition == null || condition.value(obj)) {
      removedItems.add(obj);
      extension.remove(model, index);
      deletedCount++;
    }
  }
  if (model.getSize() == 0) {
    list.clearSelection();
  }
  else if (list.getSelectedValue() == null) {
    // if nothing remains selected, set selected row
    if (firstSelectedIndex >= model.getSize()) {
      list.setSelectedIndex(model.getSize() - 1);
    }
    else {
      list.setSelectedIndex(firstSelectedIndex);
    }
  }
  return removedItems;
}
 
Example 15
Source File: ShowDiffAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void showDiffForChange(@Nullable Project project,
                                     @Nonnull Iterable<Change> changes,
                                     @Nonnull Condition<Change> condition,
                                     @Nonnull ShowDiffContext context) {
  int index = 0;
  List<ChangeDiffRequestProducer> presentables = new ArrayList<>();
  for (Change change : changes) {
    if (condition.value(change)) index = presentables.size();
    ChangeDiffRequestProducer presentable = ChangeDiffRequestProducer.create(project, change, context.getChangeContext(change));
    if (presentable != null) presentables.add(presentable);
  }

  showDiffForChange(project, presentables, index, context);
}
 
Example 16
Source File: PushedFilePropertiesUpdaterImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void filePropertiesChanged(@Nonnull VirtualFile fileOrDir, @Nonnull Condition<? super VirtualFile> acceptFileCondition) {
  if (fileOrDir.isDirectory()) {
    for (VirtualFile child : fileOrDir.getChildren()) {
      if (!child.isDirectory() && acceptFileCondition.value(child)) {
        filePropertiesChanged(child);
      }
    }
  }
  else if (acceptFileCondition.value(fileOrDir)) {
    filePropertiesChanged(fileOrDir);
  }
}
 
Example 17
Source File: FragmentGenerator.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private Set<Integer> getWalkNodes(int startNode, boolean isUp, Condition<Integer> stopFunction) {
  Set<Integer> walkNodes = new HashSet<>();

  TreeSetNodeIterator walker = new TreeSetNodeIterator(startNode, isUp);
  while (walker.notEmpty()) {
    Integer next = walker.pop();
    if (!stopFunction.value(next)) {
      walkNodes.add(next);
      walker.addAll(getNodes(next, isUp));
    }
  }

  return walkNodes;
}
 
Example 18
Source File: ObjectUtils.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static <T> T nullizeByCondition(@Nullable final T obj, @Nonnull final Condition<T> condition) {
  if (condition.value(obj)) {
    return null;
  }
  return obj;
}
 
Example 19
Source File: ComponentPopupBuilderImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public ComponentPopupBuilder setRequestFocusCondition(@Nonnull Project project, @Nonnull Condition<? super Project> condition) {
  myRequestFocus = condition.value(project);
  return this;
}
 
Example 20
Source File: BaseProjectTreeBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void _select(final Object element,
                     final VirtualFile file,
                     final boolean requestFocus,
                     final Condition<? super AbstractTreeNode> nonStopCondition,
                     final AsyncPromise<Object> result,
                     @Nonnull final ProgressIndicator indicator,
                     @Nullable final Ref<Object> virtualSelectTarget,
                     final FocusRequestor focusRequestor,
                     final boolean isSecondAttempt) {
  final AbstractTreeNode alreadySelected = alreadySelectedNode(element);

  final Runnable onDone = () -> {
    JTree tree = getTree();
    if (tree != null && requestFocus && virtualSelectTarget == null && getUi().isReady()) {
      tree.requestFocus();
    }

    result.setResult(null);
  };

  final Condition<AbstractTreeNode> condition = abstractTreeNode -> result.getState() == Promise.State.PENDING && nonStopCondition.value(abstractTreeNode);

  if (alreadySelected == null) {
    expandPathTo(file, (AbstractTreeNode)getTreeStructure().getRootElement(), element, condition, indicator, virtualSelectTarget).onSuccess(node -> {
      if (virtualSelectTarget == null) {
        select(node, onDone);
      }
      else {
        onDone.run();
      }
    }).onError(error -> {
      if (isSecondAttempt) {
        result.cancel();
      }
      else {
        _select(file, file, requestFocus, nonStopCondition, result, indicator, virtualSelectTarget, focusRequestor, true);
      }
    });
  }
  else if (virtualSelectTarget == null) {
    scrollTo(alreadySelected, onDone);
  }
  else {
    onDone.run();
  }
}