@Override public Object[] getDirectoryCachedValueDependencies(PsiPackage psiPackage) { return new Object[] { PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT, ProjectRootManager.getInstance(psiPackage.getProject()) }; }
private static PsiDirectory[] suggestMostAppropriateDirectories(PsiPackage psiPackage) { final Project project = psiPackage.getProject(); PsiDirectory[] directories = null; final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor != null) { final Document document = editor.getDocument(); final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document); if (psiFile != null) { final Module module = ModuleUtil.findModuleForPsiElement(psiFile); if (module != null) { directories = psiPackage.getDirectories(GlobalSearchScope.moduleWithDependenciesScope(module)); } else { directories = psiPackage.getDirectories( GlobalSearchScope.notScope(GlobalSearchScope.projectScope(project))); } } } if (directories == null || directories.length == 0) { directories = psiPackage.getDirectories(); } return directories; }
@Override public void navigate(final PsiPackage psiPackage, final boolean requestFocus) { final Project project = psiPackage.getProject(); ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.PROJECT_VIEW); window.activate(null); window .getActivation() .doWhenDone( new Runnable() { @Override public void run() { final ProjectView projectView = ProjectView.getInstance(project); PsiDirectory[] directories = suggestMostAppropriateDirectories(psiPackage); if (directories.length == 0) return; projectView.select(directories[0], directories[0].getVirtualFile(), requestFocus); } }); }
@Override public void handleQualifiedNameChange( final PsiPackage psiPackage, final String newQualifiedName) { ApplicationManager.getApplication().assertWriteAccessAllowed(); final String oldQualifedName = psiPackage.getQualifiedName(); final boolean anyChanged = changePackagePrefixes(psiPackage, oldQualifedName, newQualifiedName); if (anyChanged) { UndoManager.getInstance(psiPackage.getProject()) .undoableActionPerformed( new GlobalUndoableAction() { @Override public void undo() { changePackagePrefixes(psiPackage, newQualifiedName, oldQualifedName); } @Override public void redo() { changePackagePrefixes(psiPackage, oldQualifedName, newQualifiedName); } }); } }
private static boolean changePackagePrefixes( PsiPackage psiPackage, final String oldQualifiedName, final String newQualifiedName) { final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); List<ModifiableRootModel> modelsToCommit = new ArrayList<ModifiableRootModel>(); for (final Module module : modules) { boolean anyChange = false; final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); for (final ContentEntry contentEntry : rootModel.getContentEntries()) { for (final SourceFolder sourceFolder : contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(oldQualifiedName)) { sourceFolder.setPackagePrefix( newQualifiedName + packagePrefix.substring(oldQualifiedName.length())); anyChange = true; } } } if (anyChange) { modelsToCommit.add(rootModel); } else { rootModel.dispose(); } } if (!modelsToCommit.isEmpty()) { ModifiableRootModel[] rootModels = modelsToCommit.toArray(new ModifiableRootModel[modelsToCommit.size()]); if (rootModels.length > 0) { ModifiableModelCommitter.multiCommit( rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel()); } return true; } else { return false; } }
@Override public VirtualFile[] occursInPackagePrefixes(PsiPackage psiPackage) { List<VirtualFile> result = new ArrayList<VirtualFile>(); final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); for (final Module module : modules) { for (final ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) { final List<SourceFolder> sourceFolders = contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES); for (final SourceFolder sourceFolder : sourceFolders) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(psiPackage.getQualifiedName())) { final VirtualFile file = sourceFolder.getFile(); if (file != null) { result.add(file); } } } } } return VfsUtil.toVirtualFileArray(result); }
@Override public GlobalSearchScope adjustAllScope( PsiPackage psiPackage, GlobalSearchScope globalSearchScope) { return NonClasspathClassFinder.addNonClasspathScope(psiPackage.getProject(), globalSearchScope); }
@Override public boolean packagePrefixExists(PsiPackage psiPackage) { return PackagePrefixElementFinder.getInstance(psiPackage.getProject()) .packagePrefixExists(psiPackage.getQualifiedName()); }