use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class GroovyStaticImportMethodFix method getMethodsToImport.
@NotNull
private List<PsiMethod> getMethodsToImport() {
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(myMethodCall.getProject());
GrMethodCall element = myMethodCall.getElement();
LOG.assertTrue(element != null);
GrReferenceExpression reference = getMethodExpression(element);
LOG.assertTrue(reference != null);
GrArgumentList argumentList = element.getArgumentList();
String name = reference.getReferenceName();
ArrayList<PsiMethod> list = new ArrayList<>();
if (name == null)
return list;
GlobalSearchScope scope = element.getResolveScope();
PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
List<PsiMethod> applicableList = new ArrayList<>();
for (PsiMethod method : methods) {
ProgressManager.checkCanceled();
if (JavaCompletionUtil.isInExcludedPackage(method, false))
continue;
if (!method.hasModifierProperty(PsiModifier.STATIC))
continue;
PsiFile file = method.getContainingFile();
if (file instanceof PsiClassOwner && //do not show methods from default package
!((PsiClassOwner) file).getPackageName().isEmpty() && PsiUtil.isAccessible(element, method)) {
list.add(method);
if (PsiUtil.isApplicable(PsiUtil.getArgumentTypes(element, true), method, PsiSubstitutor.EMPTY, element, false)) {
applicableList.add(method);
}
}
}
List<PsiMethod> result = applicableList.isEmpty() ? list : applicableList;
Collections.sort(result, new PsiProximityComparator(argumentList));
return result;
}
use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class DefaultGroovyShellRunner method hasGroovyWithNeededJars.
public static boolean hasGroovyWithNeededJars(Module module) {
GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
JavaPsiFacade facade = JavaPsiFacade.getInstance(module.getProject());
for (String className : REQUIRED_GROOVY_CLASSES) {
if (facade.findClass(className, scope) == null) {
return false;
}
}
return true;
}
use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class SearchScope method iterateContent.
public void iterateContent(@NotNull final Project project, final Processor<VirtualFile> processor) {
switch(getScopeType()) {
case PROJECT:
//noinspection unchecked
ProjectRootManager.getInstance(project).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
break;
case MODULE:
final Module module = ModuleManager.getInstance(project).findModuleByName(getModuleName());
assert module != null;
ModuleRootManager.getInstance(module).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
break;
case DIRECTORY:
final String dirName = getPath();
assert dirName != null;
final VirtualFile virtualFile = findFile(dirName);
if (virtualFile != null) {
iterateRecursively(virtualFile, processor, isRecursive());
}
break;
case CUSTOM:
assert myCustomScope != null;
final ContentIterator iterator;
if (myCustomScope instanceof GlobalSearchScope) {
final GlobalSearchScope searchScope = (GlobalSearchScope) myCustomScope;
iterator = new MyFileIterator(processor, virtualFile13 -> searchScope.contains(virtualFile13));
if (searchScope.isSearchInLibraries()) {
final OrderEnumerator enumerator = OrderEnumerator.orderEntries(project).withoutModuleSourceEntries().withoutDepModules();
final Collection<VirtualFile> libraryFiles = new THashSet<>();
Collections.addAll(libraryFiles, enumerator.getClassesRoots());
Collections.addAll(libraryFiles, enumerator.getSourceRoots());
final Processor<VirtualFile> adapter = virtualFile1 -> iterator.processFile(virtualFile1);
for (final VirtualFile file : libraryFiles) {
iterateRecursively(file, adapter, true);
}
}
} else {
final PsiManager manager = PsiManager.getInstance(project);
iterator = new MyFileIterator(processor, virtualFile12 -> {
final PsiFile element = manager.findFile(virtualFile12);
return element != null && PsiSearchScopeUtil.isInScope(myCustomScope, element);
});
}
ProjectRootManager.getInstance(project).getFileIndex().iterateContent(iterator);
}
}
use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class PythonImportUtils method addSymbolImportCandidates.
private static void addSymbolImportCandidates(PyElement node, String refText, @Nullable String asName, AutoImportQuickFix fix, Set<String> seenCandidateNames, PsiFile existingImportFile) {
Project project = node.getProject();
List<PsiElement> symbols = new ArrayList<>();
symbols.addAll(PyClassNameIndex.find(refText, project, true));
GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(node);
if (!isQualifier(node)) {
symbols.addAll(PyFunctionNameIndex.find(refText, project, scope));
}
symbols.addAll(PyVariableNameIndex.find(refText, project, scope));
if (isPossibleModuleReference(node)) {
symbols.addAll(findImportableModules(node.getContainingFile(), refText, project, scope));
}
if (!symbols.isEmpty()) {
for (PsiElement symbol : symbols) {
if (isIndexableTopLevel(symbol)) {
// we only want top-level symbols
PsiFileSystemItem srcfile = symbol instanceof PsiFileSystemItem ? ((PsiFileSystemItem) symbol).getParent() : symbol.getContainingFile();
if (srcfile != null && isAcceptableForImport(node, existingImportFile, srcfile)) {
QualifiedName importPath = QualifiedNameFinder.findCanonicalImportPath(symbol, node);
if (importPath == null) {
continue;
}
if (symbol instanceof PsiFileSystemItem) {
importPath = importPath.removeTail(1);
}
final String symbolImportQName = importPath.append(refText).toString();
if (!seenCandidateNames.contains(symbolImportQName)) {
// a new, valid hit
fix.addImport(symbol, srcfile, importPath, asName);
seenCandidateNames.add(symbolImportQName);
}
}
}
}
}
}
use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class DefaultXmlTagNameProvider method getRootTagsVariants.
private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
int offset = context.getEditor().getCaretModel().getOffset();
context.getEditor().getCaretModel().moveToOffset(offset - 4);
AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
}
}));
final FileBasedIndex fbi = FileBasedIndex.getInstance();
Collection<String> result = new ArrayList<>();
Processor<String> processor = Processors.cancelableCollectProcessor(result);
fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
final GlobalSearchScope scope = new EverythingGlobalScope();
for (final String ns : result) {
if (ns.startsWith("file://"))
continue;
fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {
@Override
public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
List<String> tags = value.getRootTags();
for (String s : tags) {
elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = context.getDocument();
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
XmlFile psiFile = (XmlFile) context.getFile();
boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
XmlTag rootTag = psiFile.getRootTag();
if (incomplete) {
XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
// remove tag end added by smart attribute adder :(
if (token != null)
token.delete();
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
super.handleInsert(context, item);
}
}
}));
}
return true;
}
}, scope);
}
return elements;
}
Aggregations