Search in sources :

Example 1 with AdditionalIndexedRootsScope

use of com.intellij.util.indexing.AdditionalIndexedRootsScope in project intellij-community by JetBrains.

the class IndexedRelevantResource method getResources.

public static <K, V extends Comparable> List<IndexedRelevantResource<K, V>> getResources(ID<K, V> indexId, final K key, @Nullable final Module module, @NotNull Project project, @Nullable final GlobalSearchScope additionalScope) {
    if (project.isDefault())
        return Collections.emptyList();
    final ArrayList<IndexedRelevantResource<K, V>> resources = new ArrayList<>();
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    FileBasedIndex.getInstance().processValues(indexId, key, null, new FileBasedIndex.ValueProcessor<V>() {

        @Override
        public boolean process(VirtualFile file, V value) {
            ResourceRelevance relevance = ResourceRelevance.getRelevance(file, module, fileIndex, additionalScope);
            resources.add(new IndexedRelevantResource<>(file, key, value, relevance));
            return true;
        }
    }, new AdditionalIndexedRootsScope(GlobalSearchScope.allScope(project)));
    return resources;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) ArrayList(java.util.ArrayList) AdditionalIndexedRootsScope(com.intellij.util.indexing.AdditionalIndexedRootsScope) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 2 with AdditionalIndexedRootsScope

use of com.intellij.util.indexing.AdditionalIndexedRootsScope in project intellij-plugins by JetBrains.

the class ActionScriptClassResolver method doFindClassByQName.

protected PsiElement doFindClassByQName(@NotNull String link, final JavaScriptIndex index, GlobalSearchScope searchScope, boolean allowFileLocalSymbols, @NotNull DialectOptionHolder dialect) {
    Project project = index.getProject();
    boolean clazzShouldBeTakenFromOurLibrary = OBJECT_CLASS_NAME.equals(link) || "Arguments".equals(link);
    if (clazzShouldBeTakenFromOurLibrary && !(searchScope instanceof AdditionalIndexedRootsScope)) {
        // object from swf do not contain necessary members!
        searchScope = new AdditionalIndexedRootsScope(searchScope, JSIndexedRootProvider.class);
    }
    final Collection<JSQualifiedNamedElement> candidates = StubIndex.getElements(JSQualifiedElementIndex.KEY, link.hashCode(), project, searchScope, JSQualifiedNamedElement.class);
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    JSQualifiedNamedElement resultFromSourceContent = null;
    JSQualifiedNamedElement resultFromLibraries = null;
    long resultFromLibrariesTimestamp = 0;
    for (JSQualifiedNamedElement classCandidate : candidates) {
        if (!(classCandidate instanceof JSQualifiedNamedElement))
            continue;
        if (JSResolveUtil.isConstructorFunction(classCandidate))
            continue;
        JSQualifiedNamedElement clazz = classCandidate;
        if (link.equals(clazz.getQualifiedName())) {
            PsiFile file = clazz.getContainingFile();
            if (!file.getLanguage().isKindOf(JavaScriptSupportLoader.ECMA_SCRIPT_L4))
                continue;
            VirtualFile vFile = file.getVirtualFile();
            if (clazzShouldBeTakenFromOurLibrary && // object from swf do not contain necessary members!
            !JavaScriptIndex.ECMASCRIPT_JS2.equals(vFile.getName())) {
                continue;
            }
            if (!allowFileLocalSymbols && JSResolveUtil.isFileLocalSymbol(clazz)) {
                continue;
            }
            if (projectFileIndex.isInSourceContent(vFile)) {
                // the absolute preference is for classes from sources
                resultFromSourceContent = clazz;
                continue;
            }
            // choose the right class in the same way as compiler does: with the latest timestamp in catalog.xml file
            if (resultFromLibraries == null) {
                resultFromLibraries = clazz;
            // do not initialize resultFromLibrariesTimestamp here, it is expensive and may be not required if only 1 candidate
            } else if (JSCommonTypeNames.VECTOR_CLASS_NAME.equals(link)) {
                if (clazz instanceof JSClass && resultFromLibraries instanceof JSClass && ((JSClass) clazz).getFunctions().length > ((JSClass) resultFromLibraries).getFunctions().length) {
                    resultFromLibraries = clazz;
                }
            } else {
                if (resultFromLibrariesTimestamp == 0) {
                    // was not initialized yet
                    resultFromLibrariesTimestamp = getResolveResultTimestamp(resultFromLibraries);
                }
                final long classTimestamp = getResolveResultTimestamp(clazz);
                if (classTimestamp > resultFromLibrariesTimestamp) {
                    resultFromLibraries = clazz;
                    resultFromLibrariesTimestamp = classTimestamp;
                }
            }
        }
    }
    PsiElement result = resultFromSourceContent != null ? resultFromSourceContent : resultFromLibraries;
    if (result == null) {
        String className = link.substring(link.lastIndexOf('.') + 1);
        if (className.length() > 0 && !isBuiltInClassName(className) && (Character.isLetter(className.charAt(0)) || '_' == className.charAt(0))) {
            // TODO optimization, remove when packages will be properly handled
            result = findClassByQNameViaHelper(link, project, className, searchScope);
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) JSIndexedRootProvider(com.intellij.lang.javascript.index.JSIndexedRootProvider) AdditionalIndexedRootsScope(com.intellij.util.indexing.AdditionalIndexedRootsScope) PsiFile(com.intellij.psi.PsiFile) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 AdditionalIndexedRootsScope (com.intellij.util.indexing.AdditionalIndexedRootsScope)2 JSIndexedRootProvider (com.intellij.lang.javascript.index.JSIndexedRootProvider)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 FileBasedIndex (com.intellij.util.indexing.FileBasedIndex)1 ArrayList (java.util.ArrayList)1