Search in sources :

Example 1 with JSTypeSource

use of com.intellij.lang.javascript.psi.types.JSTypeSource in project intellij-plugins by JetBrains.

the class AngularJSIndexingHandler method addTypeFromResolveResult.

@Override
public boolean addTypeFromResolveResult(JSTypeEvaluator evaluator, PsiElement resolveResult, boolean hasSomeType) {
    if (!AngularIndexUtil.hasAngularJS(resolveResult.getProject()))
        return false;
    if (resolveResult instanceof JSDefinitionExpression && resolveResult.getLanguage() instanceof AngularJSLanguage) {
        final PsiElement resolveParent = resolveResult.getParent();
        if (resolveParent instanceof AngularJSAsExpression) {
            final String name = resolveParent.getFirstChild().getText();
            final JSTypeSource source = JSTypeSourceFactory.createTypeSource(resolveResult);
            final JSType type = JSNamedType.createType(name, source, JSContext.INSTANCE);
            evaluator.addType(type, resolveResult);
            return true;
        }
    }
    if (resolveResult instanceof JSVariable && resolveResult.getLanguage() instanceof AngularJSLanguage) {
        PsiElement resolveParent = resolveResult.getParent().getParent();
        if (resolveParent instanceof AngularJSRepeatExpression) {
            if (calculateRepeatParameterType(evaluator, (AngularJSRepeatExpression) resolveParent)) {
                return true;
            }
        }
    }
    if (resolveResult instanceof JSParameter && evaluator.isFromCurrentFile(resolveResult) && isInjectable(resolveResult)) {
        final String name = ((JSParameter) resolveResult).getName();
        final JSTypeSource source = JSTypeSourceFactory.createTypeSource(resolveResult);
        final JSType type = JSNamedType.createType(name, source, JSContext.UNKNOWN);
        evaluator.addType(type, resolveResult);
    }
    return false;
}
Also used : JSTypeSource(com.intellij.lang.javascript.psi.types.JSTypeSource) AngularJSRepeatExpression(org.angularjs.lang.psi.AngularJSRepeatExpression) AngularJSLanguage(org.angularjs.lang.AngularJSLanguage) PsiElement(com.intellij.psi.PsiElement) AngularJSAsExpression(org.angularjs.lang.psi.AngularJSAsExpression)

Example 2 with JSTypeSource

use of com.intellij.lang.javascript.psi.types.JSTypeSource in project oxy-template-support-plugin by mutant-industries.

the class InnerJsTypeEvaluator method getMacroFirstParameterType.

// -------------------------------------------------------------------
@NotNull
private JSType getMacroFirstParameterType(@NotNull JSProperty macro) {
    assert OxyTemplateIndexUtil.isMacro(macro);
    JSTypeSource typeSource = JSTypeSourceFactory.createTypeSource(myContext.targetFile, true);
    List<JSRecordTypeImpl.TypeMember> members = new LinkedList<>();
    for (MacroParamDescriptor paramDescriptor : MacroParamHelper.getJsMacroParamSuggestions(macro, false)) {
        if (!paramDescriptor.isDocumented()) {
            continue;
        }
        JSRecordTypeImpl.PropertySignature signature = new JSRecordTypeImpl.PropertySignatureImpl(paramDescriptor.getName(), paramDescriptor.getType(), !paramDescriptor.isRequired());
        members.add(signature);
    }
    JSRecordTypeImpl type = new JSRecordTypeImpl(typeSource, ImmutableList.copyOf(members));
    type.accept(new SimplifiedClassNameResolver(myContext.targetFile));
    return type;
}
Also used : JSTypeSource(com.intellij.lang.javascript.psi.types.JSTypeSource) MacroParamDescriptor(ool.intellij.plugin.psi.macro.param.descriptor.MacroParamDescriptor) LinkedList(java.util.LinkedList) JSRecordTypeImpl(com.intellij.lang.javascript.psi.types.JSRecordTypeImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JSTypeSource

use of com.intellij.lang.javascript.psi.types.JSTypeSource in project oxy-template-support-plugin by mutant-industries.

the class GlobalVariableTypeProvider method compute.

@Nullable
@Override
public Result<JSType> compute() {
    List<PsiElement> cacheDependencies = new LinkedList<>();
    cacheDependencies.add(expression);
    // controllers
    if (CONTROLLERS_GLOBAL_VARIABLE_NAME.equals(name)) {
        Project project = expression.getProject();
        GlobalSearchScope allScope = ProjectScope.getAllScope(project);
        GlobalSearchScope projectScope = ProjectScope.getProjectScope(project);
        List<JSRecordTypeImpl.TypeMember> members = new LinkedList<>();
        JSTypeSource typeSource = JSTypeSourceFactory.createTypeSource(expression, true);
        PsiClass controller;
        PsiClass controllerAnnotation = JavaPsiFacade.getInstance(project).findClass(CONTROLLER_FQN, allScope);
        if (controllerAnnotation != null && controllerAnnotation.isAnnotationType()) {
            for (PsiReference controllerAnnotationReference : ReferencesSearch.search(controllerAnnotation, projectScope).findAll()) {
                PsiElement reference = controllerAnnotationReference.getElement();
                if ((controller = PsiTreeUtil.getParentOfType(reference, PsiClass.class)) != null && controller.getQualifiedName() != null) {
                    JSTypeSource source = JSTypeSourceFactory.createTypeSource(controller, true);
                    JSType jsType = JSTypeUtils.createType(controller.getQualifiedName(), source);
                    JSRecordTypeImpl.PropertySignature signature = new JSRecordTypeImpl.PropertySignatureImpl(controller.getName(), jsType, false);
                    members.add(signature);
                    cacheDependencies.add(controller);
                }
            }
        }
        Collections.addAll(cacheDependencies, FilenameIndex.getFilesByName(project, CONTROLLERS_BEANS_FILE_NAME, ProjectScope.getProjectScope(project)));
        return Result.create(new JSRecordTypeImpl(typeSource, ImmutableList.copyOf(members)), cacheDependencies);
    }
    PsiReturnStatement returnStatement;
    PsiClass aClass;
    PsiElement elementAt;
    // global model provider
    if ((returnStatement = PsiTreeUtil.getParentOfType(expression, PsiReturnStatement.class)) != null && (aClass = PsiTreeUtil.getParentOfType(returnStatement, PsiClass.class)) != null && InheritanceUtil.isInheritor(aClass, GLOBAL_MODEL_PROVIDER_FQN)) {
        return Result.create(getTypeFromProvider(aClass), cacheDependencies);
    } else // model provider registry
    {
        elementAt = expression;
        while (elementAt != null && !((elementAt = elementAt.getNextSibling()) instanceof PsiExpression)) ;
        if (elementAt instanceof PsiNewExpression) {
            PsiJavaCodeReferenceElement providerClassReference = PsiTreeUtil.getNextSiblingOfType(elementAt.getFirstChild(), PsiJavaCodeReferenceElement.class);
            PsiElement providerClass;
            if (providerClassReference != null && (providerClass = providerClassReference.resolve()) instanceof PsiClass) {
                aClass = (PsiClass) providerClass;
            } else if ((aClass = PsiTreeUtil.getNextSiblingOfType(elementAt.getFirstChild(), PsiAnonymousClass.class)) == null) {
                return Result.create(null, expression);
            }
            cacheDependencies.add(aClass);
            return Result.create(getTypeFromProvider(aClass), cacheDependencies);
        } else if (elementAt != null) {
            PsiType type = ((PsiExpression) elementAt).getType();
            if (type != null && (aClass = JavaPsiFacade.getInstance(expression.getProject()).findClass(type.getCanonicalText(), ProjectScope.getAllScope(expression.getProject()))) != null) {
                return Result.create(getTypeFromProvider(aClass), cacheDependencies);
            }
        }
    }
    return Result.create(null, cacheDependencies);
}
Also used : JSTypeSource(com.intellij.lang.javascript.psi.types.JSTypeSource) JSType(com.intellij.lang.javascript.psi.JSType) PsiExpression(com.intellij.psi.PsiExpression) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) LinkedList(java.util.LinkedList) PsiNewExpression(com.intellij.psi.PsiNewExpression) JSRecordTypeImpl(com.intellij.lang.javascript.psi.types.JSRecordTypeImpl) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiReturnStatement(com.intellij.psi.PsiReturnStatement) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JSTypeSource (com.intellij.lang.javascript.psi.types.JSTypeSource)3 JSRecordTypeImpl (com.intellij.lang.javascript.psi.types.JSRecordTypeImpl)2 PsiElement (com.intellij.psi.PsiElement)2 LinkedList (java.util.LinkedList)2 JSType (com.intellij.lang.javascript.psi.JSType)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)1 PsiNewExpression (com.intellij.psi.PsiNewExpression)1 PsiReference (com.intellij.psi.PsiReference)1 PsiReturnStatement (com.intellij.psi.PsiReturnStatement)1 PsiType (com.intellij.psi.PsiType)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 MacroParamDescriptor (ool.intellij.plugin.psi.macro.param.descriptor.MacroParamDescriptor)1 AngularJSLanguage (org.angularjs.lang.AngularJSLanguage)1 AngularJSAsExpression (org.angularjs.lang.psi.AngularJSAsExpression)1 AngularJSRepeatExpression (org.angularjs.lang.psi.AngularJSRepeatExpression)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1