use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class InheritorChooser method runMethodInAbstractClass.
public boolean runMethodInAbstractClass(final ConfigurationContext context, final Runnable performRunnable, final PsiMethod psiMethod, final PsiClass containingClass, final Condition<PsiClass> acceptAbstractCondition) {
if (containingClass != null && acceptAbstractCondition.value(containingClass)) {
final Location location = context.getLocation();
if (location instanceof MethodLocation) {
final PsiClass aClass = ((MethodLocation) location).getContainingClass();
if (aClass != null && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
return false;
}
} else if (location instanceof PsiMemberParameterizedLocation) {
return false;
}
final List<PsiClass> classes = new ArrayList<>();
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
final boolean isJUnit5 = ReadAction.compute(() -> JUnitUtil.isJUnit5(containingClass));
ClassInheritorsSearch.search(containingClass).forEach(aClass -> {
if (isJUnit5 && JUnitUtil.isJUnit5TestClass(aClass, true) || PsiClassUtil.isRunnableClass(aClass, true, true)) {
classes.add(aClass);
}
return true;
});
}, "Search for " + containingClass.getQualifiedName() + " inheritors", true, containingClass.getProject())) {
return true;
}
if (classes.size() == 1) {
runForClass(classes.get(0), psiMethod, context, performRunnable);
return true;
}
if (classes.isEmpty())
return false;
final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context.getDataContext());
if (fileEditor instanceof TextEditor) {
final Document document = ((TextEditor) fileEditor).getEditor().getDocument();
final PsiFile containingFile = PsiDocumentManager.getInstance(context.getProject()).getPsiFile(document);
if (containingFile instanceof PsiClassOwner) {
final List<PsiClass> psiClasses = new ArrayList<>(Arrays.asList(((PsiClassOwner) containingFile).getClasses()));
psiClasses.retainAll(classes);
if (psiClasses.size() == 1) {
runForClass(psiClasses.get(0), psiMethod, context, performRunnable);
return true;
}
}
}
final int numberOfInheritors = classes.size();
final PsiClassListCellRenderer renderer = new PsiClassListCellRenderer() {
@Override
protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
if (value == null) {
renderer.append("All (" + numberOfInheritors + ")");
return true;
}
return super.customizeNonPsiElementLeftRenderer(renderer, list, value, index, selected, hasFocus);
}
};
Collections.sort(classes, renderer.getComparator());
//suggest to run all inherited tests
classes.add(0, null);
final JBList list = new JBList(classes);
list.setCellRenderer(renderer);
JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose executable classes to run " + (psiMethod != null ? psiMethod.getName() : containingClass.getName())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
final Object[] values = list.getSelectedValues();
if (values == null)
return;
chooseAndPerform(values, psiMethod, context, performRunnable, classes);
}).createPopup().showInBestPositionFor(context.getDataContext());
return true;
}
return false;
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class Divider method divideInsideAndOutsideInOneRoot.
private static void divideInsideAndOutsideInOneRoot(@NotNull PsiFile root, @NotNull TextRange restrictRange, @NotNull TextRange priorityRange, @NotNull List<PsiElement> inside, @NotNull List<ProperTextRange> insideRanges, @NotNull List<PsiElement> outside, @NotNull List<ProperTextRange> outsideRanges, @NotNull List<PsiElement> outParents, @NotNull List<ProperTextRange> outParentRanges, boolean includeParents) {
int startOffset = restrictRange.getStartOffset();
int endOffset = restrictRange.getEndOffset();
final Condition<PsiElement>[] filters = Extensions.getExtensions(CollectHighlightsUtil.EP_NAME);
final TIntStack starts = new TIntStack(STARTING_TREE_HEIGHT);
starts.push(startOffset);
final Stack<PsiElement> elements = new Stack<>(STARTING_TREE_HEIGHT);
final Stack<PsiElement> children = new Stack<>(STARTING_TREE_HEIGHT);
PsiElement element = root;
PsiElement child = HAVE_TO_GET_CHILDREN;
int offset = 0;
while (true) {
ProgressManager.checkCanceled();
for (Condition<PsiElement> filter : filters) {
if (!filter.value(element)) {
assert child == HAVE_TO_GET_CHILDREN;
// do not want to process children
child = null;
break;
}
}
boolean startChildrenVisiting;
if (child == HAVE_TO_GET_CHILDREN) {
startChildrenVisiting = true;
child = element.getFirstChild();
} else {
startChildrenVisiting = false;
}
if (child == null) {
if (startChildrenVisiting) {
// leaf element
offset += element.getTextLength();
}
int start = starts.pop();
if (startOffset <= start && offset <= endOffset) {
if (priorityRange.containsRange(start, offset)) {
inside.add(element);
insideRanges.add(new ProperTextRange(start, offset));
} else {
outside.add(element);
outsideRanges.add(new ProperTextRange(start, offset));
}
}
if (elements.isEmpty())
break;
element = elements.pop();
child = children.pop();
} else {
// composite element
if (offset > endOffset)
break;
children.push(child.getNextSibling());
starts.push(offset);
elements.push(element);
element = child;
child = HAVE_TO_GET_CHILDREN;
}
}
if (includeParents) {
PsiElement parent = !outside.isEmpty() ? outside.get(outside.size() - 1) : !inside.isEmpty() ? inside.get(inside.size() - 1) : CollectHighlightsUtil.findCommonParent(root, startOffset, endOffset);
while (parent != null && !(parent instanceof PsiFile)) {
parent = parent.getParent();
if (parent != null) {
outParents.add(parent);
TextRange textRange = parent.getTextRange();
assert textRange != null : "Text range for " + parent + " is null. " + parent.getClass() + "; root: " + root + ": " + root.getVirtualFile();
outParentRanges.add(ProperTextRange.create(textRange));
}
}
}
assert inside.size() == insideRanges.size();
assert outside.size() == outsideRanges.size();
assert outParents.size() == outParentRanges.size();
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class RollbackAction method actionPerformed.
public void actionPerformed(AnActionEvent e) {
Project project = e.getData(CommonDataKeys.PROJECT);
if (project == null) {
return;
}
final String title = ActionPlaces.CHANGES_VIEW_TOOLBAR.equals(e.getPlace()) ? null : "Can not " + UIUtil.removeMnemonic(RollbackUtil.getRollbackOperationName(project)) + " now";
if (ChangeListManager.getInstance(project).isFreezedWithNotification(title))
return;
FileDocumentManager.getInstance().saveAllDocuments();
List<FilePath> missingFiles = e.getData(ChangesListView.MISSING_FILES_DATA_KEY);
boolean hasChanges = false;
if (missingFiles != null && !missingFiles.isEmpty()) {
hasChanges = true;
new RollbackDeletionAction().actionPerformed(e);
}
List<Change> changes = getChanges(project, e);
final LinkedHashSet<VirtualFile> modifiedWithoutEditing = getModifiedWithoutEditing(e, project);
if (modifiedWithoutEditing != null && !modifiedWithoutEditing.isEmpty()) {
hasChanges = true;
rollbackModifiedWithoutEditing(project, modifiedWithoutEditing);
}
if (modifiedWithoutEditing != null) {
changes = ContainerUtil.filter(changes, new Condition<Change>() {
@Override
public boolean value(Change change) {
return !modifiedWithoutEditing.contains(change.getVirtualFile());
}
});
}
if (!changes.isEmpty()) {
RollbackChangesDialog.rollbackChanges(project, changes);
} else if (!hasChanges) {
LocalChangeList currentChangeList = ChangeListManager.getInstance(project).getDefaultChangeList();
RollbackChangesDialog.rollbackChanges(project, currentChangeList);
}
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class GitBranchPopup method getInstance.
/**
* @param currentRepository Current repository, which means the repository of the currently open or selected file.
* In the case of synchronized branch operations current repository matter much less, but sometimes is used,
* for example, it is preselected in the repositories combobox in the compare branches dialog.
*/
static GitBranchPopup getInstance(@NotNull final Project project, @NotNull GitRepository currentRepository) {
final GitVcsSettings vcsSettings = GitVcsSettings.getInstance(project);
Condition<AnAction> preselectActionCondition = action -> {
if (action instanceof GitBranchPopupActions.LocalBranchActions) {
GitBranchPopupActions.LocalBranchActions branchAction = (GitBranchPopupActions.LocalBranchActions) action;
String branchName = branchAction.getBranchName();
String recentBranch;
List<GitRepository> repositories = branchAction.getRepositories();
if (repositories.size() == 1) {
recentBranch = vcsSettings.getRecentBranchesByRepository().get(repositories.iterator().next().getRoot().getPath());
} else {
recentBranch = vcsSettings.getRecentCommonBranch();
}
if (recentBranch != null && recentBranch.equals(branchName)) {
return true;
}
}
return false;
};
return new GitBranchPopup(currentRepository, GitUtil.getRepositoryManager(project), vcsSettings, preselectActionCondition);
}
use of com.intellij.openapi.util.Condition in project intellij-community by JetBrains.
the class GrMethodMayBeStaticInspection method checkMethod.
private boolean checkMethod(final GrMethod method) {
if (method.hasModifierProperty(PsiModifier.STATIC))
return false;
if (method.hasModifierProperty(PsiModifier.SYNCHRONIZED))
return false;
if (method.getModifierList().hasExplicitModifier(PsiModifier.ABSTRACT))
return false;
if (method.isConstructor())
return false;
PsiClass containingClass = method.getContainingClass();
if (containingClass == null)
return false;
if (myIgnoreTraitMethods && containingClass instanceof GrTraitTypeDefinition)
return false;
if (SuperMethodsSearch.search(method, null, true, false).findFirst() != null)
return false;
if (OverridingMethodsSearch.search(method).findFirst() != null)
return false;
if (ignoreMethod(method))
return false;
if (myOnlyPrivateOrFinal) {
if (!(method.hasModifierProperty(PsiModifier.FINAL) || method.hasModifierProperty(PsiModifier.PRIVATE)))
return false;
}
GrOpenBlock block = method.getBlock();
if (block == null)
return false;
if (myIgnoreEmptyMethods && block.getStatements().length == 0)
return false;
if (containingClass.getContainingClass() != null && !containingClass.hasModifierProperty(PsiModifier.STATIC)) {
return false;
}
final Condition<PsiElement>[] addins = InspectionManager.CANT_BE_STATIC_EXTENSION.getExtensions();
for (Condition<PsiElement> addin : addins) {
if (addin.value(method)) {
return false;
}
}
MethodMayBeStaticVisitor visitor = new MethodMayBeStaticVisitor();
method.accept(visitor);
return !visitor.haveInstanceRefsOutsideClosures();
}
Aggregations