@Override protected boolean hasAnyAnnotationsRoots() { if (myHasAnyAnnotationsRoots == ThreeState.UNSURE) { final Module[] modules = ModuleManager.getInstance(myPsiManager.getProject()).getModules(); for (Module module : modules) { for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) { final String[] urls = AnnotationOrderRootType.getUrls(entry); if (urls.length > 0) { myHasAnyAnnotationsRoots = ThreeState.YES; return true; } } } myHasAnyAnnotationsRoots = ThreeState.NO; } return myHasAnyAnnotationsRoots == ThreeState.YES; }
@Override @NotNull protected List<VirtualFile> getExternalAnnotationsRoots(@NotNull VirtualFile libraryFile) { final List<OrderEntry> entries = ProjectRootManager.getInstance(myPsiManager.getProject()) .getFileIndex() .getOrderEntriesForFile(libraryFile); List<VirtualFile> result = new ArrayList<VirtualFile>(); for (OrderEntry entry : entries) { if (entry instanceof ModuleOrderEntry) { continue; } final String[] externalUrls = AnnotationOrderRootType.getUrls(entry); for (String url : externalUrls) { VirtualFile root = VirtualFileManager.getInstance().findFileByUrl(url); if (root != null) { result.add(root); } } } return result; }
@Override @NotNull public AnnotationPlace chooseAnnotationsPlace(@NotNull final PsiElement element) { if (!element.isPhysical()) return AnnotationPlace.IN_CODE; // element just created if (!element.getManager().isInProject(element)) return AnnotationPlace.EXTERNAL; final Project project = myPsiManager.getProject(); final PsiFile containingFile = element.getContainingFile(); final VirtualFile virtualFile = containingFile.getVirtualFile(); LOG.assertTrue(virtualFile != null); final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile); if (!entries.isEmpty()) { for (OrderEntry entry : entries) { if (!(entry instanceof ModuleOrderEntry)) { if (AnnotationOrderRootType.getUrls(entry).length > 0) { return AnnotationPlace.EXTERNAL; } break; } } } final MyExternalPromptDialog dialog = ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment() ? null : new MyExternalPromptDialog(project); if (dialog != null && dialog.isToBeShown()) { final PsiElement highlightElement = element instanceof PsiNameIdentifierOwner ? ((PsiNameIdentifierOwner) element).getNameIdentifier() : element.getNavigationElement(); LOG.assertTrue(highlightElement != null); final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); final List<RangeHighlighter> highlighters = new ArrayList<RangeHighlighter>(); final boolean highlight = editor != null && editor.getDocument() == PsiDocumentManager.getInstance(project).getDocument(containingFile); try { if (highlight) { // do not highlight for batch inspections final EditorColorsManager colorsManager = EditorColorsManager.getInstance(); final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES); final TextRange textRange = highlightElement.getTextRange(); HighlightManager.getInstance(project) .addRangeHighlight( editor, textRange.getStartOffset(), textRange.getEndOffset(), attributes, true, highlighters); final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(textRange.getStartOffset()); editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.CENTER); } dialog.show(); if (dialog.getExitCode() == 2) { return AnnotationPlace.EXTERNAL; } else if (dialog.getExitCode() == 1) { return AnnotationPlace.NOWHERE; } } finally { if (highlight) { HighlightManager.getInstance(project) .removeSegmentHighlighter(editor, highlighters.get(0)); } } } else if (dialog != null) { dialog.close(DialogWrapper.OK_EXIT_CODE); } return AnnotationPlace.IN_CODE; }