@NotNull @Override public ResolveResult[] multiResolve(final boolean incompleteCode) { Project project = myElement.getProject(); final String enumLiteralJavaModelName = myElement.getText().replaceAll("\"", "").toUpperCase(); final PsiShortNamesCache psiShortNamesCache = PsiShortNamesCache.getInstance(project); final PsiField[] javaEnumLiteralFields = psiShortNamesCache.getFieldsByName( enumLiteralJavaModelName, GlobalSearchScope.allScope(project) ); final Set<PsiField> enumFields = stream(javaEnumLiteralFields) .filter(literal -> literal.getParent() != null) .filter(literal -> literal.getParent() instanceof ClsClassImpl) .filter(literal -> ((ClsClassImpl) literal.getParent()).isEnum()) .collect(Collectors.toSet()); return PsiElementResolveResult.createResults(enumFields); }
@NotNull @Override public ResolveResult[] multiResolve(final boolean incompleteCode) { final Project project = myElement.getProject(); final String modelName = PATTERN.matcher(myElement.getText()).replaceAll(""); final String javaModelName = modelName + JAVA_MODEL_SUFFIX; final String jaloModelName = modelName; final PsiClass[] javaModelClasses = PsiShortNamesCache.getInstance(project).getClassesByName( javaModelName, GlobalSearchScope.allScope(project) ); final PsiClass[] jaloModelClasses = PsiShortNamesCache.getInstance(project).getClassesByName( jaloModelName, GlobalSearchScope.projectScope(project) ); final PsiClass[] psiClasses = ArrayUtil.mergeArrays(javaModelClasses, jaloModelClasses); return PsiElementResolveResult.createResults(psiClasses); }
/** * search class by class name. * * @param name * @param project * @return */ private static PsiClass searchClassByName(String name, Project project) { GlobalSearchScope searchScope = GlobalSearchScope.allScope(project); PsiClass[] psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName(name, searchScope); if (psiClasses.length == 1) { return psiClasses[0]; } if (psiClasses.length > 1) { for (PsiClass pc : psiClasses) { PsiJavaFile psiJavaFile = (PsiJavaFile) pc.getContainingFile(); String packageName = psiJavaFile.getPackageName(); if (List.class.getPackage().getName().equals(packageName) || packageName.contains("io.xujiaji.xmvp")) { return pc; } } } return null; }
public static boolean processClassesByNames(Project project, final GlobalSearchScope scope, Collection<String> names, Processor<PsiClass> processor) { final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project); for (final String name : names) { ProgressIndicatorProvider.checkCanceled(); final PsiClass[] classes = MethodUsagesSearcher.resolveInReadAction(project, new Computable<PsiClass[]>() { @Override public PsiClass[] compute() { return cache.getClassesByName(name, scope); } }); for (PsiClass psiClass : classes) { ProgressIndicatorProvider.checkCanceled(); if (!processor.process(psiClass)) { return false; } } } return true; }
public MoveClassToModuleFix(String referenceName, Module currentModule, PsiDirectory root, PsiElement psiElement) { myReferenceName = referenceName; myCurrentModule = currentModule; mySourceRoot = root; final Project project = psiElement.getProject(); final PsiClass[] classes = PsiShortNamesCache.getInstance(project).getClassesByName(referenceName, GlobalSearchScope.allScope(project)); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); for (final PsiClass aClass : classes) { if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue; final PsiFile psiFile = aClass.getContainingFile(); if (!(psiFile instanceof PsiJavaFile)) continue; if (aClass.getQualifiedName() == null) continue; VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) continue; final Module classModule = fileIndex.getModuleForFile(virtualFile); if (classModule != null && classModule != currentModule && !ModuleRootManager.getInstance(currentModule).isDependsOn(classModule)) { myModules.put(aClass, classModule); } } }
@NotNull public Collection<PsiElement> findClassesForTest(@NotNull PsiElement element) { PsiClass klass = findSourceElement(element); if (klass == null) return Collections.emptySet(); GlobalSearchScope scope = getSearchScope(element, true); PsiShortNamesCache cache = PsiShortNamesCache.getInstance(element.getProject()); List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<Pair<? extends PsiNamedElement, Integer>>(); for (Pair<String, Integer> eachNameWithWeight : TestFinderHelper.collectPossibleClassNamesWithWeights(klass.getName())) { for (PsiClass eachClass : cache.getClassesByName(eachNameWithWeight.first, scope)) { if (isTestSubjectClass(eachClass)) { classesWithWeights.add(Pair.create(eachClass, eachNameWithWeight.second)); } } } return TestFinderHelper.getSortedElements(classesWithWeights, false); }
private boolean collectTests(PsiClass klass, Processor<Pair<? extends PsiNamedElement, Integer>> processor) { GlobalSearchScope scope = getSearchScope(klass, false); PsiShortNamesCache cache = PsiShortNamesCache.getInstance(klass.getProject()); String klassName = klass.getName(); Pattern pattern = Pattern.compile(".*" + StringUtil.escapeToRegexp(klassName) + ".*", Pattern.CASE_INSENSITIVE); HashSet<String> names = new HashSet<String>(); cache.getAllClassNames(names); final TestFrameworks frameworks = TestFrameworks.getInstance(); for (String eachName : names) { if (pattern.matcher(eachName).matches()) { for (PsiClass eachClass : cache.getClassesByName(eachName, scope)) { if (eachClass.isPhysical() && (frameworks.isTestClass(eachClass) || frameworks.isPotentialTestClass(eachClass))) { if (!processor.process(Pair.create(eachClass, TestFinderHelper.calcTestNameProximity(klassName, eachName)))) { return true; } } } } } return false; }
private void appendTestClass(PsiClass aClass, String testSuffix, final GlobalSearchScope moduleScope) { PsiShortNamesCache cache = PsiShortNamesCache.getInstance(aClass.getProject()); String klassName = aClass.getName(); Pattern pattern = Pattern.compile(".*" + klassName + ".*" + testSuffix); HashSet<String> names = new HashSet<String>(); cache.getAllClassNames(names); for (String eachName : names) { if (pattern.matcher(eachName).matches()) { for (PsiClass eachClass : cache.getClassesByName(eachName, moduleScope)) { if (TestFrameworks.getInstance().isTestClass(eachClass)) { myElements.add(eachClass); } } } } }
private ResolveResult[] resolveInner() { final String value = myValue.getStringValue(); if (value == null || value.length() <= 0) { return ResolveResult.EMPTY_ARRAY; } // Search for custom views with the same name as the declare styleable, such that // you can navigate from the XML styleable declaration to the corresponding custom view final PsiClass[] classes = PsiShortNamesCache.getInstance(myElement.getProject()) .getClassesByName(value, myFacet.getModule().getModuleWithDependenciesAndLibrariesScope(false)); if (classes.length == 0) { return ResolveResult.EMPTY_ARRAY; } final ResolveResult[] result = new ResolveResult[classes.length]; for (int i = 0; i < result.length; i++) { result[i] = new PsiElementResolveResult(classes[i]); } return result; }
private static boolean findClassByText(GrReferenceExpression ref) { final String text = ref.getText(); final int i = text.indexOf('<'); String className = i == -1 ? text : text.substring(0, i); PsiClass[] names = PsiShortNamesCache.getInstance(ref.getProject()).getClassesByName(className, ref.getResolveScope()); if (names.length > 0) return true; PsiFile file = ref.getContainingFile(); if (file instanceof GroovyFile) { GrImportStatement[] imports = ((GroovyFile)file).getImportStatements(); for (GrImportStatement anImport : imports) { if (className.equals(anImport.getImportedName())) return true; } } return false; }
@Override public String getTemplateString(@NotNull PsiElement element) { Project project = element.getProject(); final GlobalSearchScope resolveScope = element.getResolveScope(); PsiClass[] buildConfigClasses = PsiShortNamesCache.getInstance(project).getClassesByName("BuildConfig", resolveScope); String buildConfigDebug = "BuildConfig.DEBUG"; if (buildConfigClasses.length != 0) { // Get BuildConfig QualifiedName PsiClass buildConfig = buildConfigClasses[0]; String qualifiedName = buildConfig.getQualifiedName(); buildConfigDebug = qualifiedName + ".DEBUG"; } return "if (" + buildConfigDebug + ") " + getStaticPrefix(LOG, "d", element) + "($TAG$, $expr$)$END$"; }
ByItem(final Converter converter, final Map<String,Set<String>> globalFields, final Map<String,Set<String>> globalMethods, final List<TypeNameItemNamePair> pairFields, final List<TypeNameItemNamePair> pairMethods, final List<Item> locals, final GlobalSearchScope scope, final IdFilter filter) { this.converter = converter; final ByItemMaps maps = JavaItems.valuesByItem(locals, true); // locals byItem map contains an entry for ObjectItem this.localValues = maps.values; this.localMethods = maps.methods; this.globalFields = globalFields; this.globalMethods = globalMethods; this.pairFields = pairFields; this.pairMethods = pairMethods; this.psiCache = PsiShortNamesCache.getInstance(converter.project); this.scope = scope; this.filter = filter; }
private int[] prepareNameTrie(final String[] fieldNames, final String[] methodNames, final String[] packageNames) { pushScope("prepare lazy trie"); try { if (updateFuture.isCancelled()) return null; final String[] classNames = DumbService.getInstance(project).runReadActionInSmartMode(new Computable<String[]>() { @Override public String[] compute() { return PsiShortNamesCache.getInstance(project).getAllClassNames(); } }); if (updateFuture.isCancelled()) return null; final String[] allNames = concatenate(classNames,fieldNames,methodNames,packageNames); if (updateFuture.isCancelled()) return null; // there may be duplicates, but we don't particularly care Arrays.sort(allNames); return JavaTrie.makeTrieStructure(allNames); } finally { popScope(); } }
@NotNull private static String[] getAllClassNames(@NotNull final Project project) { return ApplicationManager.getApplication().runReadAction(new Computable<String[]>() { @Override public String[] compute() { final long modCount = PsiManager.getInstance(project).getModificationTracker().getJavaStructureModificationCount(); TimedReference<SoftReference<Pair<String[], Long>>> ref1 = project.getUserData(ALL_CLASS_NAMES_CACHE); SoftReference<Pair<String[], Long>> ref2 = ref1 == null ? null : ref1.get(); Pair<String[], Long> pair = ref2 == null ? null : ref2.get(); if (pair != null && pair.second.equals(modCount)) { return pair.first; } String[] names = PsiShortNamesCache.getInstance(project).getAllClassNames(); ref1 = new TimedReference<SoftReference<Pair<String[], Long>>>(null); ref1.set(new SoftReference<Pair<String[], Long>>(Pair.create(names, modCount))); project.putUserData(ALL_CLASS_NAMES_CACHE, ref1); return names; } }); }
private static Set<LookupElement> suggestQualifierItems(CompletionParameters parameters, PsiJavaCodeReferenceElement qualifier, ElementFilter filter) { String referenceName = qualifier.getReferenceName(); if (referenceName == null) { return Collections.emptySet(); } PrefixMatcher qMatcher = new CamelHumpMatcher(referenceName); Set<LookupElement> plainVariants = JavaSmartCompletionContributor.completeReference(qualifier, qualifier, filter, true, true, parameters, qMatcher); for (PsiClass aClass : PsiShortNamesCache.getInstance(qualifier.getProject()).getClassesByName(referenceName, qualifier.getResolveScope())) { plainVariants.add(JavaClassNameCompletionContributor.createClassLookupItem(aClass, true)); } if (!plainVariants.isEmpty()) { return plainVariants; } final Set<LookupElement> allClasses = new LinkedHashSet<LookupElement>(); JavaClassNameCompletionContributor.addAllClasses(parameters.withPosition(qualifier.getReferenceNameElement(), qualifier.getTextRange().getEndOffset()), true, qMatcher, new CollectConsumer<LookupElement>(allClasses)); return allClasses; }
@Override @NotNull public NavigationItem[] getItemsByName(String name, final String pattern, Project project, boolean includeNonProjectItems) { GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project); PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project); List<PsiMember> result = new ArrayList<PsiMember>(); for (PsiMethod method : cache.getMethodsByName(name, scope)) { if (!method.isConstructor() && isOpenable(method) && !hasSuperMethod(method)) { result.add(method); } } for (PsiField field : cache.getFieldsByName(name, scope)) { if (isOpenable(field)) { result.add(field); } } for (PsiClass aClass : cache.getClassesByName(name, scope)) { if (isOpenable(aClass)) { result.add(aClass); } } PsiMember[] array = result.toArray(new PsiMember[result.size()]); Arrays.sort(array, MyComparator.INSTANCE); return array; }
public void handle(final String fileName) { ApplicationManager.getApplication().invokeLater( new Runnable() { public void run() { ApplicationManager.getApplication().runReadAction(new Runnable() { public void run() { PsiFile[] foundFiles = PsiShortNamesCache.getInstance(project).getFilesByName(fileName); if (foundFiles.length == 0) { log.info("No file with name " + fileName + " found"); return; } if (foundFiles.length > 1) { log.warn("Found more than one file with name " + fileName); } PsiFile foundFile = foundFiles[0]; log.info("Found file " + foundFile.getName()); OpenFileDescriptor descriptor = new OpenFileDescriptor(project, foundFile.getVirtualFile()); descriptor.navigate(true); showBalloonPopup("Codebrag<br/>File opened successfully.<br/>", MessageType.INFO); } }); } } ); }
/** * Returns the most probable WebApps folder * @param project Project * @return String value */ private String getWebAppsFolder(Project project) { // Using the api to look for the web.xml PsiShortNamesCache namesCache = PsiShortNamesCache.getInstance(project); PsiFile[] webXML = namesCache.getFilesByName("web.xml"); if (webXML == null || webXML.length < 1) return ""; // Grab the first one that the api found PsiFile file = webXML[0]; // The parent folder is the "WEB-INF" folder PsiDirectory webInfFolder = file.getParent(); if (webInfFolder == null) return ""; // The parent folder to "WEB-INF" is the WebApps folder PsiDirectory webappFolder = webInfFolder.getParent(); if (webappFolder == null) return ""; // Folder found, returns it to the user VirtualFile virtualFile = webappFolder.getVirtualFile(); return virtualFile.getPresentableUrl(); }
public static boolean processClassesByNames(Project project, final GlobalSearchScope scope, Collection<String> names, Processor<PsiClass> processor) { final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project); for(final String name : names) { ProgressIndicatorProvider.checkCanceled(); final PsiClass[] classes = MethodUsagesSearcher.resolveInReadAction(project, new Computable<PsiClass[]>() { @Override public PsiClass[] compute() { return cache.getClassesByName(name, scope); } }); for(PsiClass psiClass : classes) { ProgressIndicatorProvider.checkCanceled(); if(!processor.process(psiClass)) { return false; } } } return true; }
@Override @NotNull public PsiFile[] getFilesByName(@NotNull String name) { Merger<PsiFile> merger = null; for(PsiShortNamesCache cache : myCaches) { PsiFile[] classes = cache.getFilesByName(name); if(classes.length != 0) { if(merger == null) { merger = new Merger<PsiFile>(); } merger.add(classes); } } PsiFile[] result = merger == null ? null : merger.getResult(); return result != null ? result : PsiFile.EMPTY_ARRAY; }
@Override @NotNull public PsiClass[] getClassesByName(@NotNull String name, @NotNull GlobalSearchScope scope) { Merger<PsiClass> merger = null; for(PsiShortNamesCache cache : myCaches) { PsiClass[] classes = cache.getClassesByName(name, scope); if(classes.length != 0) { if(merger == null) { merger = new Merger<PsiClass>(); } merger.add(classes); } } PsiClass[] result = merger == null ? null : merger.getResult(); return result != null ? result : PsiClass.EMPTY_ARRAY; }
@Override @NotNull public PsiMethod[] getMethodsByName(@NotNull String name, @NotNull GlobalSearchScope scope) { Merger<PsiMethod> merger = null; for(PsiShortNamesCache cache : myCaches) { PsiMethod[] methods = cache.getMethodsByName(name, scope); if(methods.length != 0) { if(merger == null) { merger = new Merger<PsiMethod>(); } merger.add(methods); } } PsiMethod[] result = merger == null ? null : merger.getResult(); return result == null ? PsiMethod.EMPTY_ARRAY : result; }
@Override @NotNull public PsiMethod[] getMethodsByNameIfNotMoreThan(@NonNls @NotNull final String name, @NotNull final GlobalSearchScope scope, final int maxCount) { Merger<PsiMethod> merger = null; for(PsiShortNamesCache cache : myCaches) { PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, maxCount); if(methods.length == maxCount) { return methods; } if(methods.length != 0) { if(merger == null) { merger = new Merger<PsiMethod>(); } merger.add(methods); } } PsiMethod[] result = merger == null ? null : merger.getResult(); return result == null ? PsiMethod.EMPTY_ARRAY : result; }
@NotNull @Override public PsiField[] getFieldsByNameIfNotMoreThan(@NonNls @NotNull String name, @NotNull GlobalSearchScope scope, int maxCount) { Merger<PsiField> merger = null; for(PsiShortNamesCache cache : myCaches) { PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(name, scope, maxCount); if(fields.length == maxCount) { return fields; } if(fields.length != 0) { if(merger == null) { merger = new Merger<PsiField>(); } merger.add(fields); } } PsiField[] result = merger == null ? null : merger.getResult(); return result == null ? PsiField.EMPTY_ARRAY : result; }
@Override @NotNull public PsiField[] getFieldsByName(@NotNull String name, @NotNull GlobalSearchScope scope) { Merger<PsiField> merger = null; for(PsiShortNamesCache cache : myCaches) { PsiField[] classes = cache.getFieldsByName(name, scope); if(classes.length != 0) { if(merger == null) { merger = new Merger<PsiField>(); } merger.add(classes); } } PsiField[] result = merger == null ? null : merger.getResult(); return result == null ? PsiField.EMPTY_ARRAY : result; }
@Override @NotNull public String[] getAllFieldNames() { Merger<String> merger = null; for(PsiShortNamesCache cache : myCaches) { String[] classes = cache.getAllFieldNames(); if(classes.length != 0) { if(merger == null) { merger = new Merger<String>(); } merger.add(classes); } } String[] result = merger == null ? null : merger.getResult(); return result == null ? ArrayUtil.EMPTY_STRING_ARRAY : result; }
@NotNull @Override protected List<PsiMethod> getMembersToImport(boolean applicableOnly) { final Project project = myMethodCall.getProject(); PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project); final PsiMethodCallExpression element = myMethodCall.getElement(); PsiReferenceExpression reference = element == null ? null : element.getMethodExpression(); String name = reference == null ? null : reference.getReferenceName(); if(name == null) { return Collections.emptyList(); } final StaticMembersProcessor<PsiMethod> processor = new MyStaticMethodProcessor(element); cache.processMethodsWithName(name, element.getResolveScope(), processor); return processor.getMembersToImport(applicableOnly); }
public static boolean isFileExists(AnActionEvent e, String name) { Project project = e.getProject(); GlobalSearchScope searchScope = GlobalSearchScope.allScope(project); PsiClass[] psiClasses = new PsiClass[0]; try { psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName(name, searchScope); }catch (Exception ignored) { ignored.printStackTrace(); } return psiClasses.length > 0; }
public static Project processClassNames(final Project project, final GlobalSearchScope scope, final Consumer<String> consumer) { final ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator(); MethodUsagesSearcher.resolveInReadAction(project, new Computable<Void>() { @Override public Void compute() { PsiShortNamesCache.getInstance(project).processAllClassNames(new Processor<String>() { int i = 0; @Override public boolean process(String s) { if (indicator != null && i++ % 512 == 0) { indicator.checkCanceled(); } consumer.consume(s); return true; } }, scope, IdFilter.getProjectIdFilter(project, true)); return null; } }); if (indicator != null) { indicator.checkCanceled(); } return project; }
@Override @NotNull public PsiFile[] getFilesByName(@NotNull String name) { Merger<PsiFile> merger = null; for (PsiShortNamesCache cache : myCaches) { PsiFile[] classes = cache.getFilesByName(name); if (classes.length != 0) { if (merger == null) merger = new Merger<PsiFile>(); merger.add(classes); } } PsiFile[] result = merger == null ? null : merger.getResult(); return result != null ? result : PsiFile.EMPTY_ARRAY; }
@Override @NotNull public String[] getAllFileNames() { Merger<String> merger = new Merger<String>(); for (PsiShortNamesCache cache : myCaches) { merger.add(cache.getAllFileNames()); } String[] result = merger.getResult(); return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY; }
@Override @NotNull public PsiClass[] getClassesByName(@NotNull String name, @NotNull GlobalSearchScope scope) { Merger<PsiClass> merger = null; for (PsiShortNamesCache cache : myCaches) { PsiClass[] classes = cache.getClassesByName(name, scope); if (classes.length != 0) { if (merger == null) merger = new Merger<PsiClass>(); merger.add(classes); } } PsiClass[] result = merger == null ? null : merger.getResult(); return result != null ? result : PsiClass.EMPTY_ARRAY; }
@Override @NotNull public String[] getAllClassNames() { Merger<String> merger = new Merger<String>(); for (PsiShortNamesCache cache : myCaches) { String[] names = cache.getAllClassNames(); merger.add(names); } String[] result = merger.getResult(); return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY; }
@Override public boolean processAllClassNames(Processor<String> processor) { CommonProcessors.UniqueProcessor<String> uniqueProcessor = new CommonProcessors.UniqueProcessor<String>(processor); for (PsiShortNamesCache cache : myCaches) { if (!cache.processAllClassNames(uniqueProcessor)) { return false; } } return true; }
@Override public boolean processAllClassNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) { for (PsiShortNamesCache cache : myCaches) { if (!cache.processAllClassNames(processor, scope, filter)) { return false; } } return true; }
@Override public boolean processAllMethodNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) { for (PsiShortNamesCache cache : myCaches) { if (!cache.processAllMethodNames(processor, scope, filter)) { return false; } } return true; }