Search in sources :

Example 1 with JSFunctionExpression

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

the class InnerJsTypeEvaluator method checkMacroFirstParameter.

/**
 * @param parameter
 * @return macro, if parameter is its first parameter, null otherwise
 */
@Nullable
private static JSProperty checkMacroFirstParameter(@Nullable JSParameter parameter) {
    JSProperty macro;
    JSFunctionExpression functionExpression;
    if ((macro = PsiTreeUtil.getParentOfType(parameter, JSProperty.class)) != null && OxyTemplateIndexUtil.isMacro(macro) && macro.getLastChild() instanceof JSFunctionExpression) {
        functionExpression = (JSFunctionExpression) macro.getLastChild();
        if (functionExpression.getParameters().length > 0 && functionExpression.getParameters()[0].isEquivalentTo(parameter)) {
            return macro;
        }
    }
    return null;
}
Also used : JSFunctionExpression(com.intellij.lang.javascript.psi.JSFunctionExpression) JSProperty(com.intellij.lang.javascript.psi.JSProperty) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with JSFunctionExpression

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

the class JsMacroParamSuggestionProvider method getParamReferencesLocal.

@NotNull
private Collection<PsiElement> getParamReferencesLocal(@NotNull JSParameter param) {
    List<PsiElement> result = new LinkedList<>();
    JSFunctionExpression function = (JSFunctionExpression) macro.getLastChild();
    PsiFile file = macro.getContainingFile();
    Matcher matcher = Pattern.compile(param.getText()).matcher(function.getText());
    JSReferenceExpression referenceExpression;
    PsiElement resolve;
    while (matcher.find()) {
        PsiElement elementAt = file.findElementAt(function.getNode().getStartOffset() + matcher.start());
        if (elementAt != null && (referenceExpression = PsiTreeUtil.getParentOfType(elementAt, JSReferenceExpression.class)) != null && (resolve = referenceExpression.resolve()) != null && resolve.isEquivalentTo(param)) {
            result.add(referenceExpression);
        }
    }
    return result;
}
Also used : JSFunctionExpression(com.intellij.lang.javascript.psi.JSFunctionExpression) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) Matcher(java.util.regex.Matcher) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JSFunctionExpression

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

the class JsMacroParamSuggestionProvider method getJsParamDoc.

@Nullable
private static JSDocTag getJsParamDoc(@NotNull String paramName, @NotNull JSProperty macro) {
    JSFunctionExpression functionExpression = (JSFunctionExpression) macro.getLastChild();
    JSDocComment docComment;
    JSParameter[] referenceParams;
    if (!(macro.getFirstChild() instanceof JSDocComment) || (referenceParams = functionExpression.getParameterVariables()).length == 0) {
        return null;
    }
    String docParamExpectedName = referenceParams[0].getText() + '.' + paramName;
    docComment = (JSDocComment) macro.getFirstChild();
    String commentParameterName;
    for (JSDocTag docTag : docComment.getTags()) {
        if ((commentParameterName = getCommentParameterName(docTag)) == null) {
            continue;
        }
        if (paramName.equals(commentParameterName) || docParamExpectedName.equals(commentParameterName)) {
            return docTag;
        }
    }
    return null;
}
Also used : JSFunctionExpression(com.intellij.lang.javascript.psi.JSFunctionExpression) JSDocComment(com.intellij.lang.javascript.psi.jsdoc.JSDocComment) JSDocTag(com.intellij.lang.javascript.psi.jsdoc.JSDocTag) JSParameter(com.intellij.lang.javascript.psi.JSParameter) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with JSFunctionExpression

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

the class JsMacroNameDataIndexer method map.

@Override
@NotNull
public Map<String, JsMacroNameIndexedElement> map(@NotNull final FileContent inputData) {
    PsiFile jsFile = inputData.getPsiFile().getViewProvider().getPsi(OxyTemplateInnerJs.INSTANCE);
    Map<String, JsMacroNameIndexedElement> result = new HashMap<>();
    for (PsiElement psiElement : jsFile.getChildren()) {
        if (psiElement instanceof JSExpressionStatement && (psiElement = PsiTreeUtil.getChildOfAnyType(psiElement, JSAssignmentExpression.class)) != null && psiElement.getFirstChild() instanceof JSDefinitionExpression) {
            String rootNamespace = psiElement.getFirstChild().getText().replace(MACRO_REGISTRY_NAMESPACE + ".", "");
            boolean firstIteration = true;
            for (JSReferenceExpression ref : PsiTreeUtil.findChildrenOfType(psiElement.getFirstChild(), JSReferenceExpression.class)) {
                String namespace = ref.getText().replace(MACRO_REGISTRY_NAMESPACE + ".", "");
                if (!namespace.equals(DEFAULT_NAMESPACE) && !namespace.equals(MACRO_REGISTRY_NAMESPACE)) {
                    result.put(ref.getText().replace(MACRO_REGISTRY_NAMESPACE + ".", ""), new JsMacroNameIndexedElement(firstIteration && psiElement.getLastChild() instanceof JSFunctionExpression, ref.getTextOffset() + ref.getTextLength() - 1));
                }
                firstIteration = false;
            }
            if (psiElement.getLastChild() instanceof JSObjectLiteralExpression) {
                processObjectLiteralExpression((JSObjectLiteralExpression) psiElement.getLastChild(), rootNamespace, result);
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) JSFunctionExpression(com.intellij.lang.javascript.psi.JSFunctionExpression) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSDefinitionExpression(com.intellij.lang.javascript.psi.JSDefinitionExpression) PsiFile(com.intellij.psi.PsiFile) JSObjectLiteralExpression(com.intellij.lang.javascript.psi.JSObjectLiteralExpression) PsiElement(com.intellij.psi.PsiElement) JSExpressionStatement(com.intellij.lang.javascript.psi.JSExpressionStatement) JSAssignmentExpression(com.intellij.lang.javascript.psi.JSAssignmentExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with JSFunctionExpression

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

the class JsMacroNameDataIndexer method processObjectLiteralExpression.

private static void processObjectLiteralExpression(@NotNull JSObjectLiteralExpression expression, @NotNull String namespace, @NotNull Map<String, JsMacroNameIndexedElement> result) {
    for (JSProperty property : expression.getProperties()) {
        PsiElement nameIdentifier = property.getNameIdentifier();
        if (nameIdentifier == null) {
            continue;
        }
        if (property.getLastChild() instanceof JSObjectLiteralExpression) {
            result.put(namespace + "." + nameIdentifier.getText().replace(MACRO_REGISTRY_NAMESPACE + ".", ""), new JsMacroNameIndexedElement(nameIdentifier.getTextOffset() + nameIdentifier.getTextLength() - 1));
            processObjectLiteralExpression((JSObjectLiteralExpression) property.getLastChild(), namespace + "." + property.getName(), result);
        } else if (property.getLastChild() instanceof JSFunctionExpression) {
            result.put(namespace + "." + nameIdentifier.getText().replace(MACRO_REGISTRY_NAMESPACE + ".", ""), new JsMacroNameIndexedElement(true, nameIdentifier.getTextOffset() + nameIdentifier.getTextLength() - 1));
        }
    }
}
Also used : JSFunctionExpression(com.intellij.lang.javascript.psi.JSFunctionExpression) JSObjectLiteralExpression(com.intellij.lang.javascript.psi.JSObjectLiteralExpression) JSProperty(com.intellij.lang.javascript.psi.JSProperty) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JSFunctionExpression (com.intellij.lang.javascript.psi.JSFunctionExpression)5 PsiElement (com.intellij.psi.PsiElement)3 JSObjectLiteralExpression (com.intellij.lang.javascript.psi.JSObjectLiteralExpression)2 JSProperty (com.intellij.lang.javascript.psi.JSProperty)2 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)2 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 JSAssignmentExpression (com.intellij.lang.javascript.psi.JSAssignmentExpression)1 JSDefinitionExpression (com.intellij.lang.javascript.psi.JSDefinitionExpression)1 JSExpressionStatement (com.intellij.lang.javascript.psi.JSExpressionStatement)1 JSParameter (com.intellij.lang.javascript.psi.JSParameter)1 JSDocComment (com.intellij.lang.javascript.psi.jsdoc.JSDocComment)1 JSDocTag (com.intellij.lang.javascript.psi.jsdoc.JSDocTag)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1