@Nullable private ProblemDescriptor[] checkMember( final PsiDocCommentOwner docCommentOwner, final InspectionManager manager, final boolean isOnTheFly) { final ArrayList<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>(); final PsiDocComment docComment = docCommentOwner.getDocComment(); if (docComment == null) return null; final Set<PsiJavaCodeReferenceElement> references = new HashSet<PsiJavaCodeReferenceElement>(); docComment.accept(getVisitor(references, docCommentOwner, problems, manager, isOnTheFly)); for (PsiJavaCodeReferenceElement reference : references) { final List<PsiClass> classesToImport = new ImportClassFix(reference).getClassesToImport(); final PsiElement referenceNameElement = reference.getReferenceNameElement(); problems.add( manager.createProblemDescriptor( referenceNameElement != null ? referenceNameElement : reference, cannotResolveSymbolMessage("<code>" + reference.getText() + "</code>"), !isOnTheFly || classesToImport.isEmpty() ? null : new AddImportFix(classesToImport), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, isOnTheFly)); } return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]); }
public void applyFix( @NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { final PsiElement element = PsiTreeUtil.getParentOfType( descriptor.getPsiElement(), PsiJavaCodeReferenceElement.class); if (element instanceof PsiJavaCodeReferenceElement) { final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) element; Collections.sort( myClassesToImport, new PsiProximityComparator(referenceElement.getElement())); final JList list = new JBList(myClassesToImport.toArray(new PsiClass[myClassesToImport.size()])); list.setCellRenderer(new FQNameCellRenderer()); final Runnable runnable = new Runnable() { public void run() { if (!element.isValid()) return; final int index = list.getSelectedIndex(); if (index < 0) return; new WriteCommandAction(project, element.getContainingFile()) { protected void run(final Result result) throws Throwable { final PsiClass psiClass = myClassesToImport.get(index); if (psiClass.isValid()) { PsiDocumentManager.getInstance(project).commitAllDocuments(); referenceElement.bindToElement(psiClass); } } }.execute(); } }; final AsyncResult<DataContext> asyncResult = DataManager.getInstance().getDataContextFromFocus(); asyncResult.doWhenDone( new AsyncResult.Handler<DataContext>() { @Override public void run(DataContext dataContext) { final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext); assert editor != null; // available for on the fly mode only new PopupChooserBuilder(list) .setTitle(QuickFixBundle.message("class.to.import.chooser.title")) .setItemChoosenCallback(runnable) .createPopup() .showInBestPositionFor(editor); } }); } }