Search in sources :

Example 6 with Section

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

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

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

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

the class QuteDefinition method findDefinitionFromStartEndTagSection.

private static boolean findDefinitionFromStartEndTagSection(int offset, Section section, Template template, List<LocationLink> locations) {
    if (section.isInStartTagName(offset)) {
        int locationsLength = locations.size();
        if (section.getSectionKind() == SectionKind.CUSTOM) {
            QuteProject project = template.getProject();
            if (project != null) {
                String tagName = section.getTag();
                UserTag userTag = project.findUserTag(tagName);
                if (userTag != null) {
                    // 1. Jump to custom user tag file inside src/main/resources/templates/tags
                    String userTagUri = userTag.getUri();
                    Range targetRange = new Range(new Position(0, 0), new Position(0, 0));
                    Range originRange = QutePositionUtility.selectStartTagName(section);
                    locations.add(new LocationLink(userTagUri, targetRange, targetRange, originRange));
                } else {
                    // 2. Jump to custom tag declared in the the {#insert custom-tag of the included
                    // Qute template (by using {#include base).
                    Range originRange = null;
                    Node parent = section.getParent();
                    while (parent != null) {
                        if (parent.getKind() == NodeKind.Section) {
                            Section parentSection = (Section) parent;
                            if (parentSection.getSectionKind() == SectionKind.INCLUDE) {
                                IncludeSection includeSection = (IncludeSection) parentSection;
                                List<QuteIndex> indexes = project.findInsertTagParameter(includeSection.getReferencedTemplateId(), tagName);
                                if (indexes != null) {
                                    for (QuteIndex index : indexes) {
                                        String linkedTemplateUri = index.getTemplatePath().toUri().toString();
                                        Range linkedTargetRange = index.getRange();
                                        if (originRange == null) {
                                            originRange = QutePositionUtility.selectStartTagName(section);
                                        }
                                        locations.add(new LocationLink(linkedTemplateUri, linkedTargetRange, linkedTargetRange, originRange));
                                    }
                                }
                            }
                        }
                        parent = parent.getParent();
                    }
                }
            }
        }
        if (section.hasEndTag() && locationsLength == locations.size()) {
            // 3. Jump to end tag section
            Range originRange = QutePositionUtility.selectStartTagName(section);
            Range targetRange = QutePositionUtility.selectEndTagName(section);
            locations.add(new LocationLink(template.getUri(), targetRange, targetRange, originRange));
        }
        return true;
    } else if (section.isInEndTagName(offset)) {
        if (section.hasStartTag()) {
            // Jump to start tag section
            Range originRange = QutePositionUtility.selectEndTagName(section);
            Range targetRange = QutePositionUtility.selectStartTagName(section);
            locations.add(new LocationLink(template.getUri(), targetRange, targetRange, originRange));
        }
        return true;
    }
    return false;
}
Also used : LocationLink(org.eclipse.lsp4j.LocationLink) Position(org.eclipse.lsp4j.Position) Node(com.redhat.qute.parser.template.Node) QuteIndex(com.redhat.qute.project.indexing.QuteIndex) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) QuteProject(com.redhat.qute.project.QuteProject) UserTag(com.redhat.qute.project.tags.UserTag) Range(org.eclipse.lsp4j.Range) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) Section(com.redhat.qute.parser.template.Section)

Example 10 with Section

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

the class QuteDiagnostics method validateSectionTag.

private static void validateSectionTag(Section section, Template template, ResolvingJavaTypeContext resolvingJavaTypeContext, List<Diagnostic> diagnostics) {
    String tagName = section.getTag();
    if (StringUtils.isEmpty(tagName)) {
        return;
    }
    SectionKind sectionKind = section.getSectionKind();
    if (sectionKind == SectionKind.CUSTOM) {
        if (!resolvingJavaTypeContext.isBinaryUserTagResolved()) {
            // Don't validate custom tag, if the binary user tags are not loaded.
            return;
        }
        QuteProject project = template.getProject();
        if (project != null) {
            // Check if section tag is an user tag
            UserTag userTag = project.findUserTag(tagName);
            if (userTag != null) {
                return;
            }
            // Check if section tag is a parameter from an include section
            Node parent = section.getParent();
            while (parent != null) {
                if (parent.getKind() == NodeKind.Section) {
                    Section parentSection = (Section) parent;
                    if (parentSection.getSectionKind() == SectionKind.INCLUDE) {
                        IncludeSection includeSection = (IncludeSection) parentSection;
                        List<QuteIndex> indexes = project.findInsertTagParameter(includeSection.getReferencedTemplateId(), tagName);
                        if (indexes != null) {
                            return;
                        }
                    }
                }
                parent = parent.getParent();
            }
            Range range = QutePositionUtility.selectStartTagName(section);
            Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Error, QuteErrorCode.UndefinedSectionTag, tagName);
            // Create data information helpful for code action
            diagnostic.setData(DiagnosticDataFactory.createUndefinedSectionTagData(tagName));
            diagnostics.add(diagnostic);
        }
    }
}
Also used : SectionKind(com.redhat.qute.parser.template.SectionKind) Node(com.redhat.qute.parser.template.Node) QuteIndex(com.redhat.qute.project.indexing.QuteIndex) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) DiagnosticDataFactory.createDiagnostic(com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic) Diagnostic(org.eclipse.lsp4j.Diagnostic) QuteProject(com.redhat.qute.project.QuteProject) UserTag(com.redhat.qute.project.tags.UserTag) Range(org.eclipse.lsp4j.Range) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Section(com.redhat.qute.parser.template.Section)

Aggregations

Section (com.redhat.qute.parser.template.Section)18 Node (com.redhat.qute.parser.template.Node)15 Parameter (com.redhat.qute.parser.template.Parameter)11 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)8 Range (org.eclipse.lsp4j.Range)7 Expression (com.redhat.qute.parser.template.Expression)6 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)5 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)4 Part (com.redhat.qute.parser.expression.Part)4 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)4 QuteProject (com.redhat.qute.project.QuteProject)4 Parts (com.redhat.qute.parser.expression.Parts)3 JavaTypeInfoProvider (com.redhat.qute.parser.template.JavaTypeInfoProvider)3 ParameterDeclaration (com.redhat.qute.parser.template.ParameterDeclaration)3 Template (com.redhat.qute.parser.template.Template)3 WithSection (com.redhat.qute.parser.template.sections.WithSection)3 QuteIndex (com.redhat.qute.project.indexing.QuteIndex)3 List (java.util.List)3 JavaMemberInfo (com.redhat.qute.commons.JavaMemberInfo)2 JavaTypeInfo (com.redhat.qute.commons.JavaTypeInfo)2