Search in sources :

Example 1 with MacroParamSuggestionSet

use of ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet in project oxy-template-support-plugin by mutant-industries.

the class JavaMacroParamSuggestionProvider method getParamsPulledFromParamHelper.

private MacroParamSuggestionSet getParamsPulledFromParamHelper(PsiLocalVariable paramHelper) {
    MacroParamSuggestionSet params = new MacroParamSuggestionSet();
    PsiReferenceExpression methodReference;
    for (PsiReference reference : ReferencesSearch.search(paramHelper).findAll()) {
        if ((methodReference = PsiTreeUtil.getParentOfType(reference.getElement(), PsiReferenceExpression.class)) != null) {
            String parameterName = null;
            boolean required = false;
            boolean notNull = true;
            String type = JSCommonTypeNames.ANY_TYPE_NAME;
            String defaultValue = null;
            while (methodReference != null) {
                if (!(methodReference.getParent() instanceof PsiMethodCallExpression) || methodReference.getReferenceName() == null) {
                    break;
                }
                PsiMethodCallExpression callExpression = (PsiMethodCallExpression) methodReference.getParent();
                PsiExpression[] expressions = callExpression.getArgumentList().getExpressions();
                if (expressions.length == 0) {
                    break;
                }
                switch(methodReference.getReferenceName()) {
                    case "parameter":
                    case "pullParameter":
                        parameterName = getExpressionValue(expressions[0]);
                        if (expressions.length == 2 && expressions[1].getFirstChild() instanceof PsiTypeElement) {
                            type = InnerJsJavaTypeConverter.modifyCollectionType(((PsiTypeElement) expressions[1].getFirstChild()).getType(), paramHelper.getProject());
                        }
                        break;
                    case "pullIntegerValue":
                        parameterName = getExpressionValue(expressions[0]);
                        notNull = expressions.length < 2;
                        required = expressions.length < 2;
                        type = JSCommonTypeNames.NUMBER_TYPE_NAME;
                        if (expressions.length == 2) {
                            defaultValue = getExpressionValue(expressions[1]);
                        }
                        break;
                    case "pullBooleanValue":
                        parameterName = getExpressionValue(expressions[0]);
                        notNull = false;
                        required = false;
                        type = JSCommonTypeNames.BOOLEAN_TYPE_NAME;
                        if (expressions.length == 2) {
                            defaultValue = getExpressionValue(expressions[1]);
                        }
                        break;
                    case "setNonNull":
                        notNull = Boolean.valueOf(expressions[0].getText());
                        break;
                    case "setRequired":
                        required = Boolean.valueOf(expressions[0].getText());
                        break;
                }
                methodReference = PsiTreeUtil.getParentOfType(methodReference, PsiReferenceExpression.class);
            }
            if (parameterName != null) {
                params.add(new JavaMacroParamDescriptor(parameterName, macro, notNull, required, type, defaultValue));
            }
        }
    // TODO the case when object is passed to another method
    }
    return params;
}
Also used : MacroParamSuggestionSet(ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiTypeElement(com.intellij.psi.PsiTypeElement) PsiReference(com.intellij.psi.PsiReference) JavaMacroParamDescriptor(ool.intellij.plugin.psi.macro.param.descriptor.JavaMacroParamDescriptor) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression)

Example 2 with MacroParamSuggestionSet

use of ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet in project oxy-template-support-plugin by mutant-industries.

the class JavaMacroParamSuggestionProvider method getMacroParamSuggestions.

@NotNull
@Override
protected MacroParamSuggestionSet getMacroParamSuggestions() {
    MacroParamSuggestionSet params = new MacroParamSuggestionSet();
    PsiMethod[] methods = macro.findMethodsByName("execute", true);
    PsiMethod executeMethod;
    PsiParameter eventParam;
    // current and / or superclass(es) + interface method
    if (methods.length < 2 || (executeMethod = methods[0]).getParameterList().getParametersCount() < 1 || (eventParam = executeMethod.getParameterList().getParameters()[0]) == null || !eventParam.getType().equalsToText(MACRO_EVENT_FQN)) {
        return params;
    }
    for (PsiReference reference : ReferencesSearch.search(eventParam).findAll()) {
        PsiLocalVariable localVariable = PsiTreeUtil.getParentOfType(reference.getElement(), PsiLocalVariable.class);
        if (localVariable == null || !localVariable.getType().equalsToText(MACRO_PARAM_HELPER_FQN)) {
            continue;
        }
        // TODO the case when object is passed to another method
        params.addAll(getParamsPulledFromParamHelper(localVariable));
    }
    return params;
}
Also used : MacroParamSuggestionSet(ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet) PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) PsiReference(com.intellij.psi.PsiReference) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MacroParamSuggestionSet

use of ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet in project oxy-template-support-plugin by mutant-industries.

the class ParamSuggestionProvider method compute.

@Nullable
@Override
public Result<MacroParamSuggestionSet> compute() {
    MacroParamSuggestionSet macroParamSuggestions = getMacroParamSuggestions();
    Set<PsiElement> cacheDependencies = new HashSet<>();
    for (MacroParamDescriptor descriptor : macroParamSuggestions) {
        if (descriptor.getType() == null) {
            continue;
        }
        SimplifiedClassNameResolver simplifiedClassNameResolver = new SimplifiedClassNameResolver(macro.getContainingFile());
        descriptor.getType().accept(simplifiedClassNameResolver);
        cacheDependencies.addAll(simplifiedClassNameResolver.getResolvedClassList());
    }
    cacheDependencies.addAll(this.cacheDependencies);
    return Result.create(macroParamSuggestions, cacheDependencies);
}
Also used : MacroParamSuggestionSet(ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet) MacroParamDescriptor(ool.intellij.plugin.psi.macro.param.descriptor.MacroParamDescriptor) SimplifiedClassNameResolver(ool.intellij.plugin.psi.reference.innerjs.SimplifiedClassNameResolver) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with MacroParamSuggestionSet

use of ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet in project oxy-template-support-plugin by mutant-industries.

the class MacroParamDocumentationProvider method generateJsMacroDocumentation.

private static String generateJsMacroDocumentation(@NotNull JSProperty macro) {
    JSDocComment comment;
    MacroParamSuggestionSet paramDescriptors = MacroParamHelper.getJsMacroParamSuggestions(macro, false);
    String qualifiedName = OxyTemplateIndexUtil.getMacroFullyQualifiedName(macro);
    assert qualifiedName != null;
    StringBuilder result = new StringBuilder();
    result.append("<PRE><b>").append(qualifiedName).append("</b>(&nbsp;[ optional ] params )\n</PRE> ");
    if ((comment = JSStubBasedPsiTreeUtil.findDocComment(macro)) != null) {
        JSDocPlainCommentBuilder builder = new JSDocPlainCommentBuilder();
        JSDocumentationUtils.processDocumentationTextFromComment(comment.getNode(), builder);
        String doc = builder.getDoc().trim();
        if (doc.length() > 0) {
            result.append(doc).append("<br/>");
        }
    }
    if (paramDescriptors.size() != 0) {
        result.append("<br/><b>").append(I18nSupport.message("macro.param.block.heading")).append("</b><ul>");
        for (MacroParamDescriptor descriptor : paramDescriptors) {
            result.append("<li>").append(descriptor.getName()).append(" ").append(descriptor.generateTypeInfo());
            if (!StringUtil.isEmpty(descriptor.getDocText())) {
                result.append(" - ").append(descriptor.getDocText());
            }
            result.append("</li>");
        }
        result.append("</ul>");
    }
    return result.toString();
}
Also used : JSDocComment(com.intellij.lang.javascript.psi.jsdoc.JSDocComment) MacroParamSuggestionSet(ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet) MacroParamDescriptor(ool.intellij.plugin.psi.macro.param.descriptor.MacroParamDescriptor)

Example 5 with MacroParamSuggestionSet

use of ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet in project oxy-template-support-plugin by mutant-industries.

the class JsMacroParamSuggestionProvider method getMacroParamSuggestions.

@NotNull
@Override
protected MacroParamSuggestionSet getMacroParamSuggestions() {
    JSParameter[] params;
    if ((params = functionExpression.getParameterVariables()).length == 0) {
        return MacroParamSuggestionSet.empty();
    }
    final MacroParamSuggestionSet result = new MacroParamSuggestionSet();
    submergedCalls = new LinkedList<>();
    String paramsObjectName = params[0].getText();
    /**
     * !! cannot be done via {@link com.intellij.psi.search.searches.ReferencesSearch.search}, which would in some
     * special cases (foreach in foreach) end up in infinite loop - reference search calls type resolver, which makes use of this method
     */
    for (PsiElement elementLocal : getParamReferencesLocal(params[0])) {
        PsiElement element = elementLocal;
        if (!(elementLocal instanceof JSReferenceExpression)) {
            continue;
        }
        if ((elementLocal = elementLocal.getNextSibling()) != null) {
            if (elementLocal.getNode().getElementType() == TokenType.WHITE_SPACE) {
                elementLocal = elementLocal.getNextSibling();
            }
            if (elementLocal != null && elementLocal.getNode().getElementType() == JSTokenTypes.DOT) {
                if ((elementLocal = elementLocal.getNextSibling()) != null) {
                    if (elementLocal.getNode().getElementType() == TokenType.WHITE_SPACE) {
                        elementLocal = elementLocal.getNextSibling();
                    }
                    if (elementLocal != null && elementLocal.getNode().getElementType() == JSTokenTypes.IDENTIFIER) {
                        result.add(new JsMacroParamDescriptor(elementLocal.getText(), macro, getJsParamDoc(elementLocal.getText(), macro), true));
                    }
                }
                continue;
            }
        }
        JSCallExpression macroCall = getMacroCall(element);
        JSReferenceExpression callReference;
        if (macroCall == null || (callReference = PsiTreeUtil.getChildOfType(macroCall, JSReferenceExpression.class)) == null || (element = callReference.resolve()) == null) {
            continue;
        }
        submergedCalls.add(element);
    }
    if (macro.getFirstChild() instanceof JSDocComment) {
        String paramName;
        PsiReference reference;
        PsiElement resolve;
        for (JSDocTag docTag : ((JSDocComment) macro.getFirstChild()).getTags()) {
            if ((reference = docTag.getReference()) != null && (resolve = reference.resolve()) != null && !params[0].isEquivalentTo(resolve) || (paramName = getCommentParameterName(docTag)) == null) {
                continue;
            }
            paramName = paramName.replaceFirst("^" + paramsObjectName + "\\.", "");
            if (result.getByName(paramName) != null) {
                continue;
            }
            result.add(new JsMacroParamDescriptor(paramName, macro, docTag, false));
        }
    }
    return result;
}
Also used : JSDocComment(com.intellij.lang.javascript.psi.jsdoc.JSDocComment) MacroParamSuggestionSet(ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSCallExpression(com.intellij.lang.javascript.psi.JSCallExpression) PsiReference(com.intellij.psi.PsiReference) JsMacroParamDescriptor(ool.intellij.plugin.psi.macro.param.descriptor.JsMacroParamDescriptor) JSDocTag(com.intellij.lang.javascript.psi.jsdoc.JSDocTag) JSParameter(com.intellij.lang.javascript.psi.JSParameter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MacroParamSuggestionSet (ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet)5 PsiReference (com.intellij.psi.PsiReference)3 JSDocComment (com.intellij.lang.javascript.psi.jsdoc.JSDocComment)2 PsiElement (com.intellij.psi.PsiElement)2 MacroParamDescriptor (ool.intellij.plugin.psi.macro.param.descriptor.MacroParamDescriptor)2 NotNull (org.jetbrains.annotations.NotNull)2 JSCallExpression (com.intellij.lang.javascript.psi.JSCallExpression)1 JSParameter (com.intellij.lang.javascript.psi.JSParameter)1 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)1 JSDocTag (com.intellij.lang.javascript.psi.jsdoc.JSDocTag)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)1 PsiMethod (com.intellij.psi.PsiMethod)1 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)1 PsiParameter (com.intellij.psi.PsiParameter)1 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)1 PsiTypeElement (com.intellij.psi.PsiTypeElement)1 HashSet (java.util.HashSet)1 JavaMacroParamDescriptor (ool.intellij.plugin.psi.macro.param.descriptor.JavaMacroParamDescriptor)1 JsMacroParamDescriptor (ool.intellij.plugin.psi.macro.param.descriptor.JsMacroParamDescriptor)1