public PsiMethodPattern definedInClass(final ElementPattern<? extends PsiClass> pattern) { return with(new PatternConditionPlus<PsiMethod, PsiClass>("definedInClass", pattern) { @Override public boolean processValues(PsiMethod t, final ProcessingContext context, final PairProcessor<PsiClass, ProcessingContext> processor) { if (!processor.process(t.getContainingClass(), context)) return false; final Ref<Boolean> result = Ref.create(Boolean.TRUE); SuperMethodsSearch.search(t, null, true, false).forEach(new Processor<MethodSignatureBackedByPsiMethod>() { @Override public boolean process(final MethodSignatureBackedByPsiMethod signature) { if (!processor.process(signature.getMethod().getContainingClass(), context)) { result.set(Boolean.FALSE); return false; } return true; } }); return result.get(); } }); }
static SliceNode filterTree(SliceNode oldRoot, NullableFunction<SliceNode, SliceNode> filter, PairProcessor<SliceNode, List<SliceNode>> postProcessor) { SliceNode filtered = filter.fun(oldRoot); if (filtered == null) return null; List<SliceNode> childrenFiltered = new ArrayList<SliceNode>(); if (oldRoot.myCachedChildren != null) { for (SliceNode child : oldRoot.myCachedChildren) { SliceNode childFiltered = filterTree(child, filter,postProcessor); if (childFiltered != null) { childrenFiltered.add(childFiltered); } } } boolean success = postProcessor == null || postProcessor.process(filtered, childrenFiltered); if (!success) return null; filtered.myCachedChildren = new ArrayList<SliceNode>(childrenFiltered); return filtered; }
public static void _do(@NotNull final ConfigurationError error, @NotNull final Project project, @NotNull final PairProcessor<ConfigurationErrors, ConfigurationError> fun) { if (!project.isInitialized()) { StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() { @Override public void run() { fun.process(project.getMessageBus().syncPublisher(TOPIC), error); } }); return; } final MessageBus bus = project.getMessageBus(); if (EventQueue.isDispatchThread()) fun.process(bus.syncPublisher(TOPIC), error); else { //noinspection SSBasedInspection SwingUtilities.invokeLater(new Runnable() { @Override public void run() { fun.process(bus.syncPublisher(TOPIC), error); } }); } }
public Self withSuperParent(final int level, @NotNull final ElementPattern<? extends ParentType> pattern) { return with(new PatternConditionPlus<T, ParentType>(level == 1 ? "withParent" : "withSuperParent", pattern) { @Override public boolean processValues(T t, ProcessingContext context, PairProcessor<ParentType, ProcessingContext> processor) { ParentType parent = t; for (int i = 0; i < level; i++) { if (parent == null) return true; parent = getParent(parent); } return processor.process(parent, context); } }); }
private void appendParams(final StringBuilder builder, final String indent) { processParameters(new PairProcessor<String, Object>() { int count; String prevName; int prevOffset; @Override public boolean process(String name, Object value) { count ++; if (count == 2) builder.insert(prevOffset, prevName +"="); if (count > 1) builder.append(", "); prevOffset = builder.length(); if (count > 1) builder.append(name).append("="); appendValue(builder, indent, value); prevName = name; return true; } }); }
public boolean processParameters(final PairProcessor<String, Object> processor) { for (Class aClass = getClass(); aClass != null; aClass = aClass.getSuperclass()) { for (final Field field : aClass.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers()) && (((field.getModifiers() & 0x1000 /*Modifer.SYNTHETIC*/) == 0 && !aClass.equals(PatternCondition.class)) || field.getName().startsWith(PARAMETER_FIELD_PREFIX))) { final String name = field.getName(); final String fixedName = name.startsWith(PARAMETER_FIELD_PREFIX) ? name.substring(PARAMETER_FIELD_PREFIX.length()) : name; final Object value = getFieldValue(field); if (!processor.process(fixedName, value)) return false; } } } return true; }
public static boolean treeWalkUp(@NotNull final PsiElement entrance, @Nullable final PsiElement maxScope, PairProcessor<PsiElement, PsiElement> eachScopeAndLastParent) { PsiElement prevParent = null; PsiElement scope = entrance; while (scope != null) { if (!eachScopeAndLastParent.process(scope, prevParent)) return false; if (scope == maxScope) break; prevParent = scope; scope = prevParent.getContext(); } return true; }
@Override public void apply(@NotNull List<QuickList> settings) throws ConfigurationException { itemPanel.apply(); editor.ensureNonEmptyNames("Quick list should have non empty name"); editor.processModifiedItems(new PairProcessor<QuickList, QuickList>() { @Override public boolean process(QuickList newItem, QuickList oldItem) { if (!oldItem.getName().equals(newItem.getName())) { keymapListener.quickListRenamed(oldItem, newItem); } return true; } }); if (isModified(settings)) { java.util.List<QuickList> result = editor.apply(); keymapListener.processCurrentKeymapChanged(result.toArray(new QuickList[result.size()])); QuickListsManager.getInstance().setQuickLists(result); } }
@Override protected void setUp() throws Exception { super.setUp(); myReloadFromDisk = Boolean.TRUE; FileDocumentManagerImpl impl = (FileDocumentManagerImpl)FileDocumentManager.getInstance(); impl.setAskReloadFromDisk(getTestRootDisposable(), new PairProcessor<VirtualFile, Document>() { @Override public boolean process(VirtualFile file, Document document) { if (myReloadFromDisk == null) { fail(); return false; } return myReloadFromDisk.booleanValue(); } }); myDocumentManager = impl; }
public QuerySearchRequest(@NotNull Query<PsiReference> query, @NotNull final SearchRequestCollector collector, boolean inReadAction, @NotNull final PairProcessor<PsiReference, SearchRequestCollector> processor) { this.query = query; this.collector = collector; if (inReadAction) { this.processor = new ReadActionProcessor<PsiReference>() { @Override public boolean processInReadAction(PsiReference psiReference) { return processor.process(psiReference, collector); } }; } else { this.processor = new Processor<PsiReference>() { @Override public boolean process(PsiReference psiReference) { return processor.process(psiReference, collector); } }; } }
public void getSimiliar(final Key key, final PairProcessor<Key, Val> consumer) { final int idx = Collections.binarySearch(myKeys, key, myComparator); if (idx < 0) { final int insertionIdx = - idx - 1; // take item before final int itemBeforeIdx = insertionIdx - 1; if (itemBeforeIdx >= 0) { for (ListIterator<Key> iterator = myKeys.listIterator(itemBeforeIdx + 1); iterator.hasPrevious(); ) { final Key candidate = iterator.previous(); if (! myKeysResemblance.process(candidate, key)) continue; if (consumer.process(candidate, myMap.get(candidate))) break; // if need only a part of keys } } } else { consumer.process(key, myMap.get(key)); } }
public void optimizeMap(final PairProcessor<Val, Val> valuesAreas) { int i = 0; for (Iterator<Key> iterator = myKeys.iterator(); iterator.hasNext();) { final Key key = iterator.next(); final Val value = myMap.get(key); // go for parents for (int j = i - 1; j >= 0; -- j) { final Key innerKey = myKeys.get(j); if (myKeysResemblance.process(innerKey, key)) { if (valuesAreas.process(myMap.get(innerKey), value)) { -- i; iterator.remove(); myMap.remove(key); } // otherwise we found a "parent", and do not remove the child break; } } ++ i; } }
@Override public void run(@NotNull ProgressIndicator indicator) { try { //indicator.setText2("Checking and possibly creating database"); indicator.setText2("Updating VCS and roots"); final MultiMap<String, String> map = new MultiMap<String, String>(); myCachesHolder.iterateAllRepositoryLocations(new PairProcessor<RepositoryLocation, AbstractVcs>() { @Override public boolean process(RepositoryLocation location, AbstractVcs vcs) { map.putValue(vcs.getName(), location2string(location)); return true; } }); myDbUtil.checkVcsRootsAreTracked(map); } catch (VcsException e) { LOG.info(e); myException = e; } }
public void startTemplateWithPrefix(final Editor editor, final TemplateImpl template, @Nullable final PairProcessor<String, String> processor, @Nullable String argument) { final int caretOffset = editor.getCaretModel().getOffset(); String key = template.getKey(); int startOffset = caretOffset - key.length(); if (argument != null) { if (!isDelimiter(key.charAt(key.length() - 1))) { // pass space startOffset--; } startOffset -= argument.length(); } startTemplateWithPrefix(editor, template, startOffset, processor, argument); }
public void startTemplateWithPrefix(final Editor editor, final TemplateImpl template, final int templateStart, @Nullable final PairProcessor<String, String> processor, @Nullable final String argument) { final int caretOffset = editor.getCaretModel().getOffset(); final TemplateState templateState = initTemplateState(editor); CommandProcessor commandProcessor = CommandProcessor.getInstance(); commandProcessor.executeCommand(myProject, new Runnable() { @Override public void run() { editor.getDocument().deleteString(templateStart, caretOffset); editor.getCaretModel().moveToOffset(templateStart); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); editor.getSelectionModel().removeSelection(); Map<String, String> predefinedVarValues = null; if (argument != null) { predefinedVarValues = new HashMap<String, String>(); predefinedVarValues.put(TemplateImpl.ARG, argument); } templateState.start(template, processor, predefinedVarValues); } }, CodeInsightBundle.message("insert.code.template.command"), null); }
@Nullable public static Pair<PsiFile,Editor> chooseBetweenHostAndInjected(@NotNull PsiFile hostFile, @NotNull Editor hostEditor, @NotNull PairProcessor<PsiFile, Editor> predicate) { Editor editorToApply = null; PsiFile fileToApply = null; int offset = hostEditor.getCaretModel().getOffset(); PsiFile injectedFile = InjectedLanguageUtil.findInjectedPsiNoCommit(hostFile, offset); if (injectedFile != null) { Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(hostEditor, injectedFile); if (predicate.process(injectedFile, injectedEditor)) { editorToApply = injectedEditor; fileToApply = injectedFile; } } if (editorToApply == null && predicate.process(hostFile, hostEditor)) { editorToApply = hostEditor; fileToApply = hostFile; } if (editorToApply == null) return null; return Pair.create(fileToApply, editorToApply); }
@Override protected void collectAdditionalElementsToRename(final List<Pair<PsiElement, TextRange>> stringUsages) { final String stringToSearch = myElementToRename.getName(); final PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument()); if (stringToSearch != null) { TextOccurrencesUtil .processUsagesInStringsAndComments(myElementToRename, stringToSearch, true, new PairProcessor<PsiElement, TextRange>() { @Override public boolean process(PsiElement psiElement, TextRange textRange) { if (psiElement.getContainingFile() == currentFile) { stringUsages.add(Pair.create(psiElement, textRange)); } return true; } }); } }
public static boolean processUsagesInStringsAndComments(@NotNull final PsiElement element, @NotNull final String stringToSearch, final boolean ignoreReferences, @NotNull final PairProcessor<PsiElement, TextRange> processor) { PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(element.getProject()); SearchScope scope = helper.getUseScope(element); scope = GlobalSearchScope.projectScope(element.getProject()).intersectWith(scope); Processor<PsiElement> commentOrLiteralProcessor = new Processor<PsiElement>() { @Override public boolean process(PsiElement literal) { return processTextIn(literal, stringToSearch, ignoreReferences, processor); } }; return processStringLiteralsContainingIdentifier(stringToSearch, scope, helper, commentOrLiteralProcessor) && helper.processCommentsContainingIdentifier(stringToSearch, scope, commentOrLiteralProcessor); }
public static void addUsagesInStringsAndComments(@NotNull PsiElement element, @NotNull String stringToSearch, @NotNull final Collection<UsageInfo> results, @NotNull final UsageInfoFactory factory) { final Object lock = new Object(); processUsagesInStringsAndComments(element, stringToSearch, false, new PairProcessor<PsiElement, TextRange>() { @Override public boolean process(PsiElement commentOrLiteral, TextRange textRange) { UsageInfo usageInfo = factory.createUsageInfo(commentOrLiteral, textRange.getStartOffset(), textRange.getEndOffset()); if (usageInfo != null) { synchronized (lock) { results.add(usageInfo); } } return true; } }); }
private boolean processEnumeration(XmlElement context, PairProcessor<PsiElement, String> processor, boolean forCompletion) { XmlTag contextTag = context != null ? PsiTreeUtil.getContextOfType(context, XmlTag.class, false) : null; final XmlElementDescriptorImpl elementDescriptor = (XmlElementDescriptorImpl)XmlUtil.findXmlDescriptorByType(getDeclaration(), contextTag); if (elementDescriptor!=null && elementDescriptor.getType() instanceof ComplexTypeDescriptor) { return processEnumerationImpl(((ComplexTypeDescriptor)elementDescriptor.getType()).getDeclaration(), processor, forCompletion); } final String namespacePrefix = getDeclaration().getNamespacePrefix(); XmlTag type = getDeclaration().findFirstSubTag( ((namespacePrefix.length() > 0) ? namespacePrefix + ":" : "") + "simpleType" ); if (type != null) { return processEnumerationImpl(type, processor, forCompletion); } return false; }
private static void processTags(@NotNull Project project, @Nullable String templateText, @NotNull PairProcessor<XmlTag, Boolean> processor) { if (StringUtil.isNotEmpty(templateText)) { final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(project); XmlFile xmlFile = (XmlFile)psiFileFactory.createFileFromText("dummy.xml", StdFileTypes.HTML, templateText); XmlTag tag = xmlFile.getRootTag(); boolean firstTag = true; while (tag != null) { processor.process(tag, firstTag); firstTag = false; tag = PsiTreeUtil.getNextSiblingOfType(tag, XmlTag.class); } } }
public boolean removeHostWithUndo(final Project project, final PsiLanguageInjectionHost host) { InjectedLanguage prevLanguage = host.getUserData(LanguageInjectionSupport.TEMPORARY_INJECTED_LANGUAGE); if (prevLanguage == null) return false; SmartPointerManager manager = SmartPointerManager.getInstance(myProject); SmartPsiElementPointer<PsiLanguageInjectionHost> pointer = manager.createSmartPsiElementPointer(host); TempPlace place = new TempPlace(prevLanguage, pointer); TempPlace nextPlace = new TempPlace(null, pointer); Configuration.replaceInjectionsWithUndo( project, nextPlace, place, Collections.<PsiElement>emptyList(), new PairProcessor<TempPlace, TempPlace>() { public boolean process(final TempPlace add, final TempPlace remove) { addInjectionPlace(add); return true; } }); return true; }
@NotNull @Override public Collection<? extends ActionOrGroup> getVariants(ConvertContext context) { final List<ActionOrGroup> variants = new ArrayList<ActionOrGroup>(); PairProcessor<String, ActionOrGroup> collectProcessor = new PairProcessor<String, ActionOrGroup>() { @Override public boolean process(String s, ActionOrGroup actionOrGroup) { if (isRelevant(actionOrGroup)) { variants.add(actionOrGroup); } return true; } }; processActionOrGroup(context, collectProcessor); return variants; }
@Nullable @Override public ActionOrGroup fromString(@Nullable @NonNls final String value, ConvertContext context) { if (StringUtil.isEmptyOrSpaces(value)) return null; final ActionOrGroup[] result = {null}; PairProcessor<String, ActionOrGroup> findProcessor = new PairProcessor<String, ActionOrGroup>() { @Override public boolean process(String s, ActionOrGroup actionOrGroup) { if (isRelevant(actionOrGroup) && Comparing.strEqual(value, s)) { result[0] = actionOrGroup; return false; } return true; } }; processActionOrGroup(context, findProcessor); return result[0]; }
public static boolean doTreeWalkUp(@NotNull final PsiElement place, @NotNull final PsiElement originalPlace, @NotNull final PsiScopeProcessor processor, @Nullable final PsiScopeProcessor nonCodeProcessor, @NotNull final ResolveState state) { final GrClosableBlock maxScope = nonCodeProcessor != null ? PsiTreeUtil.getParentOfType(place, GrClosableBlock.class, true, PsiFile.class) : null; return PsiTreeUtil.treeWalkUp(place, maxScope, new PairProcessor<PsiElement, PsiElement>() { @Override public boolean process(PsiElement scope, PsiElement lastParent) { ProgressManager.checkCanceled(); if (!doProcessDeclarations(originalPlace, lastParent, scope, substituteProcessor(processor, scope), nonCodeProcessor, state)) { return false; } issueLevelChangeEvents(processor, scope); return true; } }); }
static SliceNode filterTree(SliceNode oldRoot, NullableFunction<SliceNode, SliceNode> filter, PairProcessor<SliceNode, List<SliceNode>> postProcessor){ SliceNode filtered = filter.fun(oldRoot); if (filtered == null) return null; List<SliceNode> childrenFiltered = new ArrayList<SliceNode>(); if (oldRoot.myCachedChildren != null) { for (SliceNode child : oldRoot.myCachedChildren) { SliceNode childFiltered = filterTree(child, filter,postProcessor); if (childFiltered != null) { childrenFiltered.add(childFiltered); } } } boolean success = postProcessor == null || postProcessor.process(filtered, childrenFiltered); if (!success) return null; filtered.myCachedChildren = new ArrayList<SliceNode>(childrenFiltered); return filtered; }
public Self withSuperParent(final int level, @NotNull final ElementPattern<? extends ParentType> pattern) { return with(new PatternConditionPlus<T, ParentType>("withSuperParent", pattern) { @Override public boolean processValues(T t, ProcessingContext context, PairProcessor<ParentType, ProcessingContext> processor) { ParentType parent = t; for (int i = 0; i < level; i++) { if (parent == null) return false; parent = getParent(parent); } return processor.process(parent, context); } }); }
public boolean processParameters(final PairProcessor<String, Object> processor) { for (Class aClass = getClass(); aClass != null; aClass = ReflectionCache.getSuperClass(aClass)) { for (final Field field : aClass.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers()) && (((field.getModifiers() & 0x1000 /*Modifer.SYNTHETIC*/) == 0 && !aClass.equals(PatternCondition.class)) || field.getName().startsWith(PARAMETER_FIELD_PREFIX))) { final String name = field.getName(); final String fixedName = name.startsWith(PARAMETER_FIELD_PREFIX) ? name.substring(PARAMETER_FIELD_PREFIX.length()) : name; final Object value = getFieldValue(field); if (!processor.process(fixedName, value)) return false; } } } return true; }