use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class FindInProjectTask method searchInFiles.
private void searchInFiles(@NotNull Collection<VirtualFile> virtualFiles, @NotNull FindUsagesProcessPresentation processPresentation, @NotNull final Processor<UsageInfo> consumer) {
AtomicInteger occurrenceCount = new AtomicInteger();
AtomicInteger processedFileCount = new AtomicInteger();
Processor<VirtualFile> processor = virtualFile -> {
if (!virtualFile.isValid())
return true;
long fileLength = UsageViewManagerImpl.getFileLength(virtualFile);
if (fileLength == -1)
return true;
final boolean skipProjectFile = ProjectUtil.isProjectOrWorkspaceFile(virtualFile) && !myFindModel.isSearchInProjectFiles();
if (skipProjectFile && !Registry.is("find.search.in.project.files"))
return true;
if (fileLength > SINGLE_FILE_SIZE_LIMIT) {
myLargeFiles.add(virtualFile);
return true;
}
myProgress.checkCanceled();
if (myProgress.isRunning()) {
double fraction = (double) processedFileCount.incrementAndGet() / virtualFiles.size();
myProgress.setFraction(fraction);
}
String text = FindBundle.message("find.searching.for.string.in.file.progress", myFindModel.getStringToFind(), virtualFile.getPresentableUrl());
myProgress.setText(text);
myProgress.setText2(FindBundle.message("find.searching.for.string.in.file.occurrences.progress", occurrenceCount));
Pair.NonNull<PsiFile, VirtualFile> pair = ReadAction.compute(() -> findFile(virtualFile));
if (pair == null)
return true;
PsiFile psiFile = pair.first;
VirtualFile sourceVirtualFile = pair.second;
int countInFile = FindInProjectUtil.processUsagesInFile(psiFile, sourceVirtualFile, myFindModel, info -> skipProjectFile || consumer.process(info));
if (countInFile > 0 && skipProjectFile) {
processPresentation.projectFileUsagesFound(() -> {
FindModel model = myFindModel.clone();
model.setSearchInProjectFiles(true);
FindInProjectManager.getInstance(myProject).startFindInProject(model);
});
return true;
}
occurrenceCount.addAndGet(countInFile);
if (countInFile > 0) {
if (myTotalFilesSize.addAndGet(fileLength) > FILES_SIZE_LIMIT && myWarningShown.compareAndSet(false, true)) {
String message = FindBundle.message("find.excessive.total.size.prompt", UsageViewManagerImpl.presentableSize(myTotalFilesSize.longValue()), ApplicationNamesInfo.getInstance().getProductName());
UsageLimitUtil.showAndCancelIfAborted(myProject, message, processPresentation.getUsageViewPresentation());
}
}
return true;
};
PsiSearchHelperImpl.processFilesConcurrentlyDespiteWriteActions(myProject, new ArrayList<>(virtualFiles), myProgress, processor);
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class FindUsagesManager method createUsageSearcher.
/**
* @throws PsiInvalidElementAccessException when the searcher can't be created (i.e. because element was invalidated)
*/
@NotNull
private static UsageSearcher createUsageSearcher(@NotNull final PsiElement2UsageTargetAdapter[] primaryTargets, @NotNull final PsiElement2UsageTargetAdapter[] secondaryTargets, @NotNull final FindUsagesHandler handler, @NotNull FindUsagesOptions options, final PsiFile scopeFile) throws PsiInvalidElementAccessException {
ReadAction.run(() -> {
PsiElement[] primaryElements = PsiElement2UsageTargetAdapter.convertToPsiElements(primaryTargets);
PsiElement[] secondaryElements = PsiElement2UsageTargetAdapter.convertToPsiElements(secondaryTargets);
ContainerUtil.concat(primaryElements, secondaryElements).forEach(psi -> {
if (psi == null || !psi.isValid())
throw new PsiInvalidElementAccessException(psi);
});
});
FindUsagesOptions optionsClone = options.clone();
return processor -> {
PsiElement[] primaryElements = ReadAction.compute(() -> PsiElement2UsageTargetAdapter.convertToPsiElements(primaryTargets));
PsiElement[] secondaryElements = ReadAction.compute(() -> PsiElement2UsageTargetAdapter.convertToPsiElements(secondaryTargets));
Project project = ReadAction.compute(() -> scopeFile != null ? scopeFile.getProject() : primaryElements[0].getProject());
dropResolveCacheRegularly(ProgressManager.getInstance().getProgressIndicator(), project);
if (scopeFile != null) {
optionsClone.searchScope = new LocalSearchScope(scopeFile);
}
final Processor<UsageInfo> usageInfoProcessor = new CommonProcessors.UniqueProcessor<>(usageInfo -> {
Usage usage = ReadAction.compute(() -> UsageInfoToUsageConverter.convert(primaryElements, usageInfo));
return processor.process(usage);
});
final Iterable<PsiElement> elements = ContainerUtil.concat(primaryElements, secondaryElements);
optionsClone.fastTrack = new SearchRequestCollector(new SearchSession());
if (optionsClone.searchScope instanceof GlobalSearchScope) {
optionsClone.searchScope = optionsClone.searchScope.union(GlobalSearchScope.projectScope(project));
}
try {
for (final PsiElement element : elements) {
handler.processElementUsages(element, usageInfoProcessor, optionsClone);
for (CustomUsageSearcher searcher : Extensions.getExtensions(CustomUsageSearcher.EP_NAME)) {
try {
searcher.processElementUsages(element, processor, optionsClone);
} catch (IndexNotReadyException e) {
DumbService.getInstance(element.getProject()).showDumbModeNotification("Find usages is not available during indexing");
} catch (ProcessCanceledException e) {
throw e;
} catch (Exception e) {
LOG.error(e);
}
}
}
PsiSearchHelper.SERVICE.getInstance(project).processRequests(optionsClone.fastTrack, ref -> {
UsageInfo info = ReadAction.compute(() -> {
if (!ref.getElement().isValid())
return null;
return new UsageInfo(ref);
});
return info == null || usageInfoProcessor.process(info);
});
} finally {
optionsClone.fastTrack = null;
}
};
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class FindInProjectManager method startFindInProject.
public void startFindInProject(@NotNull FindModel findModel) {
if (findModel.getDirectoryName() != null && FindInProjectUtil.getDirectory(findModel) == null) {
return;
}
com.intellij.usages.UsageViewManager manager = com.intellij.usages.UsageViewManager.getInstance(myProject);
if (manager == null)
return;
final FindManager findManager = FindManager.getInstance(myProject);
findManager.getFindInProjectModel().copyFrom(findModel);
final FindModel findModelCopy = findModel.clone();
final UsageViewPresentation presentation = FindInProjectUtil.setupViewPresentation(FindSettings.getInstance().isShowResultsInSeparateView(), findModelCopy);
final boolean showPanelIfOnlyOneUsage = !FindSettings.getInstance().isSkipResultsWithOneUsage();
final FindUsagesProcessPresentation processPresentation = FindInProjectUtil.setupProcessPresentation(myProject, showPanelIfOnlyOneUsage, presentation);
ConfigurableUsageTarget usageTarget = new FindInProjectUtil.StringUsageTarget(myProject, findModel);
((FindManagerImpl) FindManager.getInstance(myProject)).getFindUsagesManager().addToHistory(usageTarget);
manager.searchAndShowUsages(new UsageTarget[] { usageTarget }, () -> processor -> {
myIsFindInProgress = true;
try {
Processor<UsageInfo> consumer = info -> {
Usage usage = UsageInfo2UsageAdapter.CONVERTER.fun(info);
usage.getPresentation().getIcon();
return processor.process(usage);
};
FindInProjectUtil.findUsages(findModelCopy, myProject, consumer, processPresentation);
} finally {
myIsFindInProgress = false;
}
}, processPresentation, presentation, null);
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class RefResolveServiceImpl method processBatch.
private void processBatch(@NotNull final ProgressIndicator indicator, @NotNull Set<VirtualFile> files) {
assert !myApplication.isDispatchThread();
final int resolvedInPreviousBatch = this.resolvedInPreviousBatch;
final int totalSize = files.size() + resolvedInPreviousBatch;
final ConcurrentIntObjectMap<int[]> fileToForwardIds = ContainerUtil.createConcurrentIntObjectMap();
final Set<VirtualFile> toProcess = Collections.synchronizedSet(files);
indicator.setIndeterminate(false);
ProgressIndicatorUtils.forceWriteActionPriority(indicator, (Disposable) indicator);
long start = System.currentTimeMillis();
Processor<VirtualFile> processor = file -> {
double fraction = 1 - toProcess.size() * 1.0 / totalSize;
indicator.setFraction(fraction);
try {
if (!file.isDirectory() && toResolve(file, myProject)) {
int fileId = getAbsId(file);
int i = totalSize - toProcess.size();
indicator.setText(i + "/" + totalSize + ": Resolving " + file.getPresentableUrl());
int[] forwardIds = processFile(file, fileId, indicator);
if (forwardIds == null) {
return false;
}
fileToForwardIds.put(fileId, forwardIds);
}
toProcess.remove(file);
return true;
} catch (RuntimeException e) {
indicator.checkCanceled();
}
return true;
};
boolean success = true;
try {
success = processFilesConcurrently(files, indicator, processor);
} finally {
this.resolvedInPreviousBatch = toProcess.isEmpty() ? 0 : totalSize - toProcess.size();
queue(toProcess, "re-added after fail. success=" + success);
storeIds(fileToForwardIds);
long end = System.currentTimeMillis();
log("Resolved batch of " + (totalSize - toProcess.size()) + " from " + totalSize + " files in " + ((end - start) / 1000) + "sec. (Gap: " + storage.gap + ")");
synchronized (filesToResolve) {
upToDate = filesToResolve.isEmpty();
log("upToDate = " + upToDate);
if (upToDate) {
for (Listener listener : myListeners) {
listener.allFilesResolved();
}
}
}
}
}
use of com.intellij.util.Processor in project intellij-plugins by JetBrains.
the class Struts2GlobalVariableProvider method getGlobalVariables.
@NotNull
public List<? extends FtlVariable> getGlobalVariables(final FtlFile file) {
final Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module == null) {
return Collections.emptyList();
}
if (StrutsFacet.getInstance(module) == null) {
return Collections.emptyList();
}
final List<FtlVariable> result = new ArrayList<>();
result.add(new MyFtlLightVariable("stack", file, (FtlType) null));
result.add(new MyFtlLightVariable("response", file, WebCommonClassNames.HTTP_SERVLET_RESPONSE));
result.add(new MyFtlLightVariable("res", file, WebCommonClassNames.HTTP_SERVLET_RESPONSE));
result.add(new MyFtlLightVariable("request", file, WebCommonClassNames.HTTP_SERVLET_REQUEST));
result.add(new MyFtlLightVariable("req", file, WebCommonClassNames.HTTP_SERVLET_REQUEST));
result.add(new MyFtlLightVariable("session", file, WebCommonClassNames.HTTP_SESSION));
result.add(new MyFtlLightVariable("application", file, WebCommonClassNames.SERVLET_CONTEXT));
result.add(new MyFtlLightVariable("base", file, CommonClassNames.JAVA_LANG_STRING));
installTaglibSupport(result, module, StrutsConstants.TAGLIB_STRUTS_UI_URI, StrutsConstants.TAGLIB_STRUTS_UI_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_JQUERY_PLUGIN_URI, StrutsConstants.TAGLIB_JQUERY_PLUGIN_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_JQUERY_RICHTEXT_PLUGIN_URI, StrutsConstants.TAGLIB_JQUERY_RICHTEXT_PLUGIN_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_JQUERY_CHART_PLUGIN_URI, StrutsConstants.TAGLIB_JQUERY_CHART_PLUGIN_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_JQUERY_TREE_PLUGIN_URI, StrutsConstants.TAGLIB_JQUERY_TREE_PLUGIN_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_JQUERY_GRID_PLUGIN_URI, StrutsConstants.TAGLIB_JQUERY_GRID_PLUGIN_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_JQUERY_MOBILE_PLUGIN_URI, StrutsConstants.TAGLIB_JQUERY_MOBILE_PLUGIN_PREFIX);
installTaglibSupport(result, module, StrutsConstants.TAGLIB_BOOTSTRAP_PLUGIN_URI, StrutsConstants.TAGLIB_BOOTSTRAP_PLUGIN_PREFIX);
final Processor<Action> processor = action -> {
final PsiClass actionClass = action.searchActionClass();
if (actionClass != null) {
for (final Result result1 : action.getResults()) {
final ResultType resultType = result1.getEffectiveResultType();
if (resultType != null && FreeMarkerStrutsResultContributor.FREEMARKER.equals(resultType.getName().getStringValue())) {
final PathReference reference = result1.getValue();
final PsiElement target = reference == null ? null : reference.resolve();
if (target != null && (file.getManager().areElementsEquivalent(file, target) || file.getManager().areElementsEquivalent(file.getOriginalFile(), target))) {
final PsiClassType actionType = PsiTypesUtil.getClassType(actionClass);
final FtlPsiType ftlPsiType = FtlPsiType.wrap(actionType);
result.add(new MyFtlLightVariable("", action.getXmlTag(), ftlPsiType));
result.add(new MyFtlLightVariable("action", action.getXmlTag(), ftlPsiType));
return false;
}
}
}
}
return true;
};
for (final StrutsModel model : StrutsManager.getInstance(file.getProject()).getAllModels(module)) {
model.processActions(processor);
}
return result;
}
Aggregations