@Nullable @Contract("null, _ -> null") public static PsiElement findSiblingByPredicate( @Nullable final PsiElement sibling, @NotNull final Predicate<PsiElement> predicate ) { if (sibling == null) { return null; } else { for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) { if (predicate.apply(child)) { return child; } } return null; } }
public static List<SmcFile> findSmcFile(Project project, Predicate<SmcFile> predicate) { List<SmcFile> result = null; Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, SmcFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { SmcFile smcFile = (SmcFile) PsiManager.getInstance(project).findFile(virtualFile); if (smcFile != null && (predicate == null || predicate.apply(smcFile))) { if (result == null) { result = new ArrayList<>(); } result.add(smcFile); } } return result != null ? result : Collections.<SmcFile>emptyList(); }
public static List<? extends SmcMethodLikeElement> findMethodLikeForMethod(PsiMethod psiMethod, Class<? extends SmcMethodLikeElement> aClass, Predicate<SmcMethodLikeElement> predicate) { List<SmcMethodLikeElement> result = null; Project project = psiMethod.getProject(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, SmcFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { SmcFile simpleFile = (SmcFile) PsiManager.getInstance(project).findFile(virtualFile); if (simpleFile != null) { Collection<? extends SmcMethodLikeElement> methodLikeElements = PsiTreeUtil.findChildrenOfType(simpleFile, aClass); for (SmcMethodLikeElement methodLike : methodLikeElements) { if (psiMethod.getName().equals(methodLike.getName()) && psiMethod.getParameterList().getParametersCount() == methodLike.getArgumentCount() && (predicate == null || predicate.apply(methodLike))) { if (result == null) { result = new ArrayList<>(); } result.add(methodLike); } } } } return result != null ? result : Collections.<SmcMethodLikeElement>emptyList(); }
public static List<SmcTransition> findTransitionByMethod(PsiMethod psiMethod, Predicate<SmcTransition> predicate) { List<SmcTransition> result = null; Project project = psiMethod.getProject(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, SmcFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { SmcFile simpleFile = (SmcFile) PsiManager.getInstance(project).findFile(virtualFile); if (simpleFile != null) { Collection<SmcTransition> smcTransitions = PsiTreeUtil.findChildrenOfType(simpleFile, SmcTransition.class); for (SmcTransition transition : smcTransitions) { if (psiMethod.getName().equals(transition.getName()) && (psiMethod.getParameterList().getParametersCount() == transition.getArgumentCount() + 1) && (predicate == null || predicate.apply(transition))) { if (result == null) { result = new ArrayList<>(); } result.add(transition); } } } } return result != null ? result : Collections.<SmcTransition>emptyList(); }
private void registerMethodLike(PsiMethod psiMethod, PsiElement identifier, Collection<? super RelatedItemLineMarkerInfo> result, Class<? extends SmcMethodLikeElement> aClass, Predicate<SmcMethodLikeElement> predicate, Icon icon, String toolTipPattern) { PsiElement nameIdentifier = identifier != null ? identifier : psiMethod.getNameIdentifier(); if (nameIdentifier != null) { final List<? extends SmcMethodLikeElement> methodLikeElements = SmcPsiUtil.findMethodLikeForMethod(psiMethod, aClass, predicate); if (methodLikeElements != null && !methodLikeElements.isEmpty()) { ItemPresentation presentation = psiMethod.getPresentation(); NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(icon). setTargets(methodLikeElements). setTooltipText(MessageFormat.format(toolTipPattern, presentation != null ? presentation.getPresentableText() : psiMethod.getName())); result.add(builder.createLineMarkerInfo(nameIdentifier)); } } }
private void registerTransition(PsiMethod psiMethod, PsiElement identifier, Collection<? super RelatedItemLineMarkerInfo> result, Predicate<SmcTransition> predicate, Icon icon, String toolTipPattern) { PsiElement nameIdentifier = identifier != null ? identifier : psiMethod.getNameIdentifier(); if (nameIdentifier != null) { final List<SmcTransition> transitions = SmcPsiUtil.findTransitionByMethod(psiMethod, predicate); if (transitions != null && !transitions.isEmpty()) { ItemPresentation presentation = psiMethod.getPresentation(); NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(icon). setTargets(transitions). setTooltipText(MessageFormat.format(toolTipPattern, presentation != null ? presentation.getPresentableText() : psiMethod.getName())); result.add(builder.createLineMarkerInfo(nameIdentifier)); } } }
public void registerMarker(PsiElement element, Collection<? super RelatedItemLineMarkerInfo> result, Predicate<SmcFile> predicate, Icon icon, String messagePattern) { PsiClass psiClass = (PsiClass) element; PsiIdentifier psiName = psiClass.getNameIdentifier(); String name = psiClass.getQualifiedName(); if (name != null && psiName != null) { final List<SmcFile> smcFiles = SmcPsiUtil.findSmcFile(psiClass.getProject(), predicate); if (smcFiles != null && !smcFiles.isEmpty()) { NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(icon). setTargets(smcFiles). setTooltipText(MessageFormat.format(messagePattern, name)); result.add(builder.createLineMarkerInfo(psiName)); } } }
private FindResult findStringLoop(CharSequence text, int offset, FindModel model, VirtualFile file, @Nullable Predicate<FindResult> filter) { final char[] textArray = CharArrayUtil.fromSequenceWithoutCopying(text); while(true) { FindResult result = doFindString(text, textArray, offset, model, file); if (filter == null || filter.apply(result)) { if (!model.isWholeWordsOnly()) { return result; } if (!result.isStringFound()) { return result; } if (isWholeWord(text, result.getStartOffset(), result.getEndOffset())) { return result; } } offset = model.isForward() ? result.getStartOffset() + 1 : result.getEndOffset() - 1; if (offset > text.length() || offset < 0) return NOT_FOUND_RESULT; } }
private static TheRS4ClassType byName(Project project, final String name, Set<String> recursionGuard) { if (recursionGuard.contains(name)) { return null; } TheRCallExpression call = TheRPsiUtils.findCall(project, "setClass", new Predicate<TheRCallExpression>() { @Override public boolean apply(@Nullable TheRCallExpression input) { TheRExpression s4Class = TheRPsiUtils.findParameterValue("Class", input); if (s4Class == null || !TheRStringLiteralExpression.class.isInstance(s4Class)) { return false; } String text = s4Class.getText(); return name.equals(text.substring(1, text.length() - 1)); } }); return call != null ? TheRS4ClassType.createFromSetClass(call, recursionGuard) : null; }
private FindResult findStringLoop(CharSequence text, int offset, FindModel model, VirtualFile file, @Nullable Predicate<FindResult> filter) { final char[] textArray = CharArrayUtil.fromSequenceWithoutCopying(text); while (true) { FindResult result = doFindString(text, textArray, offset, model, file); if (filter == null || filter.apply(result)) { if (!model.isWholeWordsOnly()) { return result; } if (!result.isStringFound()) { return result; } if (isWholeWord(text, result.getStartOffset(), result.getEndOffset())) { return result; } } offset = model.isForward() ? result.getStartOffset() + 1 : result.getEndOffset() - 1; if (offset > text.length() || offset < 0) return NOT_FOUND_RESULT; } }
private Predicate<FindResult> getFindContextPredicate(@Nonnull FindModel model, VirtualFile file, CharSequence text) { if (file == null) return null; FindModel.SearchContext context = model.getSearchContext(); if (context == FindModel.SearchContext.ANY || context == FindModel.SearchContext.IN_COMMENTS || context == FindModel.SearchContext.IN_STRING_LITERALS) { return null; } synchronized (model) { FindExceptCommentsOrLiteralsData data = model.getUserData(ourExceptCommentsOrLiteralsDataKey); if (data == null || !data.isAcceptableFor(model, file, text)) { model.putUserData(ourExceptCommentsOrLiteralsDataKey, data = new FindExceptCommentsOrLiteralsData(file, model, text)); } return data; } }
public ProjectStructureChooseLibrariesDialog(ClasspathPanel classpathPanel, StructureConfigurableContext context, Predicate<Library> acceptedLibraries) { super(classpathPanel.getComponent(), "Choose Libraries", classpathPanel.getProject(), true); myClasspathPanel = classpathPanel; myContext = context; myAcceptedLibraries = acceptedLibraries; setOKButtonText("Add Selected"); init(); }
private boolean hasLibraries() { final Predicate<Library> condition = getNotAddedSuitableLibrariesCondition(); for (LibraryTable table : ChooseLibrariesFromTablesDialog.getLibraryTables(myClasspathPanel.getProject(), true)) { final LibrariesModifiableModel model = myContext.myLevel2Providers.get(table.getTableLevel()); if (model != null) { for (Library library : model.getLibraries()) { if (condition.apply(library)) { return true; } } } } return false; }
private Predicate<FindResult> getFindContextPredicate(@NotNull FindModel model, VirtualFile file, CharSequence text) { if (file == null) return null; FindModel.SearchContext context = model.getSearchContext(); if( context == FindModel.SearchContext.ANY || context == FindModel.SearchContext.IN_COMMENTS || context == FindModel.SearchContext.IN_STRING_LITERALS) { return null; } FindExceptCommentsOrLiteralsData data = model.getUserData(ourExceptCommentsOrLiteralsDataKey); if (data == null || !data.isAcceptableFor(model, file, text)) { model.putUserData(ourExceptCommentsOrLiteralsDataKey, data = new FindExceptCommentsOrLiteralsData(file, model, text)); } return data; }
public static TheRCallExpression findCall(Project project, String functionName, Predicate<TheRCallExpression> predicate) { ProjectAndLibrariesScope scope = new ProjectAndLibrariesScope(project); Collection<TheRAssignmentStatement> possibleDefinitions = TheRAssignmentNameIndex.find(functionName, project, scope); TheRAssignmentStatement functionDefinition = null; for (TheRAssignmentStatement assignment : possibleDefinitions) { if (assignment.getAssignedValue() instanceof TheRFunctionExpression) { functionDefinition = assignment; break; } } if (functionDefinition == null) { return null; } for (PsiReference reference : ReferencesSearch.search(functionDefinition, scope)) { PsiElement referenceFrom = reference.getElement(); PsiElement parent = referenceFrom.getParent(); if (parent == null || !TheRCallExpression.class.isInstance(parent)) { continue; } TheRCallExpression call = (TheRCallExpression)parent; if (predicate.apply(call)) { return call; } } return null; }
private boolean hasLibraries() { final Predicate<Library> condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel()); for (LibraryTable table : ChooseLibrariesFromTablesDialog.getLibraryTables(myClasspathPanel.getProject(), true)) { final LibrariesModifiableModel model = myContext.myLevel2Providers.get(table.getTableLevel()); if (model != null) { for (Library library : model.getLibraries()) { if (condition.apply(library)) { return true; } } } } return false; }
@Override @NotNull public List<Library> chooseElements() { final Predicate<Library> condition = LibraryEditingUtil.getNotAddedLibrariesCondition(myClasspathPanel.getRootModel()); ProjectStructureChooseLibrariesDialog dialog = new ProjectStructureChooseLibrariesDialog(myClasspathPanel, myContext, condition); dialog.show(); return dialog.getSelectedLibraries(); }
public static <T> List<T> filter(Collection<T> data, Predicate<T> pred) { if ( data==null ) return null; List<T> filtered = new ArrayList<T>(); for (T x : data) { if ( pred.apply(x) ) filtered.add(x); } return filtered; }
private Predicate<Library> getNotAddedSuitableLibrariesCondition() { ProjectFacetsConfigurator facetsConfigurator = myContext.getModulesConfigurator().getFacetsConfigurator(); return LibraryEditingUtil.getNotAddedSuitableLibrariesCondition(myClasspathPanel.getRootModel(), facetsConfigurator); }
/** * Downloads content of {@code url} to {@code outputFile} atomically.<br/> * {@code outputFile} isn't modified if an I/O error occurs or {@code contentChecker} is provided and returns false on the downloaded content. * More formally, the steps are: * <ol> * <li>Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.</li> * <li>Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.</li> * <li>Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.</li> * </ol> * <p/> * Motivation: some web filtering products return pure HTML with HTTP 200 OK status instead of * the asked content. * * @param indicator progress indicator * @param url url to download * @param outputFile output file * @param tempFile temporary file to download to. This file is deleted on method exit. * @param contentChecker checks whether the downloaded content is OK or not * @throws IOException if an I/O error occurs * @returns true if no {@code contentChecker} is provided or the provided one returned true */ public static boolean downloadAtomically(@Nullable ProgressIndicator indicator, @NotNull String url, @NotNull File outputFile, @NotNull File tempFile, @Nullable Predicate<String> contentChecker) throws IOException { try { downloadContentToFile(indicator, url, tempFile); if (contentChecker != null) { String content = FileUtil.loadFile(tempFile); if (!contentChecker.apply(content)) { return false; } } FileUtil.rename(tempFile, outputFile); return true; } finally { FileUtil.delete(tempFile); } }
/** * Downloads content of {@code url} to {@code outputFile} atomically.<br/> * {@code outputFile} isn't modified if an I/O error occurs or {@code contentChecker} is provided and returns false on the downloaded content. * More formally, the steps are: * <ol> * <li>Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.</li> * <li>Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.</li> * <li>Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.</li> * </ol> * * Motivation: some web filtering products return pure HTML with HTTP 200 OK status instead of * the asked content. * * @param indicator progress indicator * @param url url to download * @param outputFile output file * @param tempFile temporary file to download to. This file is deleted on method exit. * @param contentChecker checks whether the downloaded content is OK or not * @returns true if no {@code contentChecker} is provided or the provided one returned true * @throws IOException if an I/O error occurs */ public static boolean downloadAtomically(@Nullable ProgressIndicator indicator, @NotNull String url, @NotNull File outputFile, @NotNull File tempFile, @Nullable Predicate<String> contentChecker) throws IOException { try { downloadContentToFile(indicator, url, tempFile); if (contentChecker != null) { String content = FileUtil.loadFile(tempFile); if (!contentChecker.apply(content)) { return false; } } FileUtil.rename(tempFile, outputFile); return true; } finally { FileUtil.delete(tempFile); } }
@Nonnull public static <E extends Predicate<IElementType>> ElementTypeEntryExtensionCollector<E> create(@Nonnull String epName) { return new ElementTypeEntryExtensionCollector<>(epName); }
/** * Downloads content of {@code url} to {@code outputFile} atomically.<br/> * {@code outputFile} isn't modified if an I/O error occurs or {@code contentChecker} is provided and returns false on the downloaded content. * More formally, the steps are: * <ol> * <li>Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.</li> * <li>Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.</li> * <li>Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.</li> * </ol> * * Motivation: some web filtering products return pure HTML with HTTP 200 OK status instead of * the asked content. * * @param indicator progress indicator * @param url url to download * @param outputFile output file * @param tempFile temporary file to download to. This file is deleted on method exit. * @param contentChecker checks whether the downloaded content is OK or not * @returns true if no {@code contentChecker} is provided or the provided one returned true * @throws IOException if an I/O error occurs */ public static boolean downloadAtomically(@Nullable ProgressIndicator indicator, @Nonnull String url, @Nonnull File outputFile, @Nonnull File tempFile, @Nullable Predicate<String> contentChecker) throws IOException { try { downloadContentToFile(indicator, url, tempFile); if (contentChecker != null) { String content = FileUtil.loadFile(tempFile); if (!contentChecker.apply(content)) { return false; } } FileUtil.rename(tempFile, outputFile); return true; } finally { FileUtil.delete(tempFile); } }