Search in sources :

Example 11 with Part

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

the class QuteDiagnostics method validateExpressionParts.

private ResolvedJavaTypeInfo validateExpressionParts(Parts parts, Section ownerSection, Template template, String projectUri, QuteValidationSettings validationSettings, ResolutionContext resolutionContext, ResolvingJavaTypeContext resolvingJavaTypeContext, List<Diagnostic> diagnostics) {
    ResolvedJavaTypeInfo resolvedJavaType = null;
    String namespace = null;
    for (int i = 0; i < parts.getChildCount(); i++) {
        Part current = parts.getChild(i);
        if (current.isLast()) {
            // It's the last part, check if it is not ended with '.'
            int end = current.getEnd();
            String text = template.getText();
            if (end < text.length()) {
                char c = text.charAt(end);
                if (c == '.') {
                    Range range = QutePositionUtility.createRange(end, end + 1, template);
                    Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Error, QuteErrorCode.SyntaxError, "Unexpected '.' token.");
                    diagnostics.add(diagnostic);
                }
            }
        }
        switch(current.getPartKind()) {
            case Namespace:
                {
                    NamespacePart namespacePart = (NamespacePart) current;
                    namespace = validateNamespace(namespacePart, projectUri, validationSettings, resolvingJavaTypeContext, diagnostics);
                    if (namespace == null) {
                        // Invalid namespace
                        return null;
                    }
                    break;
                }
            case Object:
                {
                    ObjectPart objectPart = (ObjectPart) current;
                    resolvedJavaType = validateObjectPart(namespace, objectPart, ownerSection, template, projectUri, validationSettings, resolutionContext, diagnostics, resolvingJavaTypeContext);
                    if (resolvedJavaType == null) {
                        // property, method.
                        return null;
                    }
                    break;
                }
            case Method:
            case Property:
                {
                    // java.util.List<org.acme.Item>
                    ResolvedJavaTypeInfo iter = resolvedJavaType;
                    if (resolvedJavaType != null && resolvedJavaType.isIterable() && !resolvedJavaType.isArray()) {
                        // Expression uses iterable type
                        // {@java.util.List<org.acme.Item items>
                        // {items.size()}
                        // Property, method to validate must be done for iterable type (ex :
                        // java.util.List
                        String iterableType = resolvedJavaType.getIterableType();
                        resolvedJavaType = resolveJavaType(iterableType, projectUri, resolvingJavaTypeContext);
                        if (resolvedJavaType == null || isResolvingJavaType(resolvedJavaType)) {
                            // The java type doesn't exists or it is resolving, stop the validation
                            return null;
                        }
                    }
                    resolvedJavaType = validateMemberPart(current, ownerSection, template, projectUri, validationSettings, resolutionContext, resolvedJavaType, iter, diagnostics, resolvingJavaTypeContext);
                    if (resolvedJavaType == null) {
                        // followings property, method.
                        return null;
                    }
                    break;
                }
        }
    }
    return resolvedJavaType;
}
Also used : ObjectPart(com.redhat.qute.parser.expression.ObjectPart) 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) 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 12 with Part

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

the class QuteHighlighting method findDocumentHighlights.

public List<DocumentHighlight> findDocumentHighlights(Template template, Position position, CancelChecker cancelChecker) {
    try {
        List<DocumentHighlight> highlights = new ArrayList<>();
        int offset = template.offsetAt(position);
        Node node = template.findNodeAt(offset);
        if (node == null) {
            return Collections.emptyList();
        }
        node = QutePositionUtility.findBestNode(offset, node);
        switch(node.getKind()) {
            case ParameterDeclaration:
            case Parameter:
                highlightReferenceObjectPart(node, offset, highlights, cancelChecker);
                break;
            case ExpressionPart:
                Part part = (Part) node;
                if (part.getPartKind() == PartKind.Object) {
                    highlightDeclaredObject((ObjectPart) part, highlights, cancelChecker);
                }
                break;
            case Section:
                higlightSection((Section) node, offset, position, highlights, cancelChecker);
                break;
            default:
        }
        return highlights;
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "In QuteHighlighting the client provided Position is at a BadLocation", e);
        return Collections.emptyList();
    }
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) ArrayList(java.util.ArrayList) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 13 with Part

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

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

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