Search in sources :

Example 1 with WithSection

use of com.redhat.qute.parser.template.sections.WithSection 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)

Aggregations

ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)1 Parameter (com.redhat.qute.parser.template.Parameter)1 Section (com.redhat.qute.parser.template.Section)1 SectionMetadata (com.redhat.qute.parser.template.SectionMetadata)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 CompletionItem (org.eclipse.lsp4j.CompletionItem)1 TextEdit (org.eclipse.lsp4j.TextEdit)1