use of com.intellij.psi.search.DelegatingGlobalSearchScope in project intellij-community by JetBrains.
the class GroovyScriptRunConfiguration method getSearchScope.
@Override
public GlobalSearchScope getSearchScope() {
GlobalSearchScope superScope = super.getSearchScope();
String path = getScriptPath();
if (path == null)
return superScope;
VirtualFile scriptFile = LocalFileSystem.getInstance().findFileByPath(path);
if (scriptFile == null)
return superScope;
GlobalSearchScope fileScope = GlobalSearchScope.fileScope(getProject(), scriptFile);
if (superScope == null)
return fileScope;
return new DelegatingGlobalSearchScope(fileScope.union(superScope)) {
@Override
public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
if (file1.equals(scriptFile))
return 1;
if (file2.equals(scriptFile))
return -1;
return super.compare(file1, file2);
}
};
}
use of com.intellij.psi.search.DelegatingGlobalSearchScope in project intellij-community by JetBrains.
the class CodeFragmentFactoryContextWrapper method prepareResolveScope.
private static JavaCodeFragment prepareResolveScope(JavaCodeFragment codeFragment) {
GlobalSearchScope originalResolveScope = codeFragment.getResolveScope();
codeFragment.forceResolveScope(new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(codeFragment.getProject())) {
final Comparator<VirtualFile> myScopeComparator = Comparator.comparing(originalResolveScope::contains).thenComparing(super::compare);
@Override
public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
// prefer files from the original resolve scope
return myScopeComparator.compare(file1, file2);
}
});
return codeFragment;
}
use of com.intellij.psi.search.DelegatingGlobalSearchScope in project intellij-community by JetBrains.
the class LibraryScopeCache method calcLibraryScope.
@NotNull
private GlobalSearchScope calcLibraryScope(@NotNull List<OrderEntry> orderEntries) {
List<Module> modulesLibraryUsedIn = new ArrayList<>();
LibraryOrderEntry lib = null;
for (OrderEntry entry : orderEntries) {
if (entry instanceof JdkOrderEntry) {
return getScopeForSdk((JdkOrderEntry) entry);
}
if (entry instanceof LibraryOrderEntry) {
lib = (LibraryOrderEntry) entry;
modulesLibraryUsedIn.add(entry.getOwnerModule());
} else if (entry instanceof ModuleOrderEntry) {
modulesLibraryUsedIn.add(entry.getOwnerModule());
}
}
Comparator<Module> comparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
Collections.sort(modulesLibraryUsedIn, comparator);
List<Module> uniquesList = ContainerUtil.removeDuplicatesFromSorted(modulesLibraryUsedIn, comparator);
GlobalSearchScope allCandidates = uniquesList.isEmpty() ? myLibrariesOnlyScope : getScopeForLibraryUsedIn(uniquesList);
if (lib != null) {
final LibraryRuntimeClasspathScope preferred = new LibraryRuntimeClasspathScope(myProject, lib);
// prefer current library
return new DelegatingGlobalSearchScope(allCandidates, preferred) {
@Override
public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
boolean c1 = preferred.contains(file1);
boolean c2 = preferred.contains(file2);
if (c1 && !c2)
return 1;
if (c2 && !c1)
return -1;
return super.compare(file1, file2);
}
};
}
return allCandidates;
}
use of com.intellij.psi.search.DelegatingGlobalSearchScope in project intellij-community by JetBrains.
the class UnusedDeclarationInspectionBase method runInspection.
@Override
public void runInspection(@NotNull final AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
globalContext.getRefManager().iterate(new RefJavaVisitor() {
@Override
public void visitElement(@NotNull final RefEntity refEntity) {
if (refEntity instanceof RefElementImpl) {
final RefElementImpl refElement = (RefElementImpl) refEntity;
if (!refElement.isSuspicious())
return;
PsiFile file = refElement.getContainingFile();
if (file == null)
return;
final boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
if (isSuppressed || !((GlobalInspectionContextBase) globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this)) {
if (isSuppressed || !scope.contains(file)) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
}
}
}
}
});
if (isAddNonJavaUsedEnabled()) {
checkForReachableRefs(globalContext);
final StrictUnreferencedFilter strictUnreferencedFilter = new StrictUnreferencedFilter(this, globalContext);
ProgressManager.getInstance().runProcess(new Runnable() {
@Override
public void run() {
final RefManager refManager = globalContext.getRefManager();
final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(refManager.getProject());
refManager.iterate(new RefJavaVisitor() {
@Override
public void visitElement(@NotNull final RefEntity refEntity) {
if (refEntity instanceof RefClass && strictUnreferencedFilter.accepts((RefClass) refEntity)) {
findExternalClassReferences((RefClass) refEntity);
} else if (refEntity instanceof RefMethod) {
RefMethod refMethod = (RefMethod) refEntity;
if (refMethod.isConstructor() && strictUnreferencedFilter.accepts(refMethod)) {
findExternalClassReferences(refMethod.getOwnerClass());
}
}
}
private void findExternalClassReferences(final RefClass refElement) {
final PsiClass psiClass = refElement.getElement();
String qualifiedName = psiClass != null ? psiClass.getQualifiedName() : null;
if (qualifiedName != null) {
final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(globalContext.getProject());
final PsiNonJavaFileReferenceProcessor processor = (file, startOffset, endOffset) -> {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
return false;
};
final DelegatingGlobalSearchScope globalSearchScope = new DelegatingGlobalSearchScope(projectScope) {
@Override
public boolean contains(@NotNull VirtualFile file) {
return file.getFileType() != JavaFileType.INSTANCE && super.contains(file);
}
};
if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) {
final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst();
if (reference != null) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
for (PsiMethod method : psiClass.getMethods()) {
final RefElement refMethod = refManager.getReference(method);
if (refMethod != null) {
getEntryPointsManager(globalContext).addEntryPoint(refMethod, false);
}
}
}
}
}
}
});
}
}, null);
}
myProcessedSuspicious = new HashSet<>();
myPhase = 1;
}
use of com.intellij.psi.search.DelegatingGlobalSearchScope in project intellij-community by JetBrains.
the class UsedIconsListingAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
final Project project = LangDataKeys.PROJECT.getData(e.getDataContext());
final MultiMap<String, PsiExpression> calls = new MultiMap<>();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
Processor<PsiReference> consumer = new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
PsiCallExpression call = PsiTreeUtil.getParentOfType(reference.getElement(), PsiCallExpression.class, false);
if (call == null)
return true;
if (call.getArgumentList() == null)
return true;
if (call.getArgumentList().getExpressions() == null)
return true;
PsiFile file = reference.getElement().getContainingFile();
if ("AllIcons.java".equals(file.getName()))
return true;
PsiClass container = PsiUtil.getTopLevelClass(reference.getElement());
if (container != null && container.getQualifiedName().startsWith("icons."))
return true;
for (PsiExpression arg : call.getArgumentList().getExpressions()) {
if (arg instanceof PsiLiteralExpression) {
Object value = ((PsiLiteralExpression) arg).getValue();
processValue(value, call, file);
} else {
Object value = psiFacade.getConstantEvaluationHelper().computeConstantExpression(arg, false);
processValue(value, call, file);
}
}
return true;
}
private void processValue(Object value, PsiCallExpression call, PsiFile file) {
if (value instanceof String) {
String str = StringUtil.unquoteString((String) value, '\"');
if (!str.startsWith("/")) {
if (file instanceof PsiClassOwner) {
str = "/" + ((PsiClassOwner) file).getPackageName().replace('.', '/') + "/" + str;
}
}
calls.putValue(str, call);
}
}
};
GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
PsiClass iconLoader = psiFacade.findClass("com.intellij.openapi.util.IconLoader", allScope);
PsiMethod getIconMethod = iconLoader.findMethodsByName("getIcon", false)[0];
PsiMethod findIconMethod = iconLoader.findMethodsByName("findIcon", false)[0];
if (true) {
MethodReferencesSearch.search(getIconMethod, false).forEach(consumer);
MethodReferencesSearch.search(findIconMethod, false).forEach(consumer);
}
final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
if (true) {
PsiClass javaeeIcons = psiFacade.findClass("com.intellij.javaee.oss.JavaeeIcons", allScope);
MethodReferencesSearch.search(javaeeIcons.findMethodsByName("getIcon", false)[0], false).forEach(consumer);
MethodReferencesSearch.search(findIconMethod, false).forEach(consumer);
}
final List<XmlAttribute> xmlAttributes = new ArrayList<>();
PsiSearchHelper.SERVICE.getInstance(project).processAllFilesWithWordInText("icon", new DelegatingGlobalSearchScope(GlobalSearchScope.projectScope(project)) {
@Override
public boolean contains(@NotNull VirtualFile file) {
return super.contains(file) && file.getFileType() == XmlFileType.INSTANCE && index.isInSource(file);
}
}, new Processor<PsiFile>() {
@Override
public boolean process(PsiFile file) {
file.accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
super.visitXmlTag(tag);
String icon = tag.getAttributeValue("icon");
if (icon != null) {
xmlAttributes.add(tag.getAttribute("icon"));
}
}
});
return true;
}
}, true);
PsiClass presentation = psiFacade.findClass("com.intellij.ide.presentation.Presentation", allScope);
final MultiMap<String, PsiAnnotation> annotations = new MultiMap<>();
AnnotationTargetsSearch.search(presentation).forEach(owner -> {
PsiAnnotation annotation = owner.getModifierList().findAnnotation("com.intellij.ide.presentation.Presentation");
PsiAnnotationMemberValue icon = annotation.findAttributeValue("icon");
if (icon instanceof PsiLiteralExpression) {
Object value = ((PsiLiteralExpression) icon).getValue();
if (value instanceof String) {
annotations.putValue((String) value, annotation);
}
}
return true;
});
doReplacements(project, calls, xmlAttributes, annotations, psiFacade.findClass("com.intellij.icons.AllIcons", allScope));
for (PsiClass iconClass : psiFacade.findPackage("icons").getClasses(allScope)) {
if (iconClass.getName().endsWith("Icons")) {
doReplacements(project, calls, xmlAttributes, annotations, iconClass);
}
}
}
Aggregations