use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.
the class GenerateMissedTestsAction method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PsiClass srcClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (srcClass == null)
return;
final Collection<PsiElement> testClasses = TestFinderHelper.findTestsForClass(srcClass);
if (testClasses.isEmpty()) {
HintManager.getInstance().showErrorHint(editor, "No tests found.");
return;
}
if (testClasses.size() == 1) {
generateMissedTests((PsiClass) ContainerUtil.getFirstItem(testClasses), srcClass, editor);
return;
}
final JBList list = new JBList(testClasses);
list.setCellRenderer(new PsiClassListCellRenderer());
JBPopupFactory.getInstance().createListPopupBuilder(list).setItemChoosenCallback(() -> generateMissedTests((PsiClass) list.getSelectedValue(), srcClass, editor)).setTitle("Choose Test").createPopup().showInBestPositionFor(editor);
}
use of com.intellij.ide.util.PsiClassListCellRenderer 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.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.
the class ShowSiblingsAction method performForContext.
@Override
public void performForContext(@NotNull DataContext dataContext, final boolean invokedByShortcut) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
if (project == null)
return;
PsiDocumentManager.getInstance(project).commitAllDocuments();
final Editor editor = getEditor(dataContext);
PsiElement element = getElement(project, file, editor, CommonDataKeys.PSI_ELEMENT.getData(dataContext));
if (element == null && file == null)
return;
PsiFile containingFile = element != null ? element.getContainingFile() : file;
if (containingFile == null || !containingFile.getViewProvider().isPhysical())
return;
if (editor != null) {
PsiReference ref = TargetElementUtil.findReference(editor, editor.getCaretModel().getOffset());
if (element == null && ref != null) {
element = TargetElementUtil.getInstance().adjustReference(ref);
}
}
final PsiElement[] superElements = findSuperElements(element);
if (superElements.length == 0)
return;
final boolean isMethod = superElements[0] instanceof PsiMethod;
NavigatablePsiElement[] navigatablePsiElements = ContainerUtil.findAllAsArray(superElements, NavigatablePsiElement.class);
final JBPopup popup = PsiElementListNavigator.navigateOrCreatePopup(navigatablePsiElements, "Choose super " + (isMethod ? "method" : "class or interface"), "Super " + (isMethod ? "methods" : "classes/interfaces"), isMethod ? new MethodCellRenderer(false) : new PsiClassListCellRenderer(), null, objects -> showSiblings(invokedByShortcut, project, editor, file, editor != null, (PsiElement) objects[0]));
if (popup != null) {
if (editor != null) {
popup.showInBestPositionFor(editor);
} else {
popup.showCenteredInCurrentWindow(project);
}
}
}
use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.
the class BaseExpressionToFieldHandler method invokeImpl.
protected boolean invokeImpl(final Project project, @NotNull final PsiExpression selectedExpr, final Editor editor) {
final PsiElement element = getPhysicalElement(selectedExpr);
final PsiFile file = element.getContainingFile();
LOG.assertTrue(file != null, "expr.getContainingFile() == null");
if (LOG.isDebugEnabled()) {
LOG.debug("expression:" + selectedExpr);
}
final PsiType tempType = getTypeByExpression(selectedExpr);
if (tempType == null || LambdaUtil.notInferredType(tempType)) {
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("unknown.expression.type"));
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
return false;
}
if (PsiType.VOID.equals(tempType)) {
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("selected.expression.has.void.type"));
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
return false;
}
myParentClass = getParentClass(selectedExpr);
final List<PsiClass> classes = new ArrayList<>();
PsiClass aClass = myParentClass;
while (aClass != null) {
classes.add(aClass);
final PsiField psiField = ConvertToFieldRunnable.checkForwardRefs(selectedExpr, aClass);
if (psiField != null && psiField.getParent() == aClass)
break;
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
}
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
final boolean shouldSuggestDialog = activeIntroducer instanceof InplaceIntroduceConstantPopup && activeIntroducer.startsOnTheSameElement(selectedExpr, null);
if (classes.size() == 1 || editor == null || ApplicationManager.getApplication().isUnitTestMode() || shouldSuggestDialog) {
return !convertExpressionToField(selectedExpr, editor, file, project, tempType);
} else if (!classes.isEmpty()) {
PsiClass selection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, myParentClass);
NavigationUtil.getPsiElementPopup(classes.toArray(new PsiClass[classes.size()]), new PsiClassListCellRenderer(), "Choose class to introduce " + (myIsConstant ? "constant" : "field"), new PsiElementProcessor<PsiClass>() {
@Override
public boolean execute(@NotNull PsiClass aClass) {
AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, myParentClass);
myParentClass = aClass;
convertExpressionToField(selectedExpr, editor, file, project, tempType);
return false;
}
}, selection).showInBestPositionFor(editor);
}
return true;
}
use of com.intellij.ide.util.PsiClassListCellRenderer in project intellij-community by JetBrains.
the class CreateInnerClassFromUsageFix method chooseTargetClass.
private void chooseTargetClass(PsiClass[] classes, final Editor editor, final String superClassName) {
final Project project = classes[0].getProject();
final JList list = new JBList(classes);
PsiElementListCellRenderer renderer = new PsiClassListCellRenderer();
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setCellRenderer(renderer);
final PopupChooserBuilder builder = new PopupChooserBuilder(list);
renderer.installSpeedSearch(builder);
Runnable runnable = () -> {
int index = list.getSelectedIndex();
if (index < 0)
return;
doInvoke((PsiClass) list.getSelectedValue(), superClassName);
};
builder.setTitle(QuickFixBundle.message("target.class.chooser.title")).setItemChoosenCallback(runnable).createPopup().showInBestPositionFor(editor);
}
Aggregations