Search in sources :

Example 6 with JSQualifiedNamedElement

use of com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement in project intellij-plugins by JetBrains.

the class AddImportECMAScriptClassOrFunctionAction method getCandidates.

private Collection<JSQualifiedNamedElement> getCandidates(Editor editor, PsiFile file) {
    final Collection<JSQualifiedNamedElement> candidates;
    if (myReference instanceof JSReferenceExpression && ((JSReferenceExpression) myReference).getQualifier() == null) {
        Collection<JSQualifiedNamedElement> c = getCandidates(editor, file, myReference.getCanonicalText());
        filterCandidates(c);
        candidates = new THashSet<>(c, JSPsiImplUtils.QUALIFIED_NAME_HASHING_STRATEGY);
    } else {
        JSQualifiedNamedElement invalidResult = null;
        for (ResolveResult r : myReference.multiResolve(false)) {
            PsiElement element = r.getElement();
            if (element instanceof JSQualifiedNamedElement) {
                invalidResult = (JSQualifiedNamedElement) element;
            }
        }
        if (invalidResult != null) {
            if (myReference.getElement().getParent() instanceof JSNewExpression && invalidResult instanceof JSFunction && ((JSFunction) invalidResult).isConstructor()) {
                invalidResult = (JSClass) invalidResult.getParent();
            }
            candidates = new SmartList<>();
            candidates.add(invalidResult);
        } else {
            candidates = Collections.emptyList();
        }
    }
    return candidates;
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) JSNewExpression(com.intellij.lang.javascript.psi.JSNewExpression)

Example 7 with JSQualifiedNamedElement

use of com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement in project intellij-plugins by JetBrains.

the class FlexSchemaHandler method getAvailableNamespaces.

@NotNull
@Override
public Set<String> getAvailableNamespaces(@NotNull XmlFile _file, @Nullable @NonNls final String tagName) {
    // tagName == null => tag name completion
    // tagName != null => guess namespace of unresolved tag
    PsiFile originalFile = _file.getOriginalFile();
    if (originalFile instanceof XmlFile)
        _file = (XmlFile) originalFile;
    final XmlFile file = _file;
    final Project project = file.getProject();
    final Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(file.getVirtualFile());
    final Collection<String> illegalNamespaces = getIllegalNamespaces(file);
    final Set<String> result = new THashSet<>();
    final Set<String> componentsThatHaveNotPackageBackedNamespace = new THashSet<>();
    for (final String namespace : CodeContextHolder.getInstance(project).getNamespaces(module)) {
        if (!CodeContext.isPackageBackedNamespace(namespace) && !illegalNamespaces.contains(namespace)) {
            // package backed namespaces will be added later from JSPackageIndex
            if (tagName == null) {
                result.add(namespace);
            } else {
                final XmlElementDescriptor descriptor = CodeContext.getContext(namespace, module).getElementDescriptor(tagName, (XmlTag) null);
                if (descriptor != null) {
                    result.add(namespace);
                    componentsThatHaveNotPackageBackedNamespace.add(descriptor.getQualifiedName());
                }
            }
        }
    }
    if (tagName == null && !illegalNamespaces.contains(JavaScriptSupportLoader.MXML_URI)) {
        result.add(JavaScriptSupportLoader.MXML_URI);
    }
    if (XmlBackedJSClassImpl.SCRIPT_TAG_NAME.equals(tagName) || "Style".equals(tagName))
        return result;
    if (DumbService.isDumb(project))
        return result;
    if (tagName == null) {
        FileBasedIndex.getInstance().processAllKeys(JSPackageIndex.INDEX_ID, packageName -> {
            result.add(StringUtil.isEmpty(packageName) ? "*" : packageName + ".*");
            return true;
        }, project);
    } else {
        final GlobalSearchScope scopeWithLibs = module != null ? GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module) : GlobalSearchScope.allScope(project);
        for (JSQualifiedNamedElement element : JSResolveUtil.findElementsByName(tagName, project, scopeWithLibs, false)) {
            if (element instanceof JSClass && CodeContext.hasDefaultConstructor((JSClass) element)) {
                final String packageName = StringUtil.getPackageName(element.getQualifiedName());
                if (!componentsThatHaveNotPackageBackedNamespace.contains(StringUtil.getQualifiedName(packageName, tagName))) {
                    result.add(StringUtil.isEmpty(packageName) ? "*" : packageName + ".*");
                }
            }
        }
    }
    final GlobalSearchScope scopeWithoutLibs = module != null ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.allScope(project);
    // packages that contain *.mxml files and do not contain *.as are not retrieved from JSPackageIndex
    FlexResolveHelper.processAllMxmlAndFxgFiles(scopeWithoutLibs, project, new FlexResolveHelper.MxmlAndFxgFilesProcessor() {

        public void addDependency(final PsiDirectory directory) {
        }

        public boolean processFile(final VirtualFile file, final VirtualFile root) {
            if (tagName == null || tagName.equals(file.getNameWithoutExtension())) {
                final String packageName = VfsUtilCore.getRelativePath(file.getParent(), root, '.');
                if (packageName != null && (tagName == null || !componentsThatHaveNotPackageBackedNamespace.contains(StringUtil.getQualifiedName(packageName, tagName)))) {
                    result.add(StringUtil.isEmpty(packageName) ? "*" : packageName + ".*");
                }
            }
            return true;
        }
    }, tagName);
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) FlexResolveHelper(com.intellij.javascript.flex.resolve.FlexResolveHelper) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with JSQualifiedNamedElement

use of com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement in project intellij-plugins by JetBrains.

the class FlexCssPropertyDescriptor method getDeclarations.

@NotNull
@Override
public PsiElement[] getDeclarations(@NotNull PsiElement context) {
    Map<PsiElement, PairInfo> navElement2pairInfo = new HashMap<>();
    final Project project = context.getProject();
    GlobalSearchScope scope = FlexCssUtil.getResolveScope(context);
    Set<JSClass> visited = ContainerUtil.newLinkedHashSet();
    for (String className : myClassNames) {
        Collection<JSQualifiedNamedElement> candidates = StubIndex.getElements(JSQualifiedElementIndex.KEY, className.hashCode(), project, scope, JSQualifiedNamedElement.class);
        findStyleAttributes(candidates, visited, navElement2pairInfo);
        // search in MXML files
        PsiElement jsClass = ActionScriptClassResolver.findClassByQNameStatic(className, scope);
        if (jsClass instanceof JSClass) {
            findStyleAttributesInClassOrSuper((JSClass) jsClass, visited, navElement2pairInfo);
        }
    }
    Set<JSFile> visitedFiles = ContainerUtil.newLinkedHashSet();
    for (String fileName : myFileNames) {
        Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, fileName, scope);
        for (final VirtualFile file : files) {
            PsiFile psiFile = ReadAction.compute(() -> PsiManager.getInstance(project).findFile(file));
            if (psiFile instanceof JSFile) {
                findStyleAttributesInFile((JSFile) psiFile, visitedFiles, navElement2pairInfo);
            }
        }
    }
    Set<PsiElement> navPairs = navElement2pairInfo.keySet();
    Map<String, PsiElement> qName2ResultElement = new HashMap<>();
    for (PsiElement navPair : navPairs) {
        PairInfo pairInfo = navElement2pairInfo.get(navPair);
        String jsClassQName = pairInfo.myJsClassQName;
        PsiElement navPairInOtherClassWithSameQName = jsClassQName != null ? qName2ResultElement.get(jsClassQName) : null;
        if (navPairInOtherClassWithSameQName == null || navPairInOtherClassWithSameQName == navElement2pairInfo.get(navPairInOtherClassWithSameQName) && pairInfo.myPair != navPair) {
            qName2ResultElement.put(jsClassQName, navPair);
        }
    }
    Collection<PsiElement> result = qName2ResultElement.values();
    return PsiUtilCore.toPsiElementArray(result);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) JSFile(com.intellij.lang.javascript.psi.JSFile) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with JSQualifiedNamedElement

use of com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement in project intellij-plugins by JetBrains.

the class FlexUnitTestFinder method findTestsForClass.

@NotNull
public Collection<PsiElement> findTestsForClass(@NotNull final PsiElement element) {
    final VirtualFile file = element.getContainingFile().getVirtualFile();
    final JSClass jsClass = findSourceElement(element);
    final String className = jsClass == null ? null : jsClass.getName();
    final Pair<Module, FlexUnitSupport> moduleAndSupport = FlexUnitSupport.getModuleAndSupport(element);
    final Module module = moduleAndSupport == null ? null : moduleAndSupport.first;
    final FlexUnitSupport flexUnitSupport = moduleAndSupport == null ? null : moduleAndSupport.second;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex();
    if (className == null || module == null || flexUnitSupport == null || (file != null && fileIndex.isInTestSourceContent(file))) {
        return Collections.emptyList();
    }
    final Collection<String> allNames = StubIndex.getInstance().getAllKeys(JSNameIndex.KEY, element.getProject());
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependentsScope(module);
    final List<Pair<? extends PsiNamedElement, Integer>> classesWithProximity = new ArrayList<>();
    for (final String possibleTestName : allNames) {
        if (possibleTestName.contains(className)) {
            for (final JSQualifiedNamedElement jsElement : JSResolveUtil.findElementsByName(possibleTestName, element.getProject(), scope)) {
                final VirtualFile f = jsElement.getContainingFile().getVirtualFile();
                final boolean inTestSource = f != null && fileIndex.isInTestSourceContent(f);
                if (jsElement instanceof JSClass && jsElement != jsClass && ((inTestSource && possibleTestName.contains("Test")) || flexUnitSupport.isTestClass((JSClass) jsElement, true))) {
                    classesWithProximity.add(Pair.create(jsElement, TestFinderHelper.calcTestNameProximity(className, jsElement.getName())));
                }
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithProximity, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with JSQualifiedNamedElement

use of com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement in project intellij-plugins by JetBrains.

the class FlexUnitTestFinder method findClassesForTest.

@NotNull
public Collection<PsiElement> findClassesForTest(@NotNull final PsiElement element) {
    final JSClass jsClass = findSourceElement(element);
    final String className = jsClass == null ? null : jsClass.getName();
    final Pair<Module, FlexUnitSupport> moduleAndSupport = FlexUnitSupport.getModuleAndSupport(element);
    final Module module = moduleAndSupport == null ? null : moduleAndSupport.first;
    final FlexUnitSupport flexUnitSupport = moduleAndSupport == null ? null : moduleAndSupport.second;
    if (className == null || module == null || flexUnitSupport == null) {
        return Collections.emptyList();
    }
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesScope(module).intersectWith(GlobalSearchScopes.projectProductionScope(module.getProject()));
    final List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<>();
    for (Pair<String, Integer> nameWithWeight : TestFinderHelper.collectPossibleClassNamesWithWeights(className)) {
        for (final JSQualifiedNamedElement jsElement : JSResolveUtil.findElementsByName(nameWithWeight.first, module.getProject(), scope)) {
            if (jsElement instanceof JSClass && jsElement != jsClass && !((JSClass) jsElement).isInterface() && !flexUnitSupport.isTestClass((JSClass) jsElement, true)) {
                classesWithWeights.add(Pair.create(jsElement, nameWithWeight.second));
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithWeights, false);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)27 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)13 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiFile (com.intellij.psi.PsiFile)8 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)8 JSFile (com.intellij.lang.javascript.psi.JSFile)4 Project (com.intellij.openapi.project.Project)4 XmlFile (com.intellij.psi.xml.XmlFile)4 Nullable (org.jetbrains.annotations.Nullable)4 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)3 Module (com.intellij.openapi.module.Module)3 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 ArrayList (java.util.ArrayList)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)2 JSFunction (com.intellij.lang.javascript.psi.JSFunction)2 Attachment (com.intellij.openapi.diagnostic.Attachment)2