Search in sources :

Example 1 with Part

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

the class QuteCompletions method doComplete.

/**
 * Returns completion list for the given position
 *
 * @param template           the Qute template
 * @param position           the position where completion was triggered
 * @param completionSettings the completion settings.
 * @param formattingSettings the formatting settings.
 * @param cancelChecker      the cancel checker
 * @return completion list for the given position
 */
public CompletableFuture<CompletionList> doComplete(Template template, Position position, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings, CancelChecker cancelChecker) {
    CompletionRequest completionRequest = null;
    try {
        completionRequest = new CompletionRequest(template, position, completionSettings, formattingSettings);
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "Creation of CompletionRequest failed", e);
        return EMPTY_FUTURE_COMPLETION;
    }
    Node node = completionRequest.getNode();
    if (node == null) {
        return EMPTY_FUTURE_COMPLETION;
    }
    String text = template.getText();
    int offset = completionRequest.getOffset();
    if (node.getKind() == NodeKind.Expression || node.getKind() == NodeKind.ExpressionParts || node.getKind() == NodeKind.ExpressionPart) {
        Expression expression = null;
        Node nodeExpression = null;
        if (node.getKind() == NodeKind.Expression) {
            expression = (Expression) node;
        } else if (node.getKind() == NodeKind.ExpressionParts) {
            nodeExpression = node;
            expression = ((Parts) node).getParent();
        } else if (node.getKind() == NodeKind.ExpressionPart) {
            nodeExpression = node;
            expression = ((Part) node).getParent().getParent();
        }
        return completionForExpression.doCompleteExpression(completionRequest, expression, nodeExpression, template, offset, completionSettings, formattingSettings, cancelChecker);
    } else if (node.getKind() == NodeKind.Text) {
        // The completion is triggered in text node (before node)
        Section parent = node.getParentSection();
        if (parent != null && (parent.isInEndTagName(offset))) {
            // The completion is triggered inside end tag
            return EMPTY_FUTURE_COMPLETION;
        }
        // The completion is triggered in text node
        // Check if completion is triggered after a start bracket character and if it's
        // a valid expression
        int nbBrackets = 0;
        int bracketOffset = offset - 1;
        char previousChar = text.charAt(bracketOffset);
        if (previousChar == '#') {
            // {#
            bracketOffset--;
        }
        while (bracketOffset >= 0 && text.charAt(bracketOffset) == '{') {
            bracketOffset--;
            nbBrackets++;
        }
        if (nbBrackets > 0) {
            if (nbBrackets % 2 != 0) {
                // The completion is triggered in text node after bracket '{' character
                return completionForExpression.doCompleteExpression(completionRequest, null, node, template, offset, completionSettings, formattingSettings, cancelChecker);
            }
            return EMPTY_FUTURE_COMPLETION;
        }
    } else if (node.getKind() == NodeKind.ParameterDeclaration) {
        return completionsForParameterDeclaration.doCollectJavaClassesSuggestions((ParameterDeclaration) node, template, offset, completionSettings);
    } else if (node.getKind() == NodeKind.Section) {
        // {#|}
        return completionForTagSection.doCompleteTagSection(completionRequest, completionSettings, formattingSettings, cancelChecker);
    }
    return collectSnippetSuggestions(completionRequest);
}
Also used : Expression(com.redhat.qute.parser.template.Expression) QuteCompletionsForExpression(com.redhat.qute.services.completions.QuteCompletionsForExpression) Parts(com.redhat.qute.parser.expression.Parts) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) CompletionRequest(com.redhat.qute.services.completions.CompletionRequest) QuteCompletionForTagSection(com.redhat.qute.services.completions.QuteCompletionForTagSection) Section(com.redhat.qute.parser.template.Section) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 2 with Part

use of com.redhat.qute.parser.expression.Part 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 3 with Part

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

the class QuteHover method doHoverForPropertyPart.

private CompletableFuture<Hover> doHoverForPropertyPart(Part part, String projectUri, HoverRequest hoverRequest) {
    Parts parts = part.getParent();
    Part previousPart = parts.getPreviousPart(part);
    return // 
    javaCache.resolveJavaType(previousPart, projectUri).thenCompose(resolvedJavaType -> {
        if (resolvedJavaType != null) {
            if (resolvedJavaType.isIterable() && !resolvedJavaType.isArray()) {
                // Expression uses iterable type
                // {@java.util.List<org.acme.Item items>
                // {items.si|ze()}
                // Property, method to find as hover must be done for iterable type (ex :
                // java.util.List>
                String iterableType = resolvedJavaType.getIterableType();
                CompletableFuture<ResolvedJavaTypeInfo> iterableResolvedTypeFuture = javaCache.resolveJavaType(iterableType, projectUri);
                return iterableResolvedTypeFuture.thenApply((iterableResolvedType) -> {
                    return doHoverForPropertyPart(part, projectUri, iterableResolvedType, resolvedJavaType, hoverRequest);
                });
            }
            Hover hover = doHoverForPropertyPart(part, projectUri, resolvedJavaType, null, hoverRequest);
            return CompletableFuture.completedFuture(hover);
        }
        return NO_HOVER;
    });
}
Also used : Parts(com.redhat.qute.parser.expression.Parts) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) Hover(org.eclipse.lsp4j.Hover) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo)

Example 4 with Part

use of com.redhat.qute.parser.expression.Part 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 5 with Part

use of com.redhat.qute.parser.expression.Part 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)

Aggregations

Part (com.redhat.qute.parser.expression.Part)14 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)10 ObjectPart (com.redhat.qute.parser.expression.ObjectPart)9 Parts (com.redhat.qute.parser.expression.Parts)7 Node (com.redhat.qute.parser.template.Node)7 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)6 MethodPart (com.redhat.qute.parser.expression.MethodPart)6 Expression (com.redhat.qute.parser.template.Expression)6 Parameter (com.redhat.qute.parser.template.Parameter)5 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)4 PropertyPart (com.redhat.qute.parser.expression.PropertyPart)4 Section (com.redhat.qute.parser.template.Section)4 Range (org.eclipse.lsp4j.Range)4 JavaMemberInfo (com.redhat.qute.commons.JavaMemberInfo)3 JavaTypeInfoProvider (com.redhat.qute.parser.template.JavaTypeInfoProvider)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