public static @NotNull List<SchemaNamespaceDecl> findNamespaces( @NotNull Project project, @Nullable String namePrefix, @NotNull GlobalSearchScope searchScope) { // TODO cache all namespaces (if prefix is null) SchemaNamespaceByNameIndex index = SchemaNamespaceByNameIndex.EP_NAME.findExtension(SchemaNamespaceByNameIndex.class); List<String> namespaceFqns = new SmartList<>(); index.processAllKeys( project, new FilteringProcessor<>( ns -> namePrefix == null || ns.startsWith(namePrefix), Processors.cancelableCollectProcessor(namespaceFqns) ) ); return namespaceFqns.stream() .flatMap(ns -> index.get(ns, project, searchScope).stream()) .collect(Collectors.toList()); }
private static void flashUsageScriptaculously(@Nonnull final Usage usage) { if (!(usage instanceof UsageInfo2UsageAdapter)) { return; } UsageInfo2UsageAdapter usageInfo = (UsageInfo2UsageAdapter)usage; Editor editor = usageInfo.openTextEditor(true); if (editor == null) return; TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.BLINKING_HIGHLIGHTS_ATTRIBUTES); RangeBlinker rangeBlinker = new RangeBlinker(editor, attributes, 6); List<Segment> segments = new ArrayList<>(); Processor<Segment> processor = Processors.cancelableCollectProcessor(segments); usageInfo.processRangeMarkers(processor); rangeBlinker.resetMarkers(segments); rangeBlinker.startBlinking(); }
public static <T extends PsiModifierListOwner> List<T> getSuperAnnotationOwners(@NotNull T element) { return CachedValuesManager.getCachedValue(element, () -> { Set<PsiModifierListOwner> result = ContainerUtil.newLinkedHashSet(); if(element instanceof PsiMethod) { collectSuperMethods(result, ((PsiMethod) element).getHierarchicalMethodSignature(), element, JavaPsiFacade.getInstance(element.getProject()).getResolveHelper()); } else if(element instanceof PsiClass) { //noinspection unchecked InheritanceUtil.processSupers((PsiClass) element, false, (Processor) Processors.cancelableCollectProcessor(result)); } else if(element instanceof PsiParameter) { collectSuperParameters(result, (PsiParameter) element); } //noinspection unchecked List<T> list = new ArrayList<>((Collection<? extends T>) result); return CachedValueProvider.Result.create(list, PsiModificationTracker.MODIFICATION_COUNT); }); }
@NotNull public Collection<String> getTypeNames(@NotNull GlobalSearchScope scope, @Nullable IdFilter filter) { Set<String> types = new THashSet<>(); collectTypeNames(Processors.cancelableCollectProcessor(types), scope, filter); return types; }
@NotNull public Collection<DotNetTypeDeclaration> getTypes(@NotNull String key, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) { Set<DotNetTypeDeclaration> types = new THashSet<>(); collectTypes(key, scope, filter, Processors.cancelableCollectProcessor(types)); return types; }
@Override @Nonnull public PsiElement[] findCommentsContainingIdentifier(@Nonnull String identifier, @Nonnull SearchScope searchScope) { final List<PsiElement> result = Collections.synchronizedList(new ArrayList<>()); Processor<PsiElement> processor = Processors.cancelableCollectProcessor(result); processCommentsContainingIdentifier(identifier, searchScope, processor); return PsiUtilCore.toPsiElementArray(result); }
private void getFilesWithText(@Nonnull GlobalSearchScope scope, final short searchContext, final boolean caseSensitively, @Nonnull String text, @Nonnull Collection<VirtualFile> result) { myManager.startBatchFilesProcessingMode(); try { Processor<VirtualFile> processor = Processors.cancelableCollectProcessor(result); boolean success = processFilesWithText(scope, searchContext, caseSensitively, text, processor); // success == false means exception in index } finally { myManager.finishBatchFilesProcessingMode(); } }
private void collectFiles(@Nonnull MultiMap<Set<IdIndexEntry>, RequestWithProcessor> singles, @Nonnull ProgressIndicator progress, @Nonnull final MultiMap<VirtualFile, RequestWithProcessor> intersectionResult, @Nonnull final MultiMap<VirtualFile, RequestWithProcessor> restResult) { for (Map.Entry<Set<IdIndexEntry>, Collection<RequestWithProcessor>> entry : singles.entrySet()) { final Set<IdIndexEntry> keys = entry.getKey(); if (keys.isEmpty()) { continue; } final Collection<RequestWithProcessor> processors = entry.getValue(); final GlobalSearchScope commonScope = uniteScopes(processors); final Set<VirtualFile> intersectionWithContainerNameFiles = intersectionWithContainerNameFiles(commonScope, processors, keys); List<VirtualFile> result = new ArrayList<>(); Processor<VirtualFile> processor = Processors.cancelableCollectProcessor(result); processFilesContainingAllKeys(myManager.getProject(), commonScope, null, keys, processor); for (final VirtualFile file : result) { progress.checkCanceled(); for (final IdIndexEntry indexEntry : keys) { myDumbService.runReadActionInSmartMode( () -> FileBasedIndex.getInstance().processValues(IdIndex.NAME, indexEntry, file, (file1, value) -> { int mask = value.intValue(); for (RequestWithProcessor single : processors) { final PsiSearchRequest request = single.request; if ((mask & request.searchContext) != 0 && request.searchScope.contains(file1)) { MultiMap<VirtualFile, RequestWithProcessor> result1 = intersectionWithContainerNameFiles == null || !intersectionWithContainerNameFiles.contains(file1) ? restResult : intersectionResult; result1.putValue(file1, single); } } return true; }, commonScope)); } } } }
/** * @return null means we did not find common container files */ @Nullable private Set<VirtualFile> intersectionWithContainerNameFiles(@Nonnull GlobalSearchScope commonScope, @Nonnull Collection<RequestWithProcessor> data, @Nonnull Set<IdIndexEntry> keys) { String commonName = null; short searchContext = 0; boolean caseSensitive = true; for (RequestWithProcessor r : data) { String containerName = r.request.containerName; if (containerName != null) { if (commonName == null) { commonName = containerName; searchContext = r.request.searchContext; caseSensitive = r.request.caseSensitive; } else if (commonName.equals(containerName)) { searchContext |= r.request.searchContext; caseSensitive &= r.request.caseSensitive; } else { return null; } } } if (commonName == null) return null; List<IdIndexEntry> entries = getWordEntries(commonName, caseSensitive); if (entries.isEmpty()) return null; entries.addAll(keys); // should find words from both text and container names final short finalSearchContext = searchContext; Condition<Integer> contextMatches = context -> (context.intValue() & finalSearchContext) != 0; Set<VirtualFile> containerFiles = new THashSet<>(); Processor<VirtualFile> processor = Processors.cancelableCollectProcessor(containerFiles); processFilesContainingAllKeys(myManager.getProject(), commonScope, contextMatches, entries, processor); return containerFiles; }
public static <Key, Psi extends PsiElement> Collection<Psi> getElements(@Nonnull StubIndexKey<Key, Psi> indexKey, @Nonnull Key key, @Nonnull final Project project, @javax.annotation.Nullable final GlobalSearchScope scope, @Nullable IdFilter idFilter, @Nonnull Class<Psi> requiredClass) { final List<Psi> result = new SmartList<>(); Processor<Psi> processor = Processors.cancelableCollectProcessor(result); getInstance().processElements(indexKey, key, project, scope, idFilter, requiredClass, processor); return result; }
@Override public <Key, Psi extends PsiElement> Collection<Psi> get(@Nonnull StubIndexKey<Key, Psi> indexKey, @Nonnull Key key, @Nonnull Project project, @Nullable GlobalSearchScope scope, IdFilter filter) { final List<Psi> result = new SmartList<>(); process(indexKey, key, project, scope, filter, Processors.cancelableCollectProcessor(result)); return result; }
@Override @Nonnull public <K> Collection<K> getAllKeys(@Nonnull StubIndexKey<K, ?> indexKey, @Nonnull Project project) { Set<K> allKeys = ContainerUtil.newTroveSet(); processAllKeys(indexKey, project, Processors.cancelableCollectProcessor(allKeys)); return allKeys; }
@RequiredReadAction public List<CSharpResolveContext> collectResolveContexts() { String qName = myNamespaceAsElement.getPresentableQName(); if(qName == null) { return Collections.emptyList(); } Project project = myNamespaceAsElement.getProject(); String indexableName = DotNetNamespaceStubUtil.getIndexableNamespace(qName); Set<DotNetTypeDeclaration> result = new THashSet<>(); StubIndex.getInstance().processElements(CSharpIndexKeys.TYPE_WITH_EXTENSION_METHODS_INDEX, indexableName, project, myResolveScope, DotNetTypeDeclaration.class, Processors .cancelableCollectProcessor(result)); Set<String> processed = new THashSet<>(); List<CSharpResolveContext> list = new SmartList<>(); for(DotNetTypeDeclaration typeDeclaration : result) { ProgressManager.checkCanceled(); PsiElement wrappedDeclaration = ToNativeElementTransformers.transform(typeDeclaration); if(typeDeclaration instanceof CSharpTypeDeclaration && typeDeclaration.hasModifier(CSharpModifier.PARTIAL)) { String vmQName = typeDeclaration.getVmQName(); if(processed.contains(vmQName)) { continue; } processed.add(vmQName); } CSharpResolveContext context = CSharpResolveContextUtil.createContext(DotNetGenericExtractor.EMPTY, myResolveScope, wrappedDeclaration); list.add(context); } return list.isEmpty() ? Collections.emptyList() : list; }
public static void getActionsToShow(@Nonnull final Editor hostEditor, @Nonnull final PsiFile hostFile, @Nonnull final IntentionsInfo intentions, int passIdToShowIntentionsFor) { final PsiElement psiElement = hostFile.findElementAt(hostEditor.getCaretModel().getOffset()); LOG.assertTrue(psiElement == null || psiElement.isValid(), psiElement); int offset = hostEditor.getCaretModel().getOffset(); final Project project = hostFile.getProject(); List<HighlightInfo.IntentionActionDescriptor> fixes = getAvailableFixes(hostEditor, hostFile, passIdToShowIntentionsFor); final DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project); final Document hostDocument = hostEditor.getDocument(); HighlightInfo infoAtCursor = ((DaemonCodeAnalyzerImpl)codeAnalyzer).findHighlightByOffset(hostDocument, offset, true); if (infoAtCursor == null) { intentions.errorFixesToShow.addAll(fixes); } else { final boolean isError = infoAtCursor.getSeverity() == HighlightSeverity.ERROR; for (HighlightInfo.IntentionActionDescriptor fix : fixes) { if (fix.isError() && isError) { intentions.errorFixesToShow.add(fix); } else { intentions.inspectionFixesToShow.add(fix); } } } for (final IntentionAction action : IntentionManager.getInstance().getAvailableIntentionActions()) { Pair<PsiFile, Editor> place = ShowIntentionActionsHandler .chooseBetweenHostAndInjected(hostFile, hostEditor, (psiFile, editor) -> ShowIntentionActionsHandler.availableFor(psiFile, editor, action)); if (place != null) { List<IntentionAction> enableDisableIntentionAction = new ArrayList<>(); enableDisableIntentionAction.add(new IntentionHintComponent.EnableDisableIntentionAction(action)); enableDisableIntentionAction.add(new IntentionHintComponent.EditIntentionSettingsAction(action)); HighlightInfo.IntentionActionDescriptor descriptor = new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null); if (!fixes.contains(descriptor)) { intentions.intentionsToShow.add(descriptor); } } } if (HighlightingLevelManager.getInstance(project).shouldInspect(hostFile)) { PsiElement intentionElement = psiElement; int intentionOffset = offset; if (psiElement instanceof PsiWhiteSpace && offset == psiElement.getTextRange().getStartOffset() && offset > 0) { final PsiElement prev = hostFile.findElementAt(offset - 1); if (prev != null && prev.isValid()) { intentionElement = prev; intentionOffset = offset - 1; } } if (intentionElement != null && intentionElement.getManager().isInProject(intentionElement)) { collectIntentionsFromDoNotShowLeveledInspections(project, hostFile, intentionElement, intentionOffset, intentions); } } final int line = hostDocument.getLineNumber(offset); MarkupModelEx model = (MarkupModelEx)DocumentMarkupModel.forDocument(hostDocument, project, true); List<RangeHighlighterEx> result = new ArrayList<>(); Processor<RangeHighlighterEx> processor = Processors.cancelableCollectProcessor(result); model.processRangeHighlightersOverlappingWith(hostDocument.getLineStartOffset(line), hostDocument.getLineEndOffset(line), processor); GutterIntentionAction.addActions(hostEditor, intentions, project, result); boolean cleanup = appendCleanupCode(intentions.inspectionFixesToShow, hostFile); if (!cleanup) { appendCleanupCode(intentions.errorFixesToShow, hostFile); } EditorNotificationActions.collectDescriptorsForEditor(hostEditor, intentions.notificationActionsToShow); intentions.filterActions(hostFile); }
@Nonnull private Set<PsiFile> getInjectedPsiFiles(@Nonnull final List<PsiElement> elements1, @Nonnull final List<PsiElement> elements2, @Nonnull final ProgressIndicator progress) { ApplicationManager.getApplication().assertReadAccessAllowed(); List<DocumentWindow> injected = InjectedLanguageManager.getInstance(myProject).getCachedInjectedDocumentsInRange(myFile, myFile.getTextRange()); final Collection<PsiElement> hosts = new THashSet<>(elements1.size() + elements2.size() + injected.size()); //rehighlight all injected PSI regardless the range, //since change in one place can lead to invalidation of injected PSI in (completely) other place. for (DocumentWindow documentRange : injected) { ProgressManager.checkCanceled(); if (!documentRange.isValid()) continue; PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(documentRange); if (file == null) continue; PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file); if (context != null && context.isValid() && !file.getProject().isDisposed() && (myUpdateAll || myRestrictRange.intersects(context.getTextRange()))) { hosts.add(context); } } InjectedLanguageManagerImpl injectedLanguageManager = InjectedLanguageManagerImpl.getInstanceImpl(myProject); Processor<PsiElement> collectInjectableProcessor = Processors.cancelableCollectProcessor(hosts); injectedLanguageManager.processInjectableElements(elements1, collectInjectableProcessor); injectedLanguageManager.processInjectableElements(elements2, collectInjectableProcessor); final Set<PsiFile> outInjected = new THashSet<>(); final PsiLanguageInjectionHost.InjectedPsiVisitor visitor = (injectedPsi, places) -> { synchronized (outInjected) { outInjected.add(injectedPsi); } }; if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(hosts), progress, true, element -> { ApplicationManager.getApplication().assertReadAccessAllowed(); ProgressManager.checkCanceled(); InjectedLanguageManager.getInstance(myFile.getProject()).enumerateEx(element, myFile, false, visitor); return true; })) { throw new ProcessCanceledException(); } synchronized (outInjected) { return outInjected; } }
private static Collection<DotNetTypeDeclaration> getTypesWithGeneric(CSharpReferenceExpression ref, final String refName) { List<DotNetTypeDeclaration> collection = new ArrayList<>(); GlobalSearchScopeFilter filter = new GlobalSearchScopeFilter(ref.getResolveScope()); DotNetShortNameSearcher.getInstance(ref.getProject()).collectTypes(refName, ref.getResolveScope(), filter, Processors.cancelableCollectProcessor(collection)); return collection; }