@NotNull public static String buildStringToFindForIndicesFromRegExp( @NotNull String stringToFind, @NotNull Project project) { if (!Registry.is("idea.regexp.search.uses.indices")) return ""; final AccessToken accessToken = ReadAction.start(); try { final List<PsiElement> topLevelRegExpChars = getTopLevelRegExpChars("a", project); if (topLevelRegExpChars.size() != 1) return ""; // leave only top level regExpChars return StringUtil.join( getTopLevelRegExpChars(stringToFind, project), new Function<PsiElement, String>() { final Class regExpCharPsiClass = topLevelRegExpChars.get(0).getClass(); @Override public String fun(PsiElement element) { return regExpCharPsiClass.isInstance(element) ? element.getText() : " "; } }, ""); } finally { accessToken.finish(); } }
private static void logStats(Collection<PsiFile> otherFiles, long start) { long time = System.currentTimeMillis() - start; final Multiset<String> stats = HashMultiset.create(); for (PsiFile file : otherFiles) { stats.add( StringUtil.notNullize(file.getViewProvider().getVirtualFile().getExtension()) .toLowerCase()); } List<String> extensions = ContainerUtil.newArrayList(stats.elementSet()); Collections.sort( extensions, new Comparator<String>() { @Override public int compare(String o1, String o2) { return stats.count(o2) - stats.count(o1); } }); String message = "Search in " + otherFiles.size() + " files with unknown types took " + time + "ms.\n" + "Mapping their extensions to an existing file type (e.g. Plain Text) might speed up the search.\n" + "Most frequent non-indexed file extensions: "; for (int i = 0; i < Math.min(10, extensions.size()); i++) { String extension = extensions.get(i); message += extension + "(" + stats.count(extension) + ") "; } LOG.info(message); }
/** @deprecated to be removed in IDEA 16 */ @Nullable public static Pattern createFileMaskRegExp(@Nullable String filter) { if (filter == null) { return null; } String pattern; final List<String> strings = StringUtil.split(filter, ","); if (strings.size() == 1) { pattern = PatternUtil.convertToRegex(filter.trim()); } else { pattern = StringUtil.join(strings, s -> "(" + PatternUtil.convertToRegex(s.trim()) + ")", "|"); } return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); }
private static PsiElement[] getParameterElementsToSearch(final PsiParameter parameter) { final PsiMethod method = (PsiMethod) parameter.getDeclarationScope(); PsiMethod[] overrides = OverridingMethodsSearch.search(method, true).toArray(PsiMethod.EMPTY_ARRAY); for (int i = 0; i < overrides.length; i++) { overrides[i] = (PsiMethod) overrides[i].getNavigationElement(); } List<PsiElement> elementsToSearch = new ArrayList<PsiElement>(overrides.length + 1); elementsToSearch.add(parameter); int idx = method.getParameterList().getParameterIndex(parameter); for (PsiMethod override : overrides) { final PsiParameter[] parameters = override.getParameterList().getParameters(); if (idx < parameters.length) { elementsToSearch.add(parameters[idx]); } } return elementsToSearch.toArray(new PsiElement[elementsToSearch.size()]); }
private static List<PsiElement> getTopLevelRegExpChars(String regExpText, Project project) { @SuppressWarnings("deprecation") PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("A.regexp", regExpText); List<PsiElement> result = null; final PsiElement[] children = file.getChildren(); for (PsiElement child : children) { PsiElement[] grandChildren = child.getChildren(); if (grandChildren.length != 1) return Collections .emptyList(); // a | b, more than one branch, can not predict in current way for (PsiElement grandGrandChild : grandChildren[0].getChildren()) { if (result == null) result = new ArrayList<>(); result.add(grandGrandChild); } } return result != null ? result : Collections.<PsiElement>emptyList(); }