Search in sources :

Example 1 with SectionMetadata

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

the class QuteHover method doHoverForObjectPart.

private CompletableFuture<Hover> doHoverForObjectPart(Part part, String projectUri, HoverRequest hoverRequest) {
    if (UserTagUtils.isUserTag(hoverRequest.getTemplate())) {
        // It's an user tag
        SectionMetadata specialKey = UserTagUtils.getSpecialKey(part.getPartName());
        if (specialKey != null) {
            // its a special key for user tag ({it} or {nested-content), display the special
            // key documentation.
            boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
            MarkupContent content = DocumentationUtils.getDocumentation(specialKey, hasMarkdown);
            Range range = QutePositionUtility.createRange(part);
            Hover hover = new Hover(content, range);
            return CompletableFuture.completedFuture(hover);
        }
    }
    // Check if part is a literal (ex: true, null, 123, 'abc', etc)
    Expression expression = part.getParent().getParent();
    String literalJavaType = expression.getLiteralJavaType();
    if (literalJavaType != null) {
        return doHoverForJavaType(part, literalJavaType, projectUri, hoverRequest);
    }
    return // 
    javaCache.resolveJavaType(part, projectUri).thenApply(resolvedJavaType -> {
        if (resolvedJavaType != null) {
            SectionMetadata metadata = getMetadata(part);
            boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
            MarkupContent content = DocumentationUtils.getDocumentation(resolvedJavaType, metadata != null ? metadata.getDescription() : null, hasMarkdown);
            Range range = QutePositionUtility.createRange(part);
            return new Hover(content, range);
        }
        return null;
    });
}
Also used : Expression(com.redhat.qute.parser.template.Expression) Hover(org.eclipse.lsp4j.Hover) SectionMetadata(com.redhat.qute.parser.template.SectionMetadata) Range(org.eclipse.lsp4j.Range) MarkupContent(org.eclipse.lsp4j.MarkupContent)

Example 2 with SectionMetadata

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

the class QuteCompletionsForExpression method doCompleteExpressionForObjectPartWithParentNodes.

private void doCompleteExpressionForObjectPartWithParentNodes(Node part, Node node, Range range, int offset, String projectUri, Set<String> existingVars, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings, CompletionList list) {
    Section section = node != null ? node.getParentSection() : null;
    if (section == null) {
        return;
    }
    if (section.getKind() == NodeKind.Section) {
        boolean collect = true;
        if (section.getSectionKind() == SectionKind.FOR || section.getSectionKind() == SectionKind.EACH) {
            LoopSection iterableSection = ((LoopSection) section);
            if (iterableSection.isInElseBlock(offset)) {
                // Completion is triggered after a #else inside a #for, we don't provide
                // completion for metadata or aliases
                collect = false;
            }
        }
        if (collect) {
            // 1) Completion for metadata section
            List<SectionMetadata> metadatas = section.getMetadata();
            for (SectionMetadata metadata : metadatas) {
                String name = metadata.getName();
                if (!existingVars.contains(name)) {
                    existingVars.add(name);
                    CompletionItem item = new CompletionItem();
                    item.setLabel(name);
                    item.setKind(CompletionItemKind.Keyword);
                    // Display metadata section (ex : count for #each) after declared objects
                    item.setSortText("Za" + name);
                    TextEdit textEdit = new TextEdit(range, name);
                    item.setTextEdit(Either.forLeft(textEdit));
                    item.setDetail(metadata.getDescription());
                    list.getItems().add(item);
                }
            }
            // 2) Completion for aliases section
            switch(section.getSectionKind()) {
                case EACH:
                case FOR:
                    LoopSection iterableSection = ((LoopSection) section);
                    // Completion for iterable section like #each, #for
                    String alias = iterableSection.getAlias();
                    if (!StringUtils.isEmpty(alias)) {
                        if (!existingVars.contains(alias)) {
                            existingVars.add(alias);
                            CompletionItem item = new CompletionItem();
                            item.setLabel(alias);
                            item.setKind(CompletionItemKind.Reference);
                            TextEdit textEdit = new TextEdit(range, alias);
                            item.setTextEdit(Either.forLeft(textEdit));
                            list.getItems().add(item);
                        }
                    }
                    break;
                case LET:
                case SET:
                    // completion for parameters coming from #let, #set
                    List<Parameter> parameters = section.getParameters();
                    if (parameters != null) {
                        for (Parameter parameter : parameters) {
                            String parameterName = parameter.getName();
                            if (!existingVars.contains(parameterName)) {
                                existingVars.add(parameterName);
                                CompletionItem item = new CompletionItem();
                                item.setLabel(parameterName);
                                item.setKind(CompletionItemKind.Reference);
                                TextEdit textEdit = new TextEdit(range, parameterName);
                                item.setTextEdit(Either.forLeft(textEdit));
                                list.getItems().add(item);
                            }
                        }
                    }
                    break;
                case WITH:
                    // Completion for properties/methods of with object from #with
                    Parameter object = ((WithSection) section).getObjectParameter();
                    if (object != null) {
                        ResolvedJavaTypeInfo withJavaTypeInfo = javaCache.resolveJavaType(object, projectUri).getNow(null);
                        if (withJavaTypeInfo != null) {
                            fillCompletionFields(withJavaTypeInfo, range, projectUri, existingVars, list);
                            fillCompletionMethods(withJavaTypeInfo, range, projectUri, completionSettings, formattingSettings, existingVars, new HashSet<>(), list);
                        }
                    }
                    break;
                default:
            }
        }
    }
    doCompleteExpressionForObjectPartWithParentNodes(part, section, range, offset, projectUri, existingVars, completionSettings, formattingSettings, list);
}
Also used : WithSection(com.redhat.qute.parser.template.sections.WithSection) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) WithSection(com.redhat.qute.parser.template.sections.WithSection) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Section(com.redhat.qute.parser.template.Section) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextEdit(org.eclipse.lsp4j.TextEdit) SectionMetadata(com.redhat.qute.parser.template.SectionMetadata) Parameter(com.redhat.qute.parser.template.Parameter) ExtendedDataModelParameter(com.redhat.qute.project.datamodel.ExtendedDataModelParameter)

Example 3 with SectionMetadata

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

the class QuteCompletionsForExpression method doCompleteExpressionForObjectPart.

private CompletableFuture<CompletionList> doCompleteExpressionForObjectPart(CompletionRequest completionRequest, Expression expression, String namespace, Node part, int offset, Template template, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings, CancelChecker cancelChecker) {
    // Completion for root object
    int partStart = part != null && part.getKind() != NodeKind.Text ? part.getStart() : offset;
    int partEnd = part != null && part.getKind() != NodeKind.Text ? part.getEnd() : offset;
    Range range = QutePositionUtility.createRange(partStart, partEnd, template);
    CompletionList list = new CompletionList();
    // Completion with namespace resolver
    // {data:item}
    // {inject:bean}
    doCompleteNamespaceResolvers(namespace, template, range, completionSettings, formattingSettings, list);
    if (namespace == null) {
        // Completion is triggered before the namespace
        // Collect alias declared from parameter declaration
        doCompleteExpressionForObjectPartWithParameterAlias(template, range, list);
        // Collect parameters from CheckedTemplate method parameters
        doCompleteExpressionForObjectPartWithCheckedTemplate(template, range, list);
        // Collect declared model inside section, let, etc
        Set<String> existingVars = new HashSet<>();
        doCompleteExpressionForObjectPartWithParentNodes(part, expression != null ? expression : part, range, offset, template.getProjectUri(), existingVars, completionSettings, formattingSettings, list);
        // Section tag
        if (completionRequest != null) {
            char previous = template.getText().charAt(offset - 1);
            if (previous == '#') {
                completionForTagSection.doCompleteTagSection(completionRequest, "#", completionSettings, formattingSettings, cancelChecker, list);
            }
        }
        if (UserTagUtils.isUserTag(template)) {
            // provide completion for 'it' and 'nested-content'
            Collection<SectionMetadata> metadatas = UserTagUtils.getSpecialKeys();
            for (SectionMetadata metadata : metadatas) {
                String name = metadata.getName();
                if (!existingVars.contains(name)) {
                    existingVars.add(name);
                    CompletionItem item = new CompletionItem();
                    item.setLabel(name);
                    item.setKind(CompletionItemKind.Keyword);
                    // Display metadata section (ex : count for #each) after declared objects
                    item.setSortText("Za" + name);
                    TextEdit textEdit = new TextEdit(range, name);
                    item.setTextEdit(Either.forLeft(textEdit));
                    item.setDetail(metadata.getDescription());
                    list.getItems().add(item);
                }
            }
        }
    }
    return CompletableFuture.completedFuture(list);
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextEdit(org.eclipse.lsp4j.TextEdit) SectionMetadata(com.redhat.qute.parser.template.SectionMetadata) Range(org.eclipse.lsp4j.Range) HashSet(java.util.HashSet)

Example 4 with SectionMetadata

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

the class LoopSection method loadMetadata.

private synchronized List<SectionMetadata> loadMetadata() {
    if (metadata != null) {
        return metadata;
    }
    String metadataPrefix = getOwnerTemplate().getConfiguration().getIterationMetadataPrefix();
    String prefix = prefixValue(getAlias(), metadataPrefix);
    if (prefix == null) {
        metadata = METADATA;
    } else {
        metadata = // 
        METADATA.stream().map(// 
        m -> new SectionMetadata(prefix + m.getName(), m.getJavaType(), m.getDescription())).collect(Collectors.toList());
    }
    return metadata;
}
Also used : SectionMetadata(com.redhat.qute.parser.template.SectionMetadata)

Aggregations

SectionMetadata (com.redhat.qute.parser.template.SectionMetadata)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)2 Range (org.eclipse.lsp4j.Range)2 TextEdit (org.eclipse.lsp4j.TextEdit)2 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)1 Expression (com.redhat.qute.parser.template.Expression)1 Parameter (com.redhat.qute.parser.template.Parameter)1 Section (com.redhat.qute.parser.template.Section)1 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)1 WithSection (com.redhat.qute.parser.template.sections.WithSection)1 ExtendedDataModelParameter (com.redhat.qute.project.datamodel.ExtendedDataModelParameter)1 HashSet (java.util.HashSet)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 Hover (org.eclipse.lsp4j.Hover)1 MarkupContent (org.eclipse.lsp4j.MarkupContent)1