Search in sources :

Example 6 with Part

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

the class Expression method getLastPart.

/**
 * Returns the last part of the expression and null otherwise.
 *
 * @return the last part of the expression and null otherwise.
 */
public Part getLastPart() {
    List<Node> nodes = getExpressionContent();
    if (nodes.isEmpty()) {
        return null;
    }
    Parts parts = (Parts) nodes.get(0);
    return (Part) parts.getLastChild();
}
Also used : Parts(com.redhat.qute.parser.expression.Parts) Part(com.redhat.qute.parser.expression.Part) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart)

Example 7 with Part

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

the class QuteCompletionsForExpression method doCompleteExpressionForMemberPart.

/**
 * Returns the completion result for Java fields, methods of the Java type
 * class, interface of the given part <code>part</code>
 *
 * @param part               the part.
 * @param parts              the owner parts.
 * @param template           the owner template.
 * @param completionSettings the completion settings.
 * @param formattingSettings the formatting settings.
 *
 * @return the completion list.
 */
private CompletableFuture<CompletionList> doCompleteExpressionForMemberPart(Part part, Parts parts, Template template, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings) {
    int start = part != null ? part.getStart() : parts.getEnd();
    int end = part != null ? part.getEnd() : parts.getEnd();
    String projectUri = template.getProjectUri();
    Part previousPart = parts.getPreviousPart(part);
    return // 
    javaCache.resolveJavaType(previousPart, projectUri).thenCompose(resolvedType -> {
        if (resolvedType == null) {
            return EMPTY_FUTURE_COMPLETION;
        }
        if (resolvedType.isIterable() && !resolvedType.isArray()) {
            // 'java.util.List<org.acme.Item>' Java class iterable
            return // 
            javaCache.resolveJavaType(resolvedType.getIterableType(), projectUri).thenApply(resolvedIterableType -> {
                if (resolvedIterableType == null) {
                    return EMPTY_COMPLETION;
                }
                return doCompleteForJavaTypeMembers(resolvedIterableType, start, end, template, completionSettings, formattingSettings);
            });
        }
        // Completion for member of the given Java class
        // ex : org.acme.Item
        CompletionList list = doCompleteForJavaTypeMembers(resolvedType, start, end, template, completionSettings, formattingSettings);
        return CompletableFuture.completedFuture(list);
    });
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) MethodPart(com.redhat.qute.parser.expression.MethodPart) Part(com.redhat.qute.parser.expression.Part) NamespacePart(com.redhat.qute.parser.expression.NamespacePart)

Example 8 with Part

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

Example 9 with Part

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

the class QuteDefinition method findDefinition.

public CompletableFuture<List<? extends LocationLink>> findDefinition(Template template, Position position, CancelChecker cancelChecker) {
    try {
        DefinitionRequest definitionRequest = new DefinitionRequest(template, position);
        Node node = definitionRequest.getNode();
        if (node == null) {
            return NO_DEFINITION;
        }
        int offset = definitionRequest.getOffset();
        switch(node.getKind()) {
            case Section:
                // - Java data model definition
                return findDefinitionFromSection(offset, (Section) node, template, cancelChecker);
            case ParameterDeclaration:
                // Return Java class definition
                return findDefinitionFromParameterDeclaration(offset, (ParameterDeclaration) node, template);
            case Expression:
                return findDefinitionFromExpression(offset, (Expression) node, template, cancelChecker);
            case ExpressionPart:
                Part part = (Part) node;
                return findDefinitionFromPart(part, template, cancelChecker);
            default:
                // no definitions
                return NO_DEFINITION;
        }
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "Failed creating DefinitionRequest", e);
        return NO_DEFINITION;
    }
}
Also used : DefinitionRequest(com.redhat.qute.services.definition.DefinitionRequest) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) MethodPart(com.redhat.qute.parser.expression.MethodPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) PropertyPart(com.redhat.qute.parser.expression.PropertyPart) Node(com.redhat.qute.parser.template.Node) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 10 with Part

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

the class QuteDefinition method findDefinitionFromPropertyPart.

private CompletableFuture<List<? extends LocationLink>> findDefinitionFromPropertyPart(Part part, Template template) {
    String projectUri = template.getProjectUri();
    if (projectUri != null) {
        Parts parts = part.getParent();
        Part previousPart = parts.getPreviousPart(part);
        return // 
        javaCache.resolveJavaType(previousPart, projectUri).thenCompose(previousResolvedType -> {
            if (previousResolvedType != null) {
                if (previousResolvedType.isIterable()) {
                    // Expression uses iterable type
                    // {@java.util.List<org.acme.Item items>
                    // {items.si|ze()}
                    // Property, method to find as definition must be done for iterable type (ex :
                    // java.util.List>
                    String iterableType = previousResolvedType.getIterableType();
                    CompletableFuture<ResolvedJavaTypeInfo> iterableResolvedTypeFuture = javaCache.resolveJavaType(iterableType, projectUri);
                    return iterableResolvedTypeFuture.thenCompose((iterableResolvedType) -> {
                        return findDefinitionFromPropertyPart(part, projectUri, iterableResolvedType);
                    });
                }
                return findDefinitionFromPropertyPart(part, projectUri, previousResolvedType);
            }
            return NO_DEFINITION;
        });
    }
    return NO_DEFINITION;
}
Also used : Parts(com.redhat.qute.parser.expression.Parts) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) MethodPart(com.redhat.qute.parser.expression.MethodPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) PropertyPart(com.redhat.qute.parser.expression.PropertyPart) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo)

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