Search in sources :

Example 1 with JSExpression

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

the class ActionScriptAccessibilityProcessingHandler method acceptsForMembersVisibility.

@Override
protected boolean acceptsForMembersVisibility(@NotNull JSPsiElementBase element, @NotNull SinkResolveProcessor resolveProcessor) {
    if (!(element instanceof JSAttributeListOwner))
        return true;
    final JSAttributeList attributeList = ((JSAttributeListOwner) element).getAttributeList();
    if (JSResolveUtil.getClassOfContext(place) != JSResolveUtil.getClassOfContext(element)) {
        if (!acceptPrivateMembers) {
            if (attributeList != null && attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE) {
                resolveProcessor.addPossibleCandidateResult(element, JSResolveResult.PRIVATE_MEMBER_IS_NOT_ACCESSIBLE);
                return false;
            }
        }
        if (!acceptProtectedMembers) {
            if (attributeList != null && attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
                // we are resolving in context of the class or element within context of the class
                if ((myClassScopeTypeName != null || isParentClassContext(element))) {
                    resolveProcessor.addPossibleCandidateResult(element, JSResolveResult.PROTECTED_MEMBER_IS_NOT_ACCESSIBLE);
                    return false;
                }
            // if element / context out of class then protected element is ok due to includes
            }
        }
    }
    PsiElement elt = JSResolveUtil.findParent(element);
    if (processStatics) {
        if ((attributeList == null || !attributeList.hasModifier(JSAttributeList.ModifierType.STATIC))) {
            if (JSResolveUtil.PROTOTYPE_FIELD_NAME.equals(resolveProcessor.getName()))
                return true;
            resolveProcessor.addPossibleCandidateResult(element, JSResolveResult.INSTANCE_MEMBER_INACCESSIBLE);
            return false;
        }
        if (myTypeName != null && elt instanceof JSClass && !myTypeName.equals(((JSClass) elt).getQualifiedName())) {
            // static members are inherited in TypeScript classes
            resolveProcessor.addPossibleCandidateResult(element, JSResolveResult.STATIC_MEMBER_INACCESSIBLE);
            return false;
        }
    } else if (myClassDeclarationStarted && !allowUnqualifiedStaticsFromInstance) {
        // ActionScript only?
        if (attributeList != null && attributeList.hasModifier(JSAttributeList.ModifierType.STATIC)) {
            boolean referencingClass = false;
            if (place instanceof JSReferenceExpression) {
                JSExpression qualifier = ((JSReferenceExpression) place).getQualifier();
                if (qualifier instanceof JSReferenceExpression) {
                    JSElement expression = JSSymbolUtil.calcRefExprValue((JSReferenceExpression) qualifier);
                    if (expression instanceof JSReferenceExpression) {
                        for (ResolveResult r : ((JSReferenceExpression) expression).multiResolve(false)) {
                            PsiElement rElement = r.getElement();
                            if (rElement instanceof JSClass) {
                                referencingClass = true;
                                break;
                            }
                        }
                    }
                }
            }
            if (!referencingClass) {
                resolveProcessor.addPossibleCandidateResult(element, JSResolveResult.STATIC_MEMBER_INACCESSIBLE);
                return false;
            }
        }
    }
    if (processActionScriptNotAllowedNsAttributes(element, resolveProcessor, attributeList))
        return false;
    return true;
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSElement(com.intellij.lang.javascript.psi.JSElement) JSAttributeListOwner(com.intellij.lang.javascript.psi.ecmal4.JSAttributeListOwner) JSExpression(com.intellij.lang.javascript.psi.JSExpression) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement)

Example 2 with JSExpression

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

the class FlexXmlBackedClassesIndex method getIndexer.

@Override
@NotNull
public DataIndexer<String, Void, FileContent> getIndexer() {
    return new DataIndexer<String, Void, FileContent>() {

        @Override
        @NotNull
        public Map<String, Void> map(@NotNull FileContent inputData) {
            final XmlFile file = (XmlFile) inputData.getPsiFile();
            final Map<String, Void> result = new HashMap<>();
            for (JSClass clazz : XmlBackedJSClassImpl.getClasses(file)) {
                JSReferenceList supers = getSupers(clazz);
                if (supers != null) {
                    final JSExpression[] expressions = supers.getExpressions();
                    for (JSExpression expr : expressions) {
                        String s = expr instanceof JSReferenceExpression ? ((JSReferenceExpression) expr).getReferenceName() : null;
                        if (s != null)
                            result.put(s, null);
                    }
                }
            }
            return result;
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) HashMap(com.intellij.util.containers.HashMap) JSExpression(com.intellij.lang.javascript.psi.JSExpression) NotNull(org.jetbrains.annotations.NotNull) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSReferenceList(com.intellij.lang.javascript.psi.ecmal4.JSReferenceList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JSExpression

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

the class JavaContext method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    Module module;
    PsiElement element = parameters.getOriginalFile().getViewProvider().findElementAt(parameters.getOffset() - 1, OxyTemplateInnerJs.INSTANCE);
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT && (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    while (element != null && element.getNode().getElementType() != JSTokenTypes.DOT) {
        element = element.getLastChild();
        if (element instanceof PsiErrorElement) {
            element = element.getPrevSibling();
        }
    }
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT || (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    if (!(element instanceof JSExpression) || (module = ModuleUtilCore.findModuleForPsiElement(parameters.getOriginalFile())) == null) {
        return;
    }
    JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
    JSTypeEvaluator.evaluateTypes((JSExpression) element, parameters.getOriginalFile().getViewProvider().getPsi(OxyTemplateInnerJs.INSTANCE), typeProcessor);
    JSType type = typeProcessor.getType();
    List<String> possibleJavaTypes = new LinkedList<>();
    if (type != null) {
        if (type instanceof JSCompositeTypeImpl) {
            possibleJavaTypes.addAll(((JSCompositeTypeImpl) type).getTypes().stream().filter(jsType -> jsType instanceof JSTypeImpl).map(JSType::getTypeText).collect(Collectors.toList()));
        } else if (type instanceof JSTypeImpl) {
            possibleJavaTypes.add(type.getTypeText());
        }
    }
    JavaPsiFacade facade = JavaPsiFacade.getInstance(parameters.getOriginalFile().getProject());
    GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    List<PsiClass> possibleInstancesOf = new LinkedList<>();
    boolean suggestionsFound = false;
    PsiClass aClass;
    for (String possibleType : possibleJavaTypes) {
        if ((aClass = facade.findClass(possibleType, scope)) != null) {
            possibleInstancesOf.add(aClass);
            possibleInstancesOf.addAll(ExtenderProvider.getExtenders(aClass));
        }
    }
    for (PsiClass psiClass : possibleInstancesOf) {
        List<String> alreadySuggested = new LinkedList<>();
        for (PsiMethod method : psiClass.getAllMethods()) {
            if (method.getContainingClass() == null || Object.class.getName().equals(method.getContainingClass().getQualifiedName()) || method.getReturnType() == null || shippedBaseExtenderMethods.contains(method.getName()) || !method.getModifierList().hasModifierProperty(PsiModifier.PUBLIC) || InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_INTERFACE_FQN) && !InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_BASE_CLASS_FQN)) {
                continue;
            }
            String insertText = method.getName();
            String presentableText = method.getName();
            if (insertText.matches("((^is)|(^get)|(^set))[A-Z].*")) {
                insertText = presentableText = StringUtils.decapitalize(insertText.replaceFirst("(^is)|(^get)|(^set)", ""));
            } else {
                presentableText = method.getPresentation() == null ? presentableText + "()" : method.getPresentation().getPresentableText();
                insertText += "()";
            }
            if (alreadySuggested.contains(insertText)) {
                continue;
            }
            result.consume(LookupElementBuilder.create(method, insertText).withIcon(method.getIcon(0)).withTypeText(method.getReturnType().getPresentableText(), true).withTailText(" (" + psiClass.getContainingFile().getName() + ")", true).withPresentableText(presentableText).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
            alreadySuggested.add(insertText);
            suggestionsFound = true;
        }
    }
    if (suggestionsFound) {
        result.stopHere();
    }
}
Also used : Arrays(java.util.Arrays) PsiErrorElement(com.intellij.psi.PsiErrorElement) JSTypeEvaluator(com.intellij.lang.javascript.psi.resolve.JSTypeEvaluator) StringUtils(com.sun.xml.internal.ws.util.StringUtils) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) JSCompositeTypeImpl(com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl) Extender(ool.web.model.Extender) TokenType(com.intellij.psi.TokenType) PsiModifier(com.intellij.psi.PsiModifier) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) CompletionContributor(com.intellij.codeInsight.completion.CompletionContributor) PsiClass(com.intellij.psi.PsiClass) JSTokenTypes(com.intellij.lang.javascript.JSTokenTypes) PsiElement(com.intellij.psi.PsiElement) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) JSExpression(com.intellij.lang.javascript.psi.JSExpression) JSType(com.intellij.lang.javascript.psi.JSType) LinkedList(java.util.LinkedList) Module(com.intellij.openapi.module.Module) OxyTemplateInnerJs(ool.intellij.plugin.lang.OxyTemplateInnerJs) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) PsiMethod(com.intellij.psi.PsiMethod) ExtenderProvider(ool.intellij.plugin.psi.reference.innerjs.ExtenderProvider) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collectors(java.util.stream.Collectors) AutoCompletionPolicy(com.intellij.codeInsight.lookup.AutoCompletionPolicy) List(java.util.List) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) Pattern(java.util.regex.Pattern) BaseExtender(ool.web.model.BaseExtender) NotNull(org.jetbrains.annotations.NotNull) JSTypeImpl(com.intellij.lang.javascript.psi.types.JSTypeImpl) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) JSType(com.intellij.lang.javascript.psi.JSType) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) JSExpression(com.intellij.lang.javascript.psi.JSExpression) LinkedList(java.util.LinkedList) PsiErrorElement(com.intellij.psi.PsiErrorElement) JSTypeImpl(com.intellij.lang.javascript.psi.types.JSTypeImpl) JSCompositeTypeImpl(com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 4 with JSExpression

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

the class JsMacroParamSuggestionProvider method getMacroCall.

/**
 * @param argument argument of call expression
 * @return parent call expression, where utils namespace is skipped e.g.:
 * oxy.foo(params) -> oxy.foo
 * oxy.bar(utils.extend(params, {param: false}) -> oxy.bar
 */
@Nullable
private static JSCallExpression getMacroCall(@Nullable PsiElement argument) {
    if (argument == null) {
        return null;
    }
    JSCallExpression callExpression = PsiTreeUtil.getParentOfType(argument, JSCallExpression.class);
    if (callExpression == null) {
        return null;
    }
    if (callExpression.getText().startsWith(MacroIndex.UTILS_NAMESPACE)) {
        return getMacroCall(callExpression);
    }
    JSExpression[] arguments = callExpression.getArguments();
    if (arguments.length == 0 || !arguments[0].isEquivalentTo(argument)) {
        return null;
    }
    return callExpression;
}
Also used : JSCallExpression(com.intellij.lang.javascript.psi.JSCallExpression) JSExpression(com.intellij.lang.javascript.psi.JSExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with JSExpression

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

the class AngularJSTargetElementEvaluator method getElementByReference.

@Nullable
@Override
public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) {
    if (ref instanceof JSTextReference) {
        final PsiElement element = ref.getElement();
        final JSCallExpression call = PsiTreeUtil.getParentOfType(element, JSCallExpression.class);
        final JSExpression expression = call != null ? call.getMethodExpression() : null;
        if (expression instanceof JSReferenceExpression) {
            JSReferenceExpression callee = (JSReferenceExpression) expression;
            JSExpression qualifier = callee.getQualifier();
            if (qualifier != null && AngularJSIndexingHandler.INTERESTING_METHODS.contains(callee.getReferencedName()) && AngularIndexUtil.hasAngularJS(element.getProject())) {
                return element;
            }
        }
    }
    return null;
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSTextReference(com.intellij.lang.javascript.psi.impl.JSTextReference) JSCallExpression(com.intellij.lang.javascript.psi.JSCallExpression) JSExpression(com.intellij.lang.javascript.psi.JSExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JSExpression (com.intellij.lang.javascript.psi.JSExpression)8 PsiElement (com.intellij.psi.PsiElement)4 JSCallExpression (com.intellij.lang.javascript.psi.JSCallExpression)3 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 JstdTestCaseStructure (com.google.jstestdriver.idea.assertFramework.jstd.JstdTestCaseStructure)1 CompletionContributor (com.intellij.codeInsight.completion.CompletionContributor)1 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 AutoCompletionPolicy (com.intellij.codeInsight.lookup.AutoCompletionPolicy)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 JSTokenTypes (com.intellij.lang.javascript.JSTokenTypes)1 JSArgumentList (com.intellij.lang.javascript.psi.JSArgumentList)1 JSDefinitionExpression (com.intellij.lang.javascript.psi.JSDefinitionExpression)1 JSElement (com.intellij.lang.javascript.psi.JSElement)1 JSObjectLiteralExpression (com.intellij.lang.javascript.psi.JSObjectLiteralExpression)1 JSProperty (com.intellij.lang.javascript.psi.JSProperty)1 JSType (com.intellij.lang.javascript.psi.JSType)1