private boolean spansMultipleModules() { final String qualifiedName = getConfiguration().getPackage(); if (qualifiedName != null) { final Project project = getConfiguration().getProject(); final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(qualifiedName); if (aPackage != null) { final TestSearchScope scope = getScope(); if (scope != null) { final SourceScope sourceScope = scope.getSourceScope(getConfiguration()); if (sourceScope != null) { final GlobalSearchScope configurationSearchScope = GlobalSearchScopesCore.projectTestScope(project).intersectWith( sourceScope.getGlobalSearchScope()); final PsiDirectory[] directories = aPackage.getDirectories(configurationSearchScope); return directories.length > 1; } } } } return false; }
@NotNull @Override public Object[] getVariants() { final Project project = myElement.getProject(); PropertiesReferenceManager referenceManager = PropertiesReferenceManager.getInstance(project); final List<LookupElement> variants = new ArrayList<LookupElement>(); referenceManager.processPropertiesFiles(GlobalSearchScopesCore.projectProductionScope(project), new PropertiesFileProcessor() { public boolean process(String baseName, PropertiesFile propertiesFile) { final Icon icon = propertiesFile.getContainingFile().getIcon(Iconable.ICON_FLAG_READ_STATUS); final String relativePath = ProjectUtil.calcRelativeToProjectPath(propertiesFile.getVirtualFile(), project); variants.add(LookupElementBuilder.create(propertiesFile, baseName) .withIcon(icon) .withTailText(" (" + relativePath + ")", true)); return true; } }, this); return variants.toArray(new LookupElement[variants.size()]); }
protected boolean spansMultipleModules(final String qualifiedName) { if(qualifiedName != null) { final Project project = getConfiguration().getProject(); final PsiJavaPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(qualifiedName); if(aPackage != null) { final TestSearchScope scope = getScope(); if(scope != null) { final SourceScope sourceScope = scope.getSourceScope(getConfiguration()); if(sourceScope != null) { final GlobalSearchScope configurationSearchScope = GlobalSearchScopesCore.projectTestScope(project).intersectWith(sourceScope.getGlobalSearchScope()); final PsiDirectory[] directories = aPackage.getDirectories(configurationSearchScope); return Arrays.stream(directories).map(dir -> ModuleUtilCore.findModuleForFile(dir.getVirtualFile(), project)).filter(Objects::nonNull).distinct().count() > 1; } } } } return false; }
@Nullable private static PsiClass createTestClass(CreateTestDialog d) { final TestFramework testFrameworkDescriptor = d.getSelectedTestFrameworkDescriptor(); final FileTemplateDescriptor fileTemplateDescriptor = TestIntegrationUtils.MethodKind.TEST_CLASS.getFileTemplateDescriptor(testFrameworkDescriptor); final PsiDirectory targetDirectory = d.getTargetDirectory(); final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(targetDirectory); if (aPackage != null) { final GlobalSearchScope scope = GlobalSearchScopesCore.directoryScope(targetDirectory, false); final PsiClass[] classes = aPackage.findClassByShortName(d.getClassName(), scope); if (classes.length > 0) { if (!FileModificationService.getInstance().preparePsiElementForWrite(classes[0])) { return null; } return classes[0]; } } if (fileTemplateDescriptor != null) { final PsiClass classFromTemplate = createTestClassFromCodeTemplate(d, fileTemplateDescriptor, targetDirectory); if (classFromTemplate != null) { return classFromTemplate; } } return JavaDirectoryService.getInstance().createClass(targetDirectory, d.getClassName()); }
public void testNoFilesFromAdditionalIndexedRootsWithCustomExclusionScope() throws ParsingException { FindModel findModel = FindManagerTestUtils.configureFindModel("Object.prototype.__defineGetter__"); PackageSet compile = PackageSetFactory.getInstance().compile("!src[subdir]:*..*"); findModel.setCustomScope(GlobalSearchScopesCore.filterScope(myProject, new NamedScope.UnnamedScope(compile))); findModel.setCustomScope(true); assertSize(0, findUsages(findModel)); }
public void testScopeCreatedForFindInDirectory() { VirtualFile dir = getProject().getBaseDir(); FindModel findModel = new FindModel(); findModel.setDirectoryName(dir.getPath()); findModel.setWithSubdirectories(true); findModel.setProjectScope(false); UsageTarget target = new FindInProjectUtil.StringUsageTarget(getProject(), findModel); UsageViewManagerImpl manager = (UsageViewManagerImpl)UsageViewManager.getInstance(getProject()); SearchScope scope = manager.getMaxSearchScopeToWarnOfFallingOutOf(new UsageTarget[]{target}); assertEquals(scope, GlobalSearchScopesCore.directoryScope(getProject(), dir, true)); }
@Nullable @Override public SourceScope getSourceScope() { final String dirName = getConfiguration().getPersistentData().getDirName(); final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(dirName)); final GlobalSearchScope globalSearchScope = file == null ? GlobalSearchScope.EMPTY_SCOPE : GlobalSearchScopesCore.directoryScope( getConfiguration().getProject(), file, true); return new SourceScope() { @Override public GlobalSearchScope getGlobalSearchScope() { return globalSearchScope; } @Override public Project getProject() { return getConfiguration().getProject(); } @Override public GlobalSearchScope getLibrariesScope() { final Module module = getConfiguration().getConfigurationModule().getModule(); return module != null ? GlobalSearchScope.moduleWithLibrariesScope(module) : GlobalSearchScope.allScope( getConfiguration().getProject()); } @Override public Module[] getModulesToCompile() { final Collection<Module> validModules = getConfiguration().getValidModules(); return validModules.toArray(new Module[validModules.size()]); } }; }
public static Collection<IdeaPlugin> getAllPlugins(final Project project) { if (DumbService.isDumb(project)) return Collections.emptyList(); return CachedValuesManager.getManager(project).getCachedValue(project, new CachedValueProvider<Collection<IdeaPlugin>>() { @Nullable @Override public Result<Collection<IdeaPlugin>> compute() { GlobalSearchScope scope = GlobalSearchScopesCore.projectProductionScope(project). union(ProjectScope.getLibrariesScope(project)); return Result.create(getPlugins(project, scope), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT); } }); }
private static List<LanguageDefinition> collectLanguageDefinitions(final ConvertContext context) { final PsiClass languageClass = DomJavaUtil.findClass(Language.class.getName(), context.getInvocationElement()); if (languageClass == null) { return Collections.emptyList(); } final Project project = context.getProject(); final GlobalSearchScope projectProductionScope = GlobalSearchScopesCore.projectProductionScope(project); final Collection<PsiClass> allLanguages = CachedValuesManager.getCachedValue(languageClass, new CachedValueProvider<Collection<PsiClass>>() { @Nullable @Override public Result<Collection<PsiClass>> compute() { GlobalSearchScope allScope = projectProductionScope.union(ProjectScope.getLibrariesScope(project)); return Result.create(ClassInheritorsSearch.search(languageClass, allScope, true).findAll(), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); } }); ; final List<LanguageDefinition> libraryDefinitions = collectLibraryLanguages(context, allLanguages); final Collection<PsiClass> projectLanguages = ContainerUtil.filter(allLanguages, new Condition<PsiClass>() { @Override public boolean value(PsiClass aClass) { return PsiSearchScopeUtil.isInScope(projectProductionScope, aClass); } }); final List<LanguageDefinition> projectDefinitions = collectProjectLanguages(projectLanguages, libraryDefinitions); final List<LanguageDefinition> all = new ArrayList<LanguageDefinition>(libraryDefinitions); all.addAll(projectDefinitions); return all; }
private static boolean checkIdeaProject(@NotNull Project project) { if (!isIntelliJBasedDir(project.getBaseDir())) { return false; } GlobalSearchScope scope = GlobalSearchScopesCore.projectProductionScope(project); if (JavaPsiFacade.getInstance(project).findClass(IDE_PROJECT_MARKER_CLASS, scope) == null) { return false; } return true; }
private GlobalSearchScope getSearchScopeInner(Project project) { final RunConfigurationBase configuration = getRunConfiguration(); if (configuration instanceof ModuleBasedConfiguration) { final Module module = ((ModuleBasedConfiguration)configuration).getConfigurationModule().getModule(); if (module != null) { return GlobalSearchScope.moduleRuntimeScope(module, isTrackTestFolders()); } } return isTrackTestFolders() ? GlobalSearchScope.projectScope(project) : GlobalSearchScopesCore.projectProductionScope(project); }
/** * Creates a search scope for finding import files based on the endpoint entry file, falling back to * project scope in case no entry file has been configured. * @param project project to search * @param entryFile the entry file, or null to look it up */ @NotNull public static GlobalSearchScope getImportScopeFromEntryFile(Project project, @Nullable VirtualFile entryFile) { if(entryFile == null) { entryFile = JSGraphQLConfigurationProvider.getService(project).getEndpointEntryFile(); } final GlobalSearchScope scope; if(entryFile != null) { scope = GlobalSearchScopesCore.directoriesScope(project, true, entryFile.getParent()); } else { scope = GlobalSearchScope.projectScope(project); } return scope; }
public static Collection<IdeaPlugin> getAllPlugins(final Project project) { if (DumbService.isDumb(project)) return Collections.emptyList(); GlobalSearchScope scope = GlobalSearchScopesCore.projectProductionScope(project); List<DomFileElement<IdeaPlugin>> files = DomService.getInstance().getFileElements(IdeaPlugin.class, project, scope); return ContainerUtil.map(files, new Function<DomFileElement<IdeaPlugin>, IdeaPlugin>() { public IdeaPlugin fun(DomFileElement<IdeaPlugin> ideaPluginDomFileElement) { return ideaPluginDomFileElement.getRootElement(); } }); }
@NotNull private static Collection<VirtualFile> getFilesWithPotentialNullPassingCalls(@NotNull PsiMethod method, int parameterIndex) { final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance(); final CommonProcessors.CollectProcessor<VirtualFile> collector = new CommonProcessors.CollectProcessor<>(new ArrayList<>()); GlobalSearchScope searchScope = GlobalSearchScopeUtil.toGlobalSearchScope(method.getUseScope(), method.getProject()); searchScope = searchScope.intersectWith(GlobalSearchScopesCore.projectProductionScope(method.getProject())); fileBasedIndex.getFilesWithKey(JavaNullMethodArgumentIndex.INDEX_ID, Collections.singleton(new JavaNullMethodArgumentIndex.MethodCallData(method.getName(), parameterIndex)), collector, searchScope); return collector.getResults(); }
@NotNull public SearchScope toSearchScope() { switch (myType) { case CUSTOM: return myScope; case DIRECTORY: return GlobalSearchScopesCore.directoryScope((PsiDirectory)myElement, true); case FILE: return new LocalSearchScope(myElement); case INVALID: return LocalSearchScope.EMPTY; case MODULE: GlobalSearchScope moduleScope = GlobalSearchScope.moduleScope(myModule); return myIncludeTestSource ? moduleScope : GlobalSearchScope.notScope(GlobalSearchScopesCore.projectTestScope(myModule.getProject())).intersectWith(moduleScope); case MODULES: SearchScope scope = GlobalSearchScope.EMPTY_SCOPE; for (Module module : myModules) { scope = scope.union(GlobalSearchScope.moduleScope(module)); } return scope; case PROJECT: return myIncludeTestSource ? GlobalSearchScope.projectScope(myProject) : GlobalSearchScopesCore.projectProductionScope(myProject); case VIRTUAL_FILES: return new GlobalSearchScope() { @Override public boolean contains(@NotNull VirtualFile file) { return myFilesSet.contains(file); } @Override public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) { return 0; } @Override public boolean isSearchInModuleContent(@NotNull Module aModule) { return false; } @Override public boolean isSearchInLibraries() { return false; } }; default: LOG.error("invalid type " + myType); return GlobalSearchScope.EMPTY_SCOPE; } }
@NotNull protected static List<Location> collectRelativeLocations(Project project, VirtualFile file) { if (DumbService.isDumb(project)) return Collections.emptyList(); final List<Location> locations = new ArrayList<Location>(); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); if (fileIndex.isInContent(file) && !fileIndex.isInSource(file) && !fileIndex.isInLibraryClasses(file)) { final VirtualFile parent = file.getParent(); final VirtualFile contentRoot = fileIndex.getContentRootForFile(file); if (contentRoot != null) { final String relativePath = VfsUtilCore.getRelativePath(parent, contentRoot, '/'); if (relativePath != null) { final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project); final List<String> words = StringUtil.getWordsIn(relativePath); // put longer strings first Collections.sort(words, new Comparator<String>() { @Override public int compare(final String o1, final String o2) { return o2.length() - o1.length(); } }); final GlobalSearchScope testScope = GlobalSearchScopesCore.projectTestScope(project); Set<PsiFile> resultFiles = null; for (String word : words) { if (word.length() < 5) { continue; } final Set<PsiFile> files = new THashSet<PsiFile>(); searchHelper.processAllFilesWithWordInLiterals(word, testScope, new CommonProcessors.CollectProcessor<PsiFile>(files)); if (resultFiles == null) { resultFiles = files; } else { resultFiles.retainAll(files); } if (resultFiles.isEmpty()) break; } if (resultFiles != null) { for (Iterator<PsiFile> iterator = resultFiles.iterator(); iterator.hasNext(); ) { if (!VfsUtilCore.isAncestor(contentRoot, iterator.next().getVirtualFile(), true)) { iterator.remove(); } } final String fileName = file.getName(); final String nameWithoutExtension = file.getNameWithoutExtension(); for (PsiFile resultFile : resultFiles) { if (resultFile instanceof PsiClassOwner) { final PsiClass[] classes = ((PsiClassOwner)resultFile).getClasses(); if (classes.length > 0) { ContainerUtil.addIfNotNull(locations, getLocation(project, fileName, nameWithoutExtension, classes[0])); } } } } } } } return locations; }
@NotNull public SearchScope toSearchScope() { switch (myType) { case CUSTOM: return myScope; case DIRECTORY: return GlobalSearchScopesCore.directoryScope((PsiDirectory)myElement, true); case FILE: return new LocalSearchScope(myElement); case INVALID: return LocalSearchScope.EMPTY; case MODULE: GlobalSearchScope moduleScope = GlobalSearchScope.moduleScope(myModule); return myIncludeTestSource ? moduleScope : GlobalSearchScope.notScope(GlobalSearchScopesCore.projectTestScope(myModule.getProject())).intersectWith(moduleScope); case MODULES: SearchScope scope = GlobalSearchScope.EMPTY_SCOPE; for (Module module : myModules) { scope = scope.union(GlobalSearchScope.moduleScope(module)); } return scope; case PROJECT: return myIncludeTestSource ? GlobalSearchScope.projectScope(myProject) : GlobalSearchScopesCore.projectProductionScope(myProject); case VIRTUAL_FILES: return new GlobalSearchScope() { @Override public boolean contains(VirtualFile file) { return myFilesSet.contains(file); } @Override public int compare(VirtualFile file1, VirtualFile file2) { return 0; } @Override public boolean isSearchInModuleContent(@NotNull Module aModule) { return false; } @Override public boolean isSearchInLibraries() { return false; } }; default: LOG.error("invalid type " + myType); return GlobalSearchScope.EMPTY_SCOPE; } }
@Nonnull public SearchScope toSearchScope() { switch (myType) { case CUSTOM: return myScope; case DIRECTORY: return GlobalSearchScopesCore.directoryScope((PsiDirectory)myElement, true); case FILE: return new LocalSearchScope(myElement); case INVALID: return LocalSearchScope.EMPTY; case MODULE: GlobalSearchScope moduleScope = GlobalSearchScope.moduleScope(myModule); return myIncludeTestSource ? moduleScope : GlobalSearchScope.notScope(GlobalSearchScopesCore.projectTestScope(myModule.getProject())).intersectWith(moduleScope); case MODULES: SearchScope scope = GlobalSearchScope.EMPTY_SCOPE; for (Module module : myModules) { scope = scope.union(GlobalSearchScope.moduleScope(module)); } return scope; case PROJECT: return myIncludeTestSource ? GlobalSearchScope.projectScope(myProject) : GlobalSearchScopesCore.projectProductionScope(myProject); case VIRTUAL_FILES: return new GlobalSearchScope() { @Override public boolean contains(@Nonnull VirtualFile file) { return myFilesSet.contains(file); } @Override public int compare(@Nonnull VirtualFile file1, @Nonnull VirtualFile file2) { return 0; } @Override public boolean isSearchInModuleContent(@Nonnull Module aModule) { return false; } @Override public boolean isSearchInLibraries() { return false; } }; default: LOG.error("invalid type " + myType); return GlobalSearchScope.EMPTY_SCOPE; } }