@NotNull private static DartLibInfo collectPackagesLibraryRoots( @NotNull final Project project, @NotNull final DartSdk sdk) { final DartLibInfo libInfo = new DartLibInfo(false); final Collection<VirtualFile> pubspecYamlFiles = FilenameIndex.getVirtualFilesByName( project, PUBSPEC_YAML, GlobalSearchScope.projectScope(project)); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); for (VirtualFile pubspecFile : pubspecYamlFiles) { final VirtualFile dotPackagesFile = pubspecFile.getParent().findChild(DotPackagesFileUtil.DOT_PACKAGES); final Module module = dotPackagesFile == null ? null : fileIndex.getModuleForFile(dotPackagesFile); if (dotPackagesFile != null && !dotPackagesFile.isDirectory() && module != null && DartSdkGlobalLibUtil.isDartSdkEnabled(module)) { final Map<String, String> packagesMap = DotPackagesFileUtil.getPackagesMap(dotPackagesFile); if (packagesMap != null) { for (Map.Entry<String, String> entry : packagesMap.entrySet()) { final String packageName = entry.getKey(); final String packagePath = entry.getValue(); if (isPathOutsideProjectContent(fileIndex, packagePath)) { libInfo.addPackage(packageName, packagePath); } } } } } return libInfo; }
@Override public void annotateExternally( @NotNull final PsiModifierListOwner listOwner, @NotNull final String annotationFQName, @NotNull final PsiFile fromFile, final PsiNameValuePair[] value) { final Project project = myPsiManager.getProject(); final PsiFile containingFile = listOwner.getContainingFile(); if (!(containingFile instanceof PsiJavaFile)) { return; } final String packageName = ((PsiJavaFile) containingFile).getPackageName(); final VirtualFile virtualFile = containingFile.getVirtualFile(); LOG.assertTrue(virtualFile != null); final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile); if (entries.isEmpty()) { return; } for (final OrderEntry entry : entries) { if (entry instanceof ModuleOrderEntry) continue; VirtualFile[] virtualFiles = AnnotationOrderRootType.getFiles(entry); virtualFiles = filterByReadOnliness(virtualFiles); if (virtualFiles.length > 0) { chooseRootAndAnnotateExternally( listOwner, annotationFQName, fromFile, project, packageName, virtualFile, virtualFiles, value); } else { if (ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment()) { return; } SwingUtilities.invokeLater( new Runnable() { @Override public void run() { setupRootAndAnnotateExternally( entry, project, listOwner, annotationFQName, fromFile, packageName, virtualFile, value); } }); } break; } }
public static void removeDuplicatingClasses( final Module module, @NotNull final String packageName, @NotNull String className, @Nullable File classFile, String sourceRootPath) { if (sourceRootPath == null) { return; } VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByPath(sourceRootPath); if (sourceRoot == null) { return; } final Project project = module.getProject(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final String interfaceQualifiedName = packageName + '.' + className; PsiClass[] classes = facade.findClasses(interfaceQualifiedName, GlobalSearchScope.moduleScope(module)); final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex(); for (PsiClass c : classes) { PsiFile psiFile = c.getContainingFile(); if (className.equals(FileUtil.getNameWithoutExtension(psiFile.getName()))) { VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile != null && projectFileIndex.getSourceRootForFile(virtualFile) == sourceRoot) { final String path = virtualFile.getPath(); File f = new File(path); try { f = f.getCanonicalFile(); classFile = classFile != null ? classFile.getCanonicalFile() : null; if (f != null && !f.equals(classFile) && f.exists()) { if (f.delete()) { virtualFile.refresh(true, false); } else { ApplicationManager.getApplication() .invokeLater( new Runnable() { public void run() { Messages.showErrorDialog( project, "Can't delete file " + path, CommonBundle.getErrorTitle()); } }, project.getDisposed()); } } } catch (IOException e) { LOG.info(e); } } } } }
private boolean isExcludeRoot(VirtualFile file) { VirtualFile parent = file.getParent(); if (parent == null) return false; Module module = myProjectRootManager.getFileIndex().getModuleForFile(parent); if (module == null) return false; VirtualFile[] excludeRoots = ModuleRootManager.getInstance(module).getExcludeRoots(); for (VirtualFile root : excludeRoots) { if (root.equals(file)) return true; } return false; }
@Override public void run() { Project project = myModule.getProject(); if (GoSdkService.getInstance(project).isGoModule(myModule)) { synchronized (myLastHandledGoPathSourcesRoots) { Collection<VirtualFile> goPathSourcesRoots = GoSdkUtil.getGoPathSources(project, myModule); Set<VirtualFile> excludeRoots = ContainerUtil.newHashSet(ProjectRootManager.getInstance(project).getContentRoots()); ProgressIndicatorProvider.checkCanceled(); if (!myLastHandledGoPathSourcesRoots.equals(goPathSourcesRoots) || !myLastHandledExclusions.equals(excludeRoots)) { Collection<VirtualFile> includeRoots = gatherIncludeRoots(goPathSourcesRoots, excludeRoots); ApplicationManager.getApplication() .invokeLater( () -> { if (!myModule.isDisposed() && GoSdkService.getInstance(project).isGoModule(myModule)) { attachLibraries(includeRoots, excludeRoots); } }); myLastHandledGoPathSourcesRoots.clear(); myLastHandledGoPathSourcesRoots.addAll(goPathSourcesRoots); myLastHandledExclusions.clear(); myLastHandledExclusions.addAll(excludeRoots); List<String> paths = ContainerUtil.map(goPathSourcesRoots, VirtualFile::getPath); myWatchedRequests.clear(); myWatchedRequests.addAll(LocalFileSystem.getInstance().addRootsToWatch(paths, true)); } } } else { synchronized (myLastHandledGoPathSourcesRoots) { LocalFileSystem.getInstance().removeWatchedRoots(myWatchedRequests); myLastHandledGoPathSourcesRoots.clear(); myLastHandledExclusions.clear(); ApplicationManager.getApplication() .invokeLater( () -> { if (!myModule.isDisposed() && GoSdkService.getInstance(project).isGoModule(myModule)) { removeLibraryIfNeeded(); } }); } } }
@Nullable public VirtualFile findAppDirectory(@Nullable PsiElement element) { if (element == null) return null; PsiFile containingFile = element.getContainingFile().getOriginalFile(); VirtualFile file = containingFile.getVirtualFile(); if (file == null) return null; ProjectFileIndex index = ProjectRootManager.getInstance(containingFile.getProject()).getFileIndex(); VirtualFile root = index.getContentRootForFile(file); if (root == null) return null; return root.findChild(getApplicationDirectoryName()); }
public PsiVFSListener(Project project) { myProject = project; myFileTypeManager = FileTypeManager.getInstance(); myProjectRootManager = ProjectRootManager.getInstance(project); myManager = (PsiManagerImpl) PsiManager.getInstance(project); myFileManager = (FileManagerImpl) myManager.getFileManager(); myConnection = project.getMessageBus().connect(project); StartupManager.getInstance(project) .registerPreStartupActivity( new Runnable() { @Override public void run() { final BulkVirtualFileListenerAdapter adapter = new BulkVirtualFileListenerAdapter(PsiVFSListener.this); myConnection.subscribe( VirtualFileManager.VFS_CHANGES, new BulkFileListener() { @Override public void before(@NotNull List<? extends VFileEvent> events) { adapter.before(events); } @Override public void after(@NotNull List<? extends VFileEvent> events) { myReportedUnloadedPsiChange = false; adapter.after(events); myReportedUnloadedPsiChange = false; } }); myConnection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyModuleRootListener()); myConnection.subscribe( FileTypeManager.TOPIC, new FileTypeListener.Adapter() { @Override public void fileTypesChanged(@NotNull FileTypeEvent e) { myFileManager.processFileTypesChanged(); } }); myConnection.subscribe( AppTopics.FILE_DOCUMENT_SYNC, new MyFileDocumentManagerAdapter()); myFileManager.markInitialized(); } }); }
@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; }