Search in sources :

Example 16 with Parameter

use of com.redhat.qute.parser.template.Parameter 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 17 with Parameter

use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.

the class ObjectPart method resolveJavaType.

public JavaTypeInfoProvider resolveJavaType() {
    Template template = super.getOwnerTemplate();
    String partName = getPartName();
    boolean hasNamespace = getNamespace() != null;
    if (hasNamespace) {
        // ex : {data:item}
        return template.findWithNamespace(this);
    }
    // ex : {item}
    // Loop for parent section to discover the class name
    Section section = super.getParentSection();
    while (section != null) {
        switch(section.getSectionKind()) {
            case EACH:
            case FOR:
                LoopSection iterableSection = (LoopSection) section;
                if (!iterableSection.isInElseBlock(getStart())) {
                    String alias = iterableSection.getAlias();
                    if (partName.equals(alias)) {
                        return iterableSection.getIterableParameter();
                    }
                }
                break;
            case LET:
            case SET:
                List<Parameter> parameters = section.getParameters();
                for (Parameter parameter : parameters) {
                    if (partName.equals(parameter.getName())) {
                        return parameter;
                    }
                }
                break;
            default:
        }
        // ex : count for #each
        JavaTypeInfoProvider metadata = section.getMetadata(partName);
        if (metadata != null) {
            return metadata;
        }
        section = section.getParentSection();
    }
    // - from @CheckedTemplate
    return template.findInInitialDataModel(this);
}
Also used : JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Parameter(com.redhat.qute.parser.template.Parameter) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Section(com.redhat.qute.parser.template.Section) Template(com.redhat.qute.parser.template.Template)

Example 18 with Parameter

use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.

the class QuteCodeLens method collectInsertCodeLenses.

private static void collectInsertCodeLenses(Node parent, Template template, QuteProject project, List<CodeLens> lenses, CancelChecker cancelChecker) {
    cancelChecker.checkCanceled();
    if (parent.getKind() == NodeKind.Section) {
        Section section = (Section) parent;
        if (section.getSectionKind() == SectionKind.INSERT) {
            if (project != null) {
                Parameter parameter = section.getParameterAtIndex(0);
                if (parameter != null) {
                    String tag = parameter.getValue();
                    // TODO : implement findNbreferencesOfInsertTag correctly
                    // project.findNbreferencesOfInsertTag(template.getTemplateId(), tag);
                    int nbReferences = 0;
                    if (nbReferences > 0) {
                        String title = nbReferences == 1 ? "1 reference" : nbReferences + " references";
                        Range range = QutePositionUtility.createRange(parameter);
                        Command command = new Command(title, "");
                        CodeLens codeLens = new CodeLens(range, command, null);
                        lenses.add(codeLens);
                    }
                }
            }
        }
    }
    List<Node> children = parent.getChildren();
    for (Node node : children) {
        collectInsertCodeLenses(node, template, project, lenses, cancelChecker);
    }
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Node(com.redhat.qute.parser.template.Node) Parameter(com.redhat.qute.parser.template.Parameter) ExtendedDataModelParameter(com.redhat.qute.project.datamodel.ExtendedDataModelParameter) Range(org.eclipse.lsp4j.Range) Section(com.redhat.qute.parser.template.Section)

Example 19 with Parameter

use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.

the class QuteHover method doHoverForParameter.

private CompletableFuture<Hover> doHoverForParameter(Parameter parameter, Template template, HoverRequest hoverRequest) {
    int offset = hoverRequest.getOffset();
    if (parameter.isInName(offset)) {
        // A parameter name is hovered
        if (parameter.getOwnerSection() != null && (parameter.getOwnerSection().getSectionKind() == SectionKind.FOR || parameter.getOwnerSection().getSectionKind() == SectionKind.EACH)) {
            // a parameter from #for section is hovered
            LoopSection iterableSection = (LoopSection) parameter.getOwnerSection();
            if (iterableSection.isInAlias(offset)) {
                Parameter iterableParameter = iterableSection.getIterableParameter();
                if (iterableParameter != null) {
                    boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
                    String projectUri = template.getProjectUri();
                    Part iterablePart = iterableParameter.getJavaTypeExpression().getLastPart();
                    return // 
                    javaCache.resolveJavaType(iterablePart, projectUri).thenCompose(resolvedJavaType -> {
                        if (resolvedJavaType != null && resolvedJavaType.isIterable()) {
                            return // 
                            javaCache.resolveJavaType(resolvedJavaType.getIterableOf(), projectUri).thenApply(resolvedIterableOf -> {
                                MarkupContent content = DocumentationUtils.getDocumentation(resolvedIterableOf, hasMarkdown);
                                Range range = QutePositionUtility.createRange(parameter);
                                return new Hover(content, range);
                            });
                        }
                        return NO_HOVER;
                    });
                }
            }
        } else {
            // Other parameter
            // Check if part is a literal (ex: true, null, 123, 'abc', etc)
            Expression expression = parameter.getJavaTypeExpression();
            if (expression != null) {
                String projectUri = template.getProjectUri();
                String literalJavaType = expression.getLiteralJavaType();
                if (literalJavaType != null) {
                    return // 
                    javaCache.resolveJavaType(literalJavaType, projectUri).thenApply(resolvedJavaType -> {
                        if (resolvedJavaType != null) {
                            boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
                            MarkupContent content = DocumentationUtils.getDocumentation(resolvedJavaType, hasMarkdown);
                            Range range = QutePositionUtility.createRange(parameter);
                            return new Hover(content, range);
                        }
                        return null;
                    });
                }
                return // 
                javaCache.resolveJavaType(parameter, projectUri).thenApply(resolvedJavaType -> {
                    if (resolvedJavaType != null) {
                        boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
                        MarkupContent content = DocumentationUtils.getDocumentation(resolvedJavaType, hasMarkdown);
                        Range range = QutePositionUtility.createRange(parameter);
                        return new Hover(content, range);
                    }
                    return null;
                });
            }
        }
    }
    return NO_HOVER;
}
Also used : LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Expression(com.redhat.qute.parser.template.Expression) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) Hover(org.eclipse.lsp4j.Hover) Parameter(com.redhat.qute.parser.template.Parameter) Range(org.eclipse.lsp4j.Range) MarkupContent(org.eclipse.lsp4j.MarkupContent)

Example 20 with Parameter

use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.

the class QuteHover method doHover.

public CompletableFuture<Hover> doHover(Template template, Position position, SharedSettings settings, CancelChecker cancelChecker) {
    cancelChecker.checkCanceled();
    HoverRequest hoverRequest = null;
    try {
        hoverRequest = new HoverRequest(template, position, settings);
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "Failed creating HoverRequest", e);
        return NO_HOVER;
    }
    Node node = hoverRequest.getNode();
    if (node == null) {
        return NO_HOVER;
    }
    switch(node.getKind()) {
        case Section:
            // - Start end tag definition
            Section section = (Section) node;
            return doHoverForSection(section, template, hoverRequest, cancelChecker);
        case ParameterDeclaration:
            ParameterDeclaration parameterDeclaration = (ParameterDeclaration) node;
            return doHoverForParameterDeclaration(parameterDeclaration, template, hoverRequest, cancelChecker);
        case ExpressionPart:
            Part part = (Part) node;
            return doHoverForExpressionPart(part, template, hoverRequest, cancelChecker);
        case Parameter:
            Parameter parameter = (Parameter) node;
            return doHoverForParameter(parameter, template, hoverRequest);
        default:
            return NO_HOVER;
    }
}
Also used : HoverRequest(com.redhat.qute.services.hover.HoverRequest) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) Parameter(com.redhat.qute.parser.template.Parameter) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Section(com.redhat.qute.parser.template.Section) BadLocationException(com.redhat.qute.ls.commons.BadLocationException) ParameterDeclaration(com.redhat.qute.parser.template.ParameterDeclaration)

Aggregations

Parameter (com.redhat.qute.parser.template.Parameter)30 Node (com.redhat.qute.parser.template.Node)9 Section (com.redhat.qute.parser.template.Section)9 Range (org.eclipse.lsp4j.Range)9 Expression (com.redhat.qute.parser.template.Expression)6 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)6 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)3 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)3 Part (com.redhat.qute.parser.expression.Part)3 ParameterDeclaration (com.redhat.qute.parser.template.ParameterDeclaration)3 DiagnosticDataFactory.createDiagnostic (com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Diagnostic (org.eclipse.lsp4j.Diagnostic)3 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)2 MethodPart (com.redhat.qute.parser.expression.MethodPart)2 RangeOffset (com.redhat.qute.parser.template.RangeOffset)2 Template (com.redhat.qute.parser.template.Template)2 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)2