Search in sources :

Example 1 with ObjectPart

use of com.redhat.qute.parser.expression.ObjectPart in project quarkus-ls by redhat-developer.

the class QuteDiagnostics method validateObjectPart.

private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart objectPart, Section ownerSection, Template template, String projectUri, QuteValidationSettings validationSettings, ResolutionContext resolutionContext, List<Diagnostic> diagnostics, ResolvingJavaTypeContext resolvingJavaTypeContext) {
    JavaMemberInfo javaMember = resolutionContext.findMemberWithObject(objectPart.getPartName(), projectUri);
    if (javaMember != null) {
        ResolvedJavaTypeInfo resolvedJavaType = resolveJavaType(javaMember.getJavaElementType(), projectUri, resolvingJavaTypeContext);
        if (isResolvingJavaType(resolvedJavaType)) {
            return null;
        }
        return resolvedJavaType;
    }
    JavaTypeInfoProvider javaTypeInfo = objectPart.resolveJavaType();
    if (javaTypeInfo == null) {
        if (!resolvingJavaTypeContext.isDataModelTemplateResolved()) {
            // The data model is not loaded, ignore the error of undefined object
            return null;
        }
        if (UserTagUtils.isUserTag(template)) {
            // Ignore undefined object diagnostic for user tag
            return null;
        }
        // ex : {item} --> undefined object
        DiagnosticSeverity severity = validationSettings.getUndefinedObject().getDiagnosticSeverity();
        if (severity == null) {
            return null;
        }
        Range range = QutePositionUtility.createRange(objectPart);
        Diagnostic diagnostic = createDiagnostic(range, severity, QuteErrorCode.UndefinedObject, objectPart.getPartName());
        // Create data information helpful for code action
        diagnostic.setData(DiagnosticDataFactory.createUndefinedObjectData(objectPart.getPartName(), ownerSection != null && ownerSection.isIterable()));
        diagnostics.add(diagnostic);
        return null;
    }
    String javaTypeToResolve = javaTypeInfo.getJavaType();
    if (javaTypeToResolve == null) {
        // case of (#for item as data.items) where data.items expression must be
        // evaluated
        Expression expression = javaTypeInfo.getJavaTypeExpression();
        if (expression != null) {
            String literalJavaType = expression.getLiteralJavaType();
            if (literalJavaType != null) {
                javaTypeToResolve = literalJavaType;
            } else {
                Part lastPart = expression.getLastPart();
                if (lastPart != null) {
                    ResolvedJavaTypeInfo alias = javaCache.resolveJavaType(lastPart, projectUri).getNow(RESOLVING_JAVA_TYPE);
                    if (isResolvingJavaType(alias)) {
                        return null;
                    }
                    if (alias == null) {
                        if (!resolvingJavaTypeContext.isDataModelTemplateResolved()) {
                            // The data model is not loaded, don't validate the item of items.
                            return null;
                        }
                    } else {
                        javaTypeToResolve = alias.getSignature();
                    }
                }
            }
        }
    }
    return validateJavaTypePart(objectPart, ownerSection, projectUri, diagnostics, resolvingJavaTypeContext, javaTypeToResolve);
}
Also used : JavaMemberInfo(com.redhat.qute.commons.JavaMemberInfo) JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) Expression(com.redhat.qute.parser.template.Expression) MethodPart(com.redhat.qute.parser.expression.MethodPart) Part(com.redhat.qute.parser.expression.Part) PropertyPart(com.redhat.qute.parser.expression.PropertyPart) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) DiagnosticDataFactory.createDiagnostic(com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range)

Example 2 with ObjectPart

use of com.redhat.qute.parser.expression.ObjectPart in project quarkus-ls by redhat-developer.

the class JavaDataModelCache method resolveJavaType.

private CompletableFuture<ResolvedJavaTypeInfo> resolveJavaType(Parts parts, int partIndex, String projectUri, boolean nullIfDontMatchWithIterable) {
    CompletableFuture<ResolvedJavaTypeInfo> future = null;
    for (int i = 0; i < partIndex + 1; i++) {
        Part current = ((Part) parts.getChild(i));
        switch(current.getPartKind()) {
            case Object:
                ObjectPart objectPart = (ObjectPart) current;
                future = resolveJavaType(objectPart, projectUri, nullIfDontMatchWithIterable);
                break;
            case Property:
            case Method:
                if (future != null) {
                    future = // 
                    future.thenCompose(resolvedType -> {
                        if (resolvedType == null) {
                            return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                        }
                        return resolveJavaType(current, projectUri, resolvedType);
                    });
                }
                break;
            default:
        }
    }
    return future != null ? future : RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
}
Also used : Template(com.redhat.qute.parser.template.Template) ValueResolver(com.redhat.qute.project.datamodel.resolvers.ValueResolver) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) StringUtils(com.redhat.qute.utils.StringUtils) CompletableFuture(java.util.concurrent.CompletableFuture) QuteJavaTypesParams(com.redhat.qute.commons.QuteJavaTypesParams) NodeKind(com.redhat.qute.parser.template.NodeKind) Parameter(com.redhat.qute.parser.template.Parameter) QuteProjectRegistry(com.redhat.qute.project.QuteProjectRegistry) Expression(com.redhat.qute.parser.template.Expression) Section(com.redhat.qute.parser.template.Section) Location(org.eclipse.lsp4j.Location) Parts(com.redhat.qute.parser.expression.Parts) JavaMemberInfo(com.redhat.qute.commons.JavaMemberInfo) InvalidMethodReason(com.redhat.qute.commons.InvalidMethodReason) NamespaceResolverInfo(com.redhat.qute.commons.datamodel.resolvers.NamespaceResolverInfo) Node(com.redhat.qute.parser.template.Node) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) JavaMemberResult(com.redhat.qute.project.JavaMemberResult) JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Part(com.redhat.qute.parser.expression.Part) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) JavaElementInfo(com.redhat.qute.commons.JavaElementInfo) QuteJavaDefinitionParams(com.redhat.qute.commons.QuteJavaDefinitionParams) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Part(com.redhat.qute.parser.expression.Part) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo)

Example 3 with ObjectPart

use of com.redhat.qute.parser.expression.ObjectPart in project quarkus-ls by redhat-developer.

the class JavaDataModelCache method resolveJavaType.

private CompletableFuture<ResolvedJavaTypeInfo> resolveJavaType(ObjectPart objectPart, String projectUri, boolean nullIfDontMatchWithIterable) {
    CompletableFuture<ResolvedJavaTypeInfo> future = null;
    JavaTypeInfoProvider javaTypeInfo = objectPart.resolveJavaType();
    if (javaTypeInfo == null) {
        return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
    }
    String javaType = javaTypeInfo.getJavaType();
    if (StringUtils.isEmpty(javaType)) {
        Expression expression = javaTypeInfo.getJavaTypeExpression();
        if (expression != null) {
            String literalJavaType = expression.getLiteralJavaType();
            if (literalJavaType != null) {
                future = resolveJavaType(literalJavaType, projectUri);
            } else {
                Part lastPart = expression.getLastPart();
                if (lastPart == null) {
                    return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                }
                future = resolveJavaType(lastPart, projectUri);
            }
        }
    }
    if (future == null) {
        future = resolveJavaType(javaType, projectUri);
    }
    Node node = javaTypeInfo.getJavaTypeOwnerNode();
    Section section = getOwnerSection(node);
    if (section != null) {
        if (section.isIterable()) {
            future = // 
            future.thenCompose(resolvedType -> {
                if (resolvedType == null) {
                    return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                }
                if (!resolvedType.isIterable()) {
                    if (resolvedType.isInteger()) {
                        // {/for}
                        return resolveJavaType(resolvedType.getName(), projectUri);
                    }
                    if (nullIfDontMatchWithIterable) {
                        // {item.|}
                        return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                    }
                }
                // valid case
                // Ex:
                // {@java.util.List<org.acme.Item> items}
                // {#for item in items}
                // {item.|}
                // Here
                // - resolvedType = java.util.List<org.acme.Item>
                // - iterTypeName = org.acme.Item
                // Resolve org.acme.Item
                String iterTypeName = resolvedType.getIterableOf();
                return resolveJavaType(iterTypeName, projectUri);
            });
        }
    }
    return future;
}
Also used : Template(com.redhat.qute.parser.template.Template) ValueResolver(com.redhat.qute.project.datamodel.resolvers.ValueResolver) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) StringUtils(com.redhat.qute.utils.StringUtils) CompletableFuture(java.util.concurrent.CompletableFuture) QuteJavaTypesParams(com.redhat.qute.commons.QuteJavaTypesParams) NodeKind(com.redhat.qute.parser.template.NodeKind) Parameter(com.redhat.qute.parser.template.Parameter) QuteProjectRegistry(com.redhat.qute.project.QuteProjectRegistry) Expression(com.redhat.qute.parser.template.Expression) Section(com.redhat.qute.parser.template.Section) Location(org.eclipse.lsp4j.Location) Parts(com.redhat.qute.parser.expression.Parts) JavaMemberInfo(com.redhat.qute.commons.JavaMemberInfo) InvalidMethodReason(com.redhat.qute.commons.InvalidMethodReason) NamespaceResolverInfo(com.redhat.qute.commons.datamodel.resolvers.NamespaceResolverInfo) Node(com.redhat.qute.parser.template.Node) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) JavaMemberResult(com.redhat.qute.project.JavaMemberResult) JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Part(com.redhat.qute.parser.expression.Part) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) JavaElementInfo(com.redhat.qute.commons.JavaElementInfo) QuteJavaDefinitionParams(com.redhat.qute.commons.QuteJavaDefinitionParams) JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) Expression(com.redhat.qute.parser.template.Expression) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) Section(com.redhat.qute.parser.template.Section)

Example 4 with ObjectPart

use of com.redhat.qute.parser.expression.ObjectPart in project quarkus-ls by redhat-developer.

the class QuteCodeActions method doCodeActionsForUndefinedObject.

private static void doCodeActionsForUndefinedObject(Template template, Diagnostic diagnostic, QuteErrorCode errorCode, List<CodeAction> codeActions) {
    try {
        String varName = null;
        boolean isIterable = false;
        JsonObject data = (JsonObject) diagnostic.getData();
        if (data != null) {
            varName = data.get(DIAGNOSTIC_DATA_NAME).getAsString();
            isIterable = data.get(DIAGNOSTIC_DATA_ITERABLE).getAsBoolean();
        } else {
            int offset = template.offsetAt(diagnostic.getRange().getStart());
            Node node = template.findNodeAt(offset);
            node = QutePositionUtility.findBestNode(offset, node);
            if (node.getKind() == NodeKind.Expression) {
                Expression expression = (Expression) node;
                ObjectPart part = expression.getObjectPart();
                if (part != null) {
                    varName = part.getPartName();
                }
            }
        }
        if (varName != null) {
            TextDocument document = template.getTextDocument();
            String lineDelimiter = document.lineDelimiter(0);
            String title = MessageFormat.format(UNDEFINED_OBJECT_CODEACTION_TITLE, varName);
            Position position = new Position(0, 0);
            StringBuilder insertText = new StringBuilder("{@");
            if (isIterable) {
                insertText.append("java.util.List");
            } else {
                insertText.append("java.lang.String");
            }
            insertText.append(" ");
            insertText.append(varName);
            insertText.append("}");
            insertText.append(lineDelimiter);
            CodeAction insertParameterDeclarationQuickFix = CodeActionFactory.insert(title, position, insertText.toString(), document, diagnostic);
            codeActions.add(insertParameterDeclarationQuickFix);
            // CodeAction to set validation severity to ignore
            doCodeActionToSetIgnoreSeverity(template, Collections.singletonList(diagnostic), errorCode, codeActions, UNDEFINED_OBJECT_SEVERITY_SETTING);
        }
    } catch (BadLocationException e) {
    }
}
Also used : TextDocument(com.redhat.qute.ls.commons.TextDocument) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Expression(com.redhat.qute.parser.template.Expression) Position(org.eclipse.lsp4j.Position) Node(com.redhat.qute.parser.template.Node) CodeAction(org.eclipse.lsp4j.CodeAction) JsonObject(com.google.gson.JsonObject) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 5 with ObjectPart

use of com.redhat.qute.parser.expression.ObjectPart in project quarkus-ls by redhat-developer.

the class QuteSearchUtils method tryToCollectObjectParts.

/**
 * Try to collect all object parts from the given expression
 * <code>nodeExpr</code> which matches the given part name
 * <code>partName</code>.
 *
 * @param partName  the part name.
 * @param matcher   the matcher strategy.
 * @param nodeExpr  the Qute node expression
 * @param collector the node collector.
 *
 * @return true if the given nodeExpr reference the part name as object part and
 *         false otherwise.
 */
private static boolean tryToCollectObjectParts(String partName, PartNameMatcher matcher, Node nodeExpr, BiConsumer<Node, Range> collector) {
    switch(nodeExpr.getKind()) {
        case Expression:
            {
                Expression expression = (Expression) nodeExpr;
                boolean result = false;
                List<Node> nodes = expression.getExpressionContent();
                for (Node node : nodes) {
                    // ex : partName=item
                    // node expression can be
                    // 1) a simple expression:
                    // --> {item_count} --> [ObjectPart=item_count]
                    // --> {item.name} --> [ObjectPart=item, PropertyPart=name)
                    // --> {item.getName()} --> [ObjectPart=item, MethodPart=getName())
                    // In those cases, one object part will be collected =>{|item|_count},
                    // {|item|.name}, {|item|.getName()}
                    // 2) an expression with method part which can host another expressions when
                    // method have parameters
                    // --> {item.getName(item.name)} --> [ObjectPart=item,
                    // MethodPart=getName(item.name))
                    // In this case, two object parts will be collected =>
                    // {|item|.getName(|item|.name)}
                    result |= tryToCollectObjectParts(partName, matcher, node, collector);
                }
                return result;
            }
        case ExpressionParts:
            {
                Parts parts = (Parts) nodeExpr;
                boolean result = false;
                for (Node partNode : parts.getChildren()) {
                    result |= tryToCollectObjectParts(partName, matcher, partNode, collector);
                }
                return result;
            }
        case ExpressionPart:
            {
                Part part = (Part) nodeExpr;
                if (part.getPartKind() == PartKind.Object) {
                    ObjectPart objectPart = (ObjectPart) part;
                    if (isMatch(objectPart, partName, matcher)) {
                        Range range = QutePositionUtility.createRange(objectPart);
                        collector.accept(objectPart, range);
                        return true;
                    }
                } else if (part.getPartKind() == PartKind.Method) {
                    MethodPart methodPart = (MethodPart) part;
                    List<Parameter> parameters = methodPart.getParameters();
                    boolean result = false;
                    for (Parameter parameter : parameters) {
                        Expression paramExpr = parameter.getJavaTypeExpression();
                        if (paramExpr != null) {
                            result |= tryToCollectObjectParts(partName, matcher, paramExpr, collector);
                        }
                    }
                    return result;
                }
            }
            break;
        default:
            break;
    }
    return false;
}
Also used : ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Expression(com.redhat.qute.parser.template.Expression) Parts(com.redhat.qute.parser.expression.Parts) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) MethodPart(com.redhat.qute.parser.expression.MethodPart) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) Parameter(com.redhat.qute.parser.template.Parameter) List(java.util.List) Range(org.eclipse.lsp4j.Range) MethodPart(com.redhat.qute.parser.expression.MethodPart)

Aggregations

ObjectPart (com.redhat.qute.parser.expression.ObjectPart)9 Part (com.redhat.qute.parser.expression.Part)6 Expression (com.redhat.qute.parser.template.Expression)5 Node (com.redhat.qute.parser.template.Node)5 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)4 JavaTypeInfoProvider (com.redhat.qute.parser.template.JavaTypeInfoProvider)4 Range (org.eclipse.lsp4j.Range)4 JavaMemberInfo (com.redhat.qute.commons.JavaMemberInfo)3 MethodPart (com.redhat.qute.parser.expression.MethodPart)3 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)3 Parts (com.redhat.qute.parser.expression.Parts)3 Parameter (com.redhat.qute.parser.template.Parameter)3 Section (com.redhat.qute.parser.template.Section)3 List (java.util.List)3 InvalidMethodReason (com.redhat.qute.commons.InvalidMethodReason)2 JavaElementInfo (com.redhat.qute.commons.JavaElementInfo)2 JavaTypeInfo (com.redhat.qute.commons.JavaTypeInfo)2 QuteJavaDefinitionParams (com.redhat.qute.commons.QuteJavaDefinitionParams)2 QuteJavaTypesParams (com.redhat.qute.commons.QuteJavaTypesParams)2 NamespaceResolverInfo (com.redhat.qute.commons.datamodel.resolvers.NamespaceResolverInfo)2