Search in sources :

Example 1 with ValueResolver

use of com.redhat.qute.project.datamodel.resolvers.ValueResolver in project quarkus-ls by redhat-developer.

the class QuteProjectRegistry method getNamespaceResolvers.

/**
 * Returns namespace resolvers from the given Qute project Uri.
 *
 * @param projectUri the Qute project Uri
 *
 * @return namespace resolvers from the given Qute project Uri.
 */
public List<ValueResolver> getNamespaceResolvers(String namespace, String projectUri) {
    if (StringUtils.isEmpty(projectUri)) {
        return Collections.emptyList();
    }
    QuteProject project = getProject(projectUri);
    if (project == null) {
        return Collections.emptyList();
    }
    ExtendedDataModelProject dataModel = project.getDataModelProject().getNow(null);
    if (dataModel == null) {
        return Collections.emptyList();
    }
    List<ValueResolver> namespaceResolvers = new ArrayList<>();
    List<TypeValueResolver> allTypeResolvers = dataModel.getTypeValueResolvers();
    if (allTypeResolvers != null) {
        for (ValueResolver resolver : allTypeResolvers) {
            if (isMatchNamespace(resolver, namespace, dataModel)) {
                namespaceResolvers.add(resolver);
            }
        }
    }
    List<MethodValueResolver> allMethodResolvers = dataModel.getMethodValueResolvers();
    if (allMethodResolvers != null) {
        for (ValueResolver resolver : allMethodResolvers) {
            if (isMatchNamespace(resolver, namespace, dataModel)) {
                namespaceResolvers.add(resolver);
            }
        }
    }
    List<FieldValueResolver> allFieldResolvers = dataModel.getFieldValueResolvers();
    if (allFieldResolvers != null) {
        for (ValueResolver resolver : allFieldResolvers) {
            if (isMatchNamespace(resolver, namespace, dataModel)) {
                namespaceResolvers.add(resolver);
            }
        }
    }
    return namespaceResolvers;
}
Also used : TypeValueResolver(com.redhat.qute.project.datamodel.resolvers.TypeValueResolver) ExtendedDataModelProject(com.redhat.qute.project.datamodel.ExtendedDataModelProject) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) FieldValueResolver(com.redhat.qute.project.datamodel.resolvers.FieldValueResolver) ValueResolver(com.redhat.qute.project.datamodel.resolvers.ValueResolver) TypeValueResolver(com.redhat.qute.project.datamodel.resolvers.TypeValueResolver) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) ArrayList(java.util.ArrayList) FieldValueResolver(com.redhat.qute.project.datamodel.resolvers.FieldValueResolver)

Example 2 with ValueResolver

use of com.redhat.qute.project.datamodel.resolvers.ValueResolver in project quarkus-ls by redhat-developer.

the class QuteCompletionsForExpression method doCompleteNamespaceResolvers.

public void doCompleteNamespaceResolvers(String namespace, Template template, Range range, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings, CompletionList list) {
    if (NamespacePart.DATA_NAMESPACE.equals(namespace)) {
        // {data:|}
        // Collect alias declared from parameter declaration
        doCompleteExpressionForObjectPartWithParameterAlias(template, range, list);
        // Collect parameters from CheckedTemplate method parameters
        doCompleteExpressionForObjectPartWithCheckedTemplate(template, range, list);
        return;
    }
    // {inject:|}
    // {|inject:}
    // {config:|}
    // {|config:}
    Set<String> existingResovers = new HashSet<>();
    List<ValueResolver> namespaceResolvers = javaCache.getNamespaceResolvers(namespace, template.getProjectUri());
    for (ValueResolver resolver : namespaceResolvers) {
        boolean useNamespaceInTextEdit = namespace == null;
        String named = resolver.getNamed();
        if (named != null) {
            // @Named("user")
            // User getUser();
            String label = useNamespaceInTextEdit ? resolver.getNamespace() + ':' + named : named;
            if (!existingResovers.contains(label)) {
                existingResovers.add(label);
                CompletionItem item = new CompletionItem();
                item.setLabel(label);
                item.setFilterText(label);
                item.setKind(CompletionItemKind.Field);
                TextEdit textEdit = new TextEdit();
                textEdit.setRange(range);
                textEdit.setNewText(label);
                item.setTextEdit(Either.forLeft(textEdit));
                item.setSortText("Zb" + item.getLabel());
                list.getItems().add(item);
            }
        } else {
            switch(resolver.getJavaElementKind()) {
                case METHOD:
                    {
                        MethodValueResolver method = (MethodValueResolver) resolver;
                        CompletionItem item = fillCompletionMethod(method, method.getNamespace(), useNamespaceInTextEdit, range, completionSettings, formattingSettings, list);
                        item.setKind(CompletionItemKind.Function);
                        // Display namespace resolvers (ex : config:getConfigProperty(...)) after
                        // declared objects
                        item.setSortText("Zc" + item.getLabel());
                        break;
                    }
                case FIELD:
                    {
                        FieldValueResolver field = (FieldValueResolver) resolver;
                        CompletionItem item = fillCompletionField(field, field.getNamespace(), namespace == null, range, list);
                        item.setKind(CompletionItemKind.Field);
                        // Display namespace resolvers (ex : inject:bean) after
                        // declared objects
                        item.setSortText("Zb" + item.getLabel());
                        break;
                    }
                default:
            }
        }
    }
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) FieldValueResolver(com.redhat.qute.project.datamodel.resolvers.FieldValueResolver) ValueResolver(com.redhat.qute.project.datamodel.resolvers.ValueResolver) TextEdit(org.eclipse.lsp4j.TextEdit) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) FieldValueResolver(com.redhat.qute.project.datamodel.resolvers.FieldValueResolver) HashSet(java.util.HashSet)

Example 3 with ValueResolver

use of com.redhat.qute.project.datamodel.resolvers.ValueResolver in project quarkus-ls by redhat-developer.

the class QuteCompletionsForExpression method createMethodSnippet.

private static String createMethodSnippet(JavaMethodInfo method, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings) {
    boolean snippetsSupported = completionSettings.isCompletionSnippetsSupported();
    String methodName = method.getMethodName();
    StringBuilder snippet = new StringBuilder();
    if (MATCH_NAME_ANY.equals(methodName)) {
        // config:*
        JavaParameterInfo firstParameter = method.getParameterAt(0);
        String parameterName = firstParameter != null ? firstParameter.getName() : methodName;
        if (snippetsSupported) {
            SnippetsBuilder.placeholders(1, parameterName, snippet);
        } else {
            snippet.append(parameterName);
        }
        if (snippetsSupported) {
            SnippetsBuilder.tabstops(0, snippet);
        }
    } else {
        snippet.append(methodName);
        if (method.hasParameters()) {
            int start = 0;
            if (method.isVirtual() && ((ValueResolver) method).getNamespace() == null) {
                start++;
            }
            snippet.append("(");
            for (int i = start; i < method.getParameters().size(); i++) {
                if (i > start) {
                    snippet.append(", ");
                }
                JavaParameterInfo parameter = method.getParameterAt(i);
                if (snippetsSupported) {
                    SnippetsBuilder.placeholders(i - start + 1, parameter.getName(), snippet);
                } else {
                    snippet.append(parameter.getName());
                }
            }
            snippet.append(")");
            if (snippetsSupported) {
                SnippetsBuilder.tabstops(0, snippet);
            }
        }
    }
    return snippet.toString();
}
Also used : MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) FieldValueResolver(com.redhat.qute.project.datamodel.resolvers.FieldValueResolver) ValueResolver(com.redhat.qute.project.datamodel.resolvers.ValueResolver) JavaParameterInfo(com.redhat.qute.commons.JavaParameterInfo)

Aggregations

FieldValueResolver (com.redhat.qute.project.datamodel.resolvers.FieldValueResolver)3 MethodValueResolver (com.redhat.qute.project.datamodel.resolvers.MethodValueResolver)3 ValueResolver (com.redhat.qute.project.datamodel.resolvers.ValueResolver)3 JavaParameterInfo (com.redhat.qute.commons.JavaParameterInfo)1 ExtendedDataModelProject (com.redhat.qute.project.datamodel.ExtendedDataModelProject)1 TypeValueResolver (com.redhat.qute.project.datamodel.resolvers.TypeValueResolver)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CompletionItem (org.eclipse.lsp4j.CompletionItem)1 TextEdit (org.eclipse.lsp4j.TextEdit)1