use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class DuplicateStringLiteralInspection method getCandidateFiles.
@NotNull
private Set<PsiFile> getCandidateFiles(String stringToFind, Project project) {
final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
final List<String> words = StringUtil.getWordsInStringLongestFirst(stringToFind);
if (words.isEmpty())
return Collections.emptySet();
Set<PsiFile> resultFiles = null;
for (String word : words) {
if (word.length() < MIN_STRING_LENGTH) {
continue;
}
ProgressManager.checkCanceled();
final Set<PsiFile> files = new THashSet<>();
Processor<PsiFile> processor = Processors.cancelableCollectProcessor(files);
searchHelper.processAllFilesWithWordInLiterals(word, scope, processor);
if (resultFiles == null) {
resultFiles = files;
} else {
resultFiles.retainAll(files);
}
if (resultFiles.isEmpty())
return Collections.emptySet();
}
return resultFiles != null ? resultFiles : Collections.emptySet();
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class JavaFxImplicitUsageProvider method isFxmlUsage.
private static boolean isFxmlUsage(PsiMember member, GlobalSearchScope scope) {
final String name = member.getName();
if (name == null)
return false;
final Project project = member.getProject();
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
final PsiSearchHelper.SearchCostResult searchCost = RefResolveService.getInstance(project).isUpToDate() ? PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES : searchHelper.isCheapEnoughToSearch(name, scope, null, null);
if (searchCost == PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
final Query<PsiReference> query = ReferencesSearch.search(member, scope);
return query.findFirst() != null;
}
return false;
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class DuplicatePropertyInspection method checkFile.
private void checkFile(final PsiFile file, final InspectionManager manager, GlobalInspectionContextBase context, final RefManager refManager, final ProblemDescriptionsProcessor processor) {
if (!(file instanceof PropertiesFile))
return;
if (!context.isToCheckFile(file, this) || SuppressionUtil.inspectionResultSuppressed(file, this))
return;
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
final PropertiesFile propertiesFile = (PropertiesFile) file;
final List<IProperty> properties = propertiesFile.getProperties();
Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module == null)
return;
final GlobalSearchScope scope = CURRENT_FILE ? GlobalSearchScope.fileScope(file) : MODULE_WITH_DEPENDENCIES ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.projectScope(file.getProject());
final Map<String, Set<PsiFile>> processedValueToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
final Map<String, Set<PsiFile>> processedKeyToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
final ProgressIndicator progress = ProgressWrapper.wrap(original);
ProgressManager.getInstance().runProcess(() -> {
if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(properties, progress, false, property -> {
if (original != null) {
if (original.isCanceled())
return false;
original.setText2(PropertiesBundle.message("searching.for.property.key.progress.text", property.getUnescapedKey()));
}
processTextUsages(processedValueToFiles, property.getValue(), processedKeyToFiles, searchHelper, scope);
processTextUsages(processedKeyToFiles, property.getUnescapedKey(), processedValueToFiles, searchHelper, scope);
return true;
}))
throw new ProcessCanceledException();
List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
Map<String, Set<String>> keyToDifferentValues = new HashMap<>();
if (CHECK_DUPLICATE_KEYS || CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
prepareDuplicateKeysByFile(processedKeyToFiles, manager, keyToDifferentValues, problemDescriptors, file, original);
}
if (CHECK_DUPLICATE_VALUES)
prepareDuplicateValuesByFile(processedValueToFiles, manager, problemDescriptors, file, original);
if (CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
processDuplicateKeysWithDifferentValues(keyToDifferentValues, processedKeyToFiles, problemDescriptors, manager, file, original);
}
if (!problemDescriptors.isEmpty()) {
processor.addProblemElement(refManager.getReference(file), problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]));
}
}, progress);
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class TestLocationDataRule method collectRelativeLocations.
@NotNull
protected static List<Location> collectRelativeLocations(Project project, VirtualFile file) {
if (DumbService.isDumb(project))
return Collections.emptyList();
final List<Location> locations = new ArrayList<>();
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 && parent != 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, (o1, o2) -> 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<>();
searchHelper.processAllFilesWithWordInLiterals(word, testScope, new CommonProcessors.CollectProcessor<>(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;
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class SystemBuilder method build.
public ReductionSystem build(final Set<PsiElement> victims) {
final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(myManager.getProject());
ReductionSystem system = new ReductionSystem(myProject, victims, myTypes, myTypeVariableFactory, mySettings);
for (final PsiElement element : victims) {
if (element instanceof PsiParameter && ((PsiParameter) element).getDeclarationScope() instanceof PsiMethod) {
if (!verifyMethod(element, victims, helper)) {
continue;
}
} else if (element instanceof PsiMethod) {
if (!verifyMethod(element, victims, helper)) {
continue;
}
}
}
for (final PsiElement element : victims) {
PsiType definedType;
if (element instanceof PsiParameter && ((PsiParameter) element).getDeclarationScope() instanceof PsiMethod) {
final PsiParameter p = myParameters.get(element);
if (p != null) {
setType(element, definedType = defineType(p));
} else {
continue;
}
} else if (element instanceof PsiMethod) {
final PsiMethod m = myMethods.get(element);
if (m != null) {
system.addSubtypeConstraint(defineType(element), definedType = defineType(m));
} else {
continue;
}
} else {
definedType = defineType(element);
}
addBoundConstraints(system, definedType, element);
}
for (final PsiElement element : victims) {
if (element instanceof PsiParameter) {
final PsiElement scope = ((PsiParameter) element).getDeclarationScope();
if (scope instanceof PsiMethod) {
final PsiParameter p = myParameters.get(element);
if (p == null)
continue;
} else /*else if (scope instanceof PsiForeachStatement) {
addForEachConstraint(system, (PsiForeachStatement)scope);
}*/
if (element instanceof PsiMethod) {
final PsiMethod m = myMethods.get(element);
if (m == null)
continue;
}
} else if (element instanceof PsiMethod) {
final PsiMethod m = myMethods.get(element);
if (m == null)
continue;
}
addUsage(system, element);
if (!(element instanceof PsiExpression)) {
for (PsiReference ref : ReferencesSearch.search(element, getScope(helper, element), true)) {
final PsiElement elt = ref.getElement();
if (elt != null) {
addUsage(system, elt);
}
}
}
}
return system;
}
Aggregations