Search in sources :

Example 1 with JavaParameterInfo

use of com.redhat.qute.commons.JavaParameterInfo in project quarkus-ls by redhat-developer.

the class QuteDiagnostics method validateMethodPart.

/**
 * Validate the given method part.
 *
 * @param part                     the method part to validate.
 * @param ownerSection             the owner section and null otherwise.
 * @param template                 the template.
 * @param projectUri               the project Uri.
 * @param resolutionContext        the resolution context.
 * @param baseType                 the base object type.
 * @param iterableOfType           the iterable of type.
 * @param diagnostics              the diagnostic list to fill.
 * @param resolvingJavaTypeContext the resolving Java type context.
 *
 * @return the Java type returned by the member part and null otherwise.
 */
private ResolvedJavaTypeInfo validateMethodPart(MethodPart methodPart, Section ownerSection, Template template, String projectUri, QuteValidationSettings validationSettings, ResolutionContext resolutionContext, ResolvedJavaTypeInfo resolvedJavaType, ResolvedJavaTypeInfo iter, List<Diagnostic> diagnostics, ResolvingJavaTypeContext resolvingJavaTypeContext) {
    // Validate parameters of the method part
    boolean undefinedType = false;
    List<ResolvedJavaTypeInfo> parameterTypes = new ArrayList<>();
    for (Parameter parameter : methodPart.getParameters()) {
        ResolvedJavaTypeInfo result = null;
        Expression expression = parameter.getJavaTypeExpression();
        if (expression != null) {
            result = validateExpression(expression, ownerSection, template, validationSettings, resolutionContext, resolvingJavaTypeContext, diagnostics);
        }
        if (result == null) {
            undefinedType = true;
        }
        parameterTypes.add(result);
    }
    if (undefinedType) {
        // One of parameter cannot be resolved as type, teh validation is stopped
        return null;
    }
    // All parameters are resolved, validate the existing of method name according
    // to the computed parameter types
    String methodName = methodPart.getPartName();
    String namespace = methodPart.getNamespace();
    JavaMemberResult result = javaCache.findMethod(resolvedJavaType, namespace, methodName, parameterTypes, projectUri);
    JavaMethodInfo method = (JavaMethodInfo) result.getMember();
    if (method == null) {
        QuteErrorCode errorCode = QuteErrorCode.UnknownMethod;
        String arg = null;
        if (namespace != null) {
            // ex :{config.getXXXX()}
            errorCode = QuteErrorCode.UnknownNamespaceResolverMethod;
            arg = namespace;
        } else {
            // ex : {@org.acme.Item item}
            // {item.getXXXX()}
            arg = resolvedJavaType.getSignature();
            InvalidMethodReason reason = javaCache.getInvalidMethodReason(methodName, resolvedJavaType, projectUri);
            if (reason != null) {
                switch(reason) {
                    case VoidReturn:
                        errorCode = QuteErrorCode.InvalidMethodVoid;
                        break;
                    case Static:
                        errorCode = QuteErrorCode.InvalidMethodStatic;
                        break;
                    case FromObject:
                        errorCode = QuteErrorCode.InvalidMethodFromObject;
                        break;
                    default:
                }
            }
        }
        Range range = QutePositionUtility.createRange(methodPart);
        Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Error, errorCode, methodName, arg);
        diagnostics.add(diagnostic);
        return null;
    }
    boolean matchVirtualMethod = result.isMatchVirtualMethod();
    if (!matchVirtualMethod) {
        Range range = QutePositionUtility.createRange(methodPart);
        Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Error, // 
        QuteErrorCode.InvalidVirtualMethod, // 
        method.getName(), // 
        method.getSimpleSourceType(), resolvedJavaType.getJavaElementSimpleType());
        diagnostics.add(diagnostic);
        return null;
    }
    boolean matchParameters = result.isMatchParameters();
    if (!matchParameters) {
        // The method codePointAt(int) in the type String is not applicable for the
        // arguments ()
        StringBuilder expectedSignature = new StringBuilder("(");
        boolean ignoreParameter = method.isVirtual();
        for (JavaParameterInfo parameter : method.getParameters()) {
            if (!ignoreParameter) {
                if (expectedSignature.length() > 1) {
                    expectedSignature.append(", ");
                }
                expectedSignature.append(parameter.getJavaElementSimpleType());
            }
            ignoreParameter = false;
        }
        expectedSignature.append(")");
        expectedSignature.insert(0, method.getName());
        StringBuilder actualSignature = new StringBuilder("(");
        for (ResolvedJavaTypeInfo parameterType : parameterTypes) {
            if (actualSignature.length() > 1) {
                actualSignature.append(", ");
            }
            actualSignature.append(parameterType != null ? parameterType.getJavaElementSimpleType() : parameterType);
        }
        actualSignature.append(")");
        Range range = QutePositionUtility.createRange(methodPart);
        Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Error, // 
        QuteErrorCode.InvalidMethodParameter, // 
        expectedSignature.toString(), // 
        method.getSimpleSourceType(), /* String */
        actualSignature.toString());
        diagnostics.add(diagnostic);
        return null;
    }
    String memberType = method.resolveJavaElementType(iter);
    return validateJavaTypePart(methodPart, ownerSection, projectUri, diagnostics, resolvingJavaTypeContext, memberType);
}
Also used : JavaMemberResult(com.redhat.qute.project.JavaMemberResult) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) ArrayList(java.util.ArrayList) DiagnosticDataFactory.createDiagnostic(com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) QuteErrorCode(com.redhat.qute.services.diagnostics.QuteErrorCode) JavaMethodInfo(com.redhat.qute.commons.JavaMethodInfo) Expression(com.redhat.qute.parser.template.Expression) Parameter(com.redhat.qute.parser.template.Parameter) InvalidMethodReason(com.redhat.qute.commons.InvalidMethodReason) JavaParameterInfo(com.redhat.qute.commons.JavaParameterInfo)

Example 2 with JavaParameterInfo

use of com.redhat.qute.commons.JavaParameterInfo in project quarkus-ls by redhat-developer.

the class QuteProjectRegistry method isMatchParameters.

private boolean isMatchParameters(JavaMethodInfo method, List<ResolvedJavaTypeInfo> parameterTypes, String projectUri) {
    boolean virtualMethod = method.isVirtual();
    if (parameterTypes.size() != (method.getParameters().size() - (virtualMethod ? 1 : 0))) {
        return false;
    }
    for (int i = 0; i < parameterTypes.size(); i++) {
        JavaParameterInfo parameterInfo = method.getParameters().get(i + (virtualMethod ? 1 : 0));
        ResolvedJavaTypeInfo result = parameterTypes.get(i);
        String parameterType = parameterInfo.getType();
        if (!isMatchType(result, parameterType, projectUri)) {
            return false;
        }
    }
    return true;
}
Also used : ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) JavaParameterInfo(com.redhat.qute.commons.JavaParameterInfo)

Example 3 with JavaParameterInfo

use of com.redhat.qute.commons.JavaParameterInfo in project quarkus-ls by redhat-developer.

the class QuteCompletionsForParameterDeclaration method collectJavaClassesSuggestions.

private CompletableFuture<CompletionList> collectJavaClassesSuggestions(String pattern, boolean hasAlias, boolean closed, JavaTypeRangeOffset rangeOffset, String projectUri, Template template, QuteCompletionSettings completionSettings) {
    QuteJavaTypesParams params = new QuteJavaTypesParams(pattern, projectUri);
    return // 
    javaCache.getJavaTypes(params).thenApply(result -> {
        if (result == null) {
            return null;
        }
        CompletionList list = new CompletionList();
        list.setItems(new ArrayList<>());
        Range range = QutePositionUtility.createRange(rangeOffset, template);
        for (JavaTypeInfo typeInfo : result) {
            String fullClassName = typeInfo.getName();
            CompletionItem item = new CompletionItem();
            item.setLabel(typeInfo.getSignature());
            TextEdit textEdit = new TextEdit();
            textEdit.setRange(range);
            StringBuilder insertText = new StringBuilder(fullClassName);
            if (typeInfo.getJavaTypeKind() == JavaTypeKind.Package) {
                item.setKind(CompletionItemKind.Module);
                if (rangeOffset.isInGeneric() && !rangeOffset.isGenericClosed()) {
                    insertText.append('>');
                }
            } else {
                item.setKind(typeInfo.getJavaTypeKind() == JavaTypeKind.Interface ? CompletionItemKind.Interface : CompletionItemKind.Class);
                int snippetIndex = 1;
                // Generate class
                int index = fullClassName.lastIndexOf('.');
                String typeName = index != -1 ? fullClassName.substring(index + 1, fullClassName.length()) : fullClassName;
                List<JavaParameterInfo> parameters = typeInfo.getTypeParameters();
                if (!parameters.isEmpty()) {
                    insertText.append('<');
                    for (int i = 0; i < parameters.size(); i++) {
                        if (i > 0) {
                            insertText.append(',');
                        }
                        String parameterType = parameters.get(i).getType();
                        if (completionSettings.isCompletionSnippetsSupported()) {
                            SnippetsBuilder.placeholders(snippetIndex++, parameterType, insertText);
                        } else {
                            insertText.append(parameterType);
                        }
                    }
                    insertText.append('>');
                } else if (rangeOffset.isInGeneric() && !rangeOffset.isGenericClosed()) {
                    insertText.append('>');
                }
                if (!hasAlias) {
                    insertText.append(' ');
                }
                // Generate alias
                String alias = String.valueOf(typeName.charAt(0)).toLowerCase() + typeName.substring(1, typeName.length());
                if (completionSettings.isCompletionSnippetsSupported()) {
                    item.setInsertTextFormat(InsertTextFormat.Snippet);
                    if (!hasAlias) {
                        SnippetsBuilder.placeholders(snippetIndex++, alias, insertText);
                        if (!closed) {
                            insertText.append("}");
                        }
                    }
                    // $0
                    SnippetsBuilder.tabstops(0, insertText);
                } else {
                    item.setInsertTextFormat(InsertTextFormat.PlainText);
                    if (!hasAlias) {
                        insertText.append(alias);
                        if (!closed) {
                            insertText.append("}");
                        }
                    }
                }
            }
            textEdit.setNewText(insertText.toString());
            item.setTextEdit(Either.forLeft(textEdit));
            list.getItems().add(item);
        }
        return list;
    });
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) QuteJavaTypesParams(com.redhat.qute.commons.QuteJavaTypesParams) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextEdit(org.eclipse.lsp4j.TextEdit) Range(org.eclipse.lsp4j.Range) JavaParameterInfo(com.redhat.qute.commons.JavaParameterInfo)

Example 4 with JavaParameterInfo

use of com.redhat.qute.commons.JavaParameterInfo in project quarkus-ls by redhat-developer.

the class DocumentationUtils method createDocumentation.

private static StringBuilder createDocumentation(JavaMemberInfo member, ResolvedJavaTypeInfo iterableOfResolvedType, boolean markdown) {
    StringBuilder documentation = new StringBuilder();
    // Title
    if (markdown) {
        documentation.append("```java");
        documentation.append(System.lineSeparator());
    }
    documentation.append(getSimpleType(member.resolveJavaElementType(iterableOfResolvedType)));
    documentation.append(" ");
    String sourceType = member.getSourceType();
    if (sourceType != null) {
        documentation.append(sourceType);
        documentation.append(".");
    }
    documentation.append(member.getName());
    if (member.getJavaElementKind() == JavaElementKind.METHOD) {
        documentation.append('(');
        JavaMethodInfo methodInfo = (JavaMethodInfo) member;
        boolean virtualMethod = methodInfo.isVirtual();
        List<JavaParameterInfo> parameters = methodInfo.getParameters();
        int start = virtualMethod ? 1 : 0;
        for (int i = start; i < parameters.size(); i++) {
            JavaParameterInfo parameter = parameters.get(i);
            String type = parameter.getJavaElementSimpleType();
            String name = parameter.getName();
            if (i > start) {
                documentation.append(", ");
            }
            documentation.append(type);
            documentation.append(' ');
            documentation.append(name);
        }
        documentation.append(')');
    }
    if (markdown) {
        documentation.append(System.lineSeparator());
        documentation.append("```");
    }
    if (member.getDocumentation() != null) {
        documentation.append(System.lineSeparator());
        documentation.append(member.getDocumentation());
    }
    return documentation;
}
Also used : JavaMethodInfo(com.redhat.qute.commons.JavaMethodInfo) JavaParameterInfo(com.redhat.qute.commons.JavaParameterInfo)

Example 5 with JavaParameterInfo

use of com.redhat.qute.commons.JavaParameterInfo 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

JavaParameterInfo (com.redhat.qute.commons.JavaParameterInfo)6 JavaMethodInfo (com.redhat.qute.commons.JavaMethodInfo)2 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)2 Range (org.eclipse.lsp4j.Range)2 InvalidMethodReason (com.redhat.qute.commons.InvalidMethodReason)1 JavaTypeInfo (com.redhat.qute.commons.JavaTypeInfo)1 QuteJavaTypesParams (com.redhat.qute.commons.QuteJavaTypesParams)1 Expression (com.redhat.qute.parser.template.Expression)1 Parameter (com.redhat.qute.parser.template.Parameter)1 JavaMemberResult (com.redhat.qute.project.JavaMemberResult)1 FieldValueResolver (com.redhat.qute.project.datamodel.resolvers.FieldValueResolver)1 MethodValueResolver (com.redhat.qute.project.datamodel.resolvers.MethodValueResolver)1 ValueResolver (com.redhat.qute.project.datamodel.resolvers.ValueResolver)1 DiagnosticDataFactory.createDiagnostic (com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic)1 QuteErrorCode (com.redhat.qute.services.diagnostics.QuteErrorCode)1 ArrayList (java.util.ArrayList)1 CompletionItem (org.eclipse.lsp4j.CompletionItem)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 Diagnostic (org.eclipse.lsp4j.Diagnostic)1 TextEdit (org.eclipse.lsp4j.TextEdit)1