Search in sources :

Example 1 with IncludeSection

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

the class QuteDiagnostics method validateDataModel.

private void validateDataModel(Node parent, Template template, QuteValidationSettings validationSettings, ResolvingJavaTypeContext resolvingJavaTypeContext, ResolutionContext currentContext, List<Diagnostic> diagnostics) {
    ResolutionContext previousContext = currentContext;
    List<Node> children = parent.getChildren();
    for (Node node : children) {
        switch(node.getKind()) {
            case ParameterDeclaration:
                {
                    ParameterDeclaration parameter = (ParameterDeclaration) node;
                    String javaTypeToResolve = parameter.getJavaType();
                    if (!StringUtils.isEmpty(javaTypeToResolve)) {
                        String projectUri = template.getProjectUri();
                        if (projectUri != null) {
                            List<JavaTypeRangeOffset> classNameRanges = parameter.getJavaTypeNameRanges();
                            for (RangeOffset classNameRange : classNameRanges) {
                                String className = template.getText(classNameRange);
                                ResolvedJavaTypeInfo resolvedJavaType = resolveJavaType(className, projectUri, resolvingJavaTypeContext);
                                if (resolvedJavaType == null) {
                                    // Java type doesn't exist
                                    Range range = QutePositionUtility.createRange(classNameRange, template);
                                    Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Error, QuteErrorCode.UnknownType, className);
                                    diagnostics.add(diagnostic);
                                } else if (!isResolvingJavaType(resolvedJavaType)) {
                                    currentContext.put(javaTypeToResolve, resolvedJavaType);
                                }
                            }
                        }
                    }
                    break;
                }
            case Section:
                {
                    Section section = (Section) node;
                    if (canChangeContext(section)) {
                        currentContext = new ResolutionContext(currentContext);
                    }
                    List<Parameter> parameters = section.getParameters();
                    // validate expression parameters
                    for (Parameter parameter : parameters) {
                        Expression expression = parameter.getJavaTypeExpression();
                        if (expression != null) {
                            ResolvedJavaTypeInfo result = validateExpression(expression, section, template, validationSettings, previousContext, resolvingJavaTypeContext, diagnostics);
                            switch(section.getSectionKind()) {
                                case FOR:
                                case EACH:
                                    String alias = ((LoopSection) section).getAlias();
                                    currentContext.put(alias, result);
                                    break;
                                case WITH:
                                    currentContext.setWithObject(result);
                                    break;
                                case LET:
                                case SET:
                                    currentContext.put(parameter.getName(), result);
                                    break;
                                default:
                            }
                        }
                    }
                    switch(section.getSectionKind()) {
                        case INCLUDE:
                            validateIncludeSection((IncludeSection) section, diagnostics);
                            break;
                        default:
                            validateSectionTag(section, template, resolvingJavaTypeContext, diagnostics);
                    }
                    break;
                }
            case Expression:
                {
                    validateExpression((Expression) node, null, template, validationSettings, previousContext, resolvingJavaTypeContext, diagnostics);
                    break;
                }
            default:
        }
        validateDataModel(node, template, validationSettings, resolvingJavaTypeContext, currentContext, diagnostics);
    }
}
Also used : Node(com.redhat.qute.parser.template.Node) 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) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Section(com.redhat.qute.parser.template.Section) Expression(com.redhat.qute.parser.template.Expression) JavaTypeRangeOffset(com.redhat.qute.parser.template.ParameterDeclaration.JavaTypeRangeOffset) RangeOffset(com.redhat.qute.parser.template.RangeOffset) Parameter(com.redhat.qute.parser.template.Parameter) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) List(java.util.List) ArrayList(java.util.ArrayList) ParameterDeclaration(com.redhat.qute.parser.template.ParameterDeclaration)

Example 2 with IncludeSection

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

the class QuteDocumentLink method findDocumentLinks.

private void findDocumentLinks(Node node, Template template, List<DocumentLink> links) {
    List<Node> children = node.getChildren();
    for (Node child : children) {
        if (child.getKind() == NodeKind.Section) {
            Section section = (Section) child;
            if (section.getSectionKind() == SectionKind.INCLUDE) {
                // #include section case:
                IncludeSection includeSection = (IncludeSection) section;
                // {#include base.qute.html}
                // In this case 'base.qute.html' is a document link
                Parameter includedTemplateId = includeSection.getParameterAtIndex(0);
                if (includedTemplateId != null) {
                    Range range = QutePositionUtility.createRange(includedTemplateId.getStart(), includedTemplateId.getEnd(), template);
                    if (range != null) {
                        Path templateFile = includeSection.getReferencedTemplateFile();
                        if (templateFile != null) {
                            String target = templateFile.toUri().toString();
                            links.add(new DocumentLink(range, target != null ? target : ""));
                        }
                    }
                }
            }
        }
        findDocumentLinks(child, template, links);
    }
}
Also used : Path(java.nio.file.Path) Node(com.redhat.qute.parser.template.Node) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) Parameter(com.redhat.qute.parser.template.Parameter) Range(org.eclipse.lsp4j.Range) IncludeSection(com.redhat.qute.parser.template.sections.IncludeSection) Section(com.redhat.qute.parser.template.Section) DocumentLink(org.eclipse.lsp4j.DocumentLink)

Example 3 with IncludeSection

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

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

Node (com.redhat.qute.parser.template.Node)4 Section (com.redhat.qute.parser.template.Section)4 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)4 Range (org.eclipse.lsp4j.Range)4 Parameter (com.redhat.qute.parser.template.Parameter)2 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)2 QuteProject (com.redhat.qute.project.QuteProject)2 QuteIndex (com.redhat.qute.project.indexing.QuteIndex)2 UserTag (com.redhat.qute.project.tags.UserTag)2 DiagnosticDataFactory.createDiagnostic (com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic)2 Diagnostic (org.eclipse.lsp4j.Diagnostic)2 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)1 Expression (com.redhat.qute.parser.template.Expression)1 ParameterDeclaration (com.redhat.qute.parser.template.ParameterDeclaration)1 JavaTypeRangeOffset (com.redhat.qute.parser.template.ParameterDeclaration.JavaTypeRangeOffset)1 RangeOffset (com.redhat.qute.parser.template.RangeOffset)1 SectionKind (com.redhat.qute.parser.template.SectionKind)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1