use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class MorphAction method actionPerformed.
protected void actionPerformed(final GuiEditor editor, final List<RadComponent> selection, final AnActionEvent e) {
Processor<ComponentItem> processor = selectedValue -> {
Runnable runnable = () -> {
for (RadComponent c : selection) {
if (!morphComponent(editor, c, selectedValue))
break;
}
editor.refreshAndSave(true);
};
CommandProcessor.getInstance().executeCommand(editor.getProject(), runnable, UIDesignerBundle.message("morph.component.command"), null);
IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
IdeFocusManager.getGlobalInstance().requestFocus(editor.getGlassLayer(), true);
});
return true;
};
PaletteListPopupStep step = new PaletteListPopupStep(editor, myLastMorphComponent, processor, UIDesignerBundle.message("morph.component.title"));
step.hideNonAtomic();
if (selection.size() == 1) {
step.hideComponentClass(selection.get(0).getComponentClassName());
}
final ListPopup listPopup = JBPopupFactory.getInstance().createListPopup(step);
FormEditingUtil.showPopupUnderComponent(listPopup, selection.get(0));
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class PyiTypeProvider method getOverloads.
@NotNull
private static List<PyFunction> getOverloads(@NotNull PyFunction function, @NotNull final TypeEvalContext context) {
final ScopeOwner owner = ScopeUtil.getScopeOwner(function);
final String name = function.getName();
final List<PyFunction> overloads = new ArrayList<>();
final Processor<PyFunction> overloadsProcessor = f -> {
if (name != null && name.equals(f.getName()) && isOverload(f, context)) {
overloads.add(f);
}
return true;
};
if (owner instanceof PyClass) {
final PyClass cls = (PyClass) owner;
if (name != null) {
cls.visitMethods(overloadsProcessor, false, context);
}
} else if (owner instanceof PyFile) {
final PyFile file = (PyFile) owner;
for (PyFunction f : file.getTopLevelFunctions()) {
if (!overloadsProcessor.process(f)) {
break;
}
}
}
return overloads;
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class JavaFxPsiUtil method computeInjectedControllerClass.
@Nullable
private static CachedValueProvider.Result<PsiClass> computeInjectedControllerClass(PsiFile containingFile) {
return ourGuard.doPreventingRecursion(containingFile, true, () -> {
final Project project = containingFile.getProject();
final Ref<PsiClass> injectedController = new Ref<>();
final PsiClass fxmlLoader = JavaPsiFacade.getInstance(project).findClass(JavaFxCommonNames.JAVAFX_FXML_FXMLLOADER, GlobalSearchScope.allScope(project));
if (fxmlLoader != null) {
final PsiMethod[] injectControllerMethods = fxmlLoader.findMethodsByName("setController", false);
if (injectControllerMethods.length == 1) {
final GlobalSearchScope globalSearchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(containingFile.getResolveScope(), StdFileTypes.JAVA);
final VirtualFile[] virtualFiles = CacheManager.SERVICE.getInstance(project).getVirtualFilesWithWord(ClassUtil.extractClassName(JavaFxCommonNames.JAVAFX_FXML_FXMLLOADER), UsageSearchContext.IN_CODE, globalSearchScope, true);
if (virtualFiles.length == 0) {
return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
}
final GlobalSearchScope filesScope = GlobalSearchScope.filesScope(project, Arrays.asList(virtualFiles));
final Processor<PsiReference> processor = loaderReference -> findControllerClassInjection(loaderReference, injectedController, injectControllerMethods[0]);
ReferencesSearch.search(containingFile, filesScope).forEach(reference -> {
final PsiElement element = reference.getElement();
if (element instanceof PsiLiteralExpression) {
final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);
if (expression != null) {
final PsiType type = expression.getType();
if (type != null && type.equalsToText(JavaFxCommonNames.JAVAFX_FXML_FXMLLOADER)) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiLocalVariable) {
ReferencesSearch.search(parent).forEach(processor);
final PsiClass controller = injectedController.get();
if (controller != null) {
return false;
}
}
}
}
}
return true;
});
}
}
return new CachedValueProvider.Result<>(injectedController.get(), PsiModificationTracker.MODIFICATION_COUNT);
});
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class PsiSearchHelperImpl method isCheapEnoughToSearch.
@NotNull
@Override
public SearchCostResult isCheapEnoughToSearch(@NotNull String name, @NotNull final GlobalSearchScope scope, @Nullable final PsiFile fileToIgnoreOccurrencesIn, @Nullable final ProgressIndicator progress) {
final AtomicInteger count = new AtomicInteger();
final ProgressIndicator indicator = progress == null ? new EmptyProgressIndicator() : progress;
final Processor<VirtualFile> processor = new Processor<VirtualFile>() {
private final VirtualFile virtualFileToIgnoreOccurrencesIn = fileToIgnoreOccurrencesIn == null ? null : fileToIgnoreOccurrencesIn.getVirtualFile();
@Override
public boolean process(VirtualFile file) {
indicator.checkCanceled();
if (Comparing.equal(file, virtualFileToIgnoreOccurrencesIn))
return true;
final int value = count.incrementAndGet();
return value < 10;
}
};
List<IdIndexEntry> keys = getWordEntries(name, true);
boolean cheap = keys.isEmpty() || processFilesContainingAllKeys(myManager.getProject(), scope, null, keys, processor);
if (!cheap) {
return SearchCostResult.TOO_MANY_OCCURRENCES;
}
return count.get() == 0 ? SearchCostResult.ZERO_OCCURRENCES : SearchCostResult.FEW_OCCURRENCES;
}
use of com.intellij.util.Processor in project intellij-community by JetBrains.
the class PsiSearchHelperImpl method processFilesConcurrentlyDespiteWriteActions.
// Tries to run {@code localProcessor} for each file in {@code files} concurrently on ForkJoinPool.
// When encounters write action request, stops all threads, waits for write action to finish and re-starts all threads again.
// {@localProcessor} must be as idempotent as possible.
public static boolean processFilesConcurrentlyDespiteWriteActions(@NotNull Project project, @NotNull List<VirtualFile> files, @NotNull final ProgressIndicator progress, @NotNull final Processor<VirtualFile> localProcessor) {
ApplicationEx app = (ApplicationEx) ApplicationManager.getApplication();
final AtomicBoolean canceled = new AtomicBoolean(false);
while (true) {
List<VirtualFile> failedList = new SmartList<>();
final List<VirtualFile> failedFiles = Collections.synchronizedList(failedList);
final Processor<VirtualFile> processor = vfile -> {
try {
boolean result = localProcessor.process(vfile);
if (!result) {
canceled.set(true);
}
return result;
} catch (ApplicationUtil.CannotRunReadActionException action) {
failedFiles.add(vfile);
}
return !canceled.get();
};
boolean completed;
if (app.isWriteAccessAllowed() || app.isReadAccessAllowed() && app.isWriteActionPending()) {
// no point in processing in separate threads - they are doomed to fail to obtain read action anyway
completed = ContainerUtil.process(files, processor);
} else if (app.isWriteActionPending()) {
completed = true;
// we don't have read action now so wait for write action to complete
failedFiles.addAll(files);
} else {
// try to run parallel read actions but fail as soon as possible
completed = JobLauncher.getInstance().invokeConcurrentlyUnderProgress(files, progress, false, true, processor);
}
if (!completed) {
return false;
}
if (failedFiles.isEmpty()) {
break;
}
// we failed to run read action in job launcher thread
// run read action in our thread instead to wait for a write action to complete and resume parallel processing
DumbService.getInstance(project).runReadActionInSmartMode(EmptyRunnable.getInstance());
files = failedList;
}
return true;
}
Aggregations