Search in sources :

Example 1 with UserTag

use of com.redhat.qute.project.tags.UserTag in project quarkus-ls by redhat-developer.

the class QuteProject method findUserTag.

/**
 * Find user tag by the given tagName and null otherwise.
 *
 * @param tagName the tag name.
 *
 * @return user tag by the given tagName and null otherwise.
 */
public UserTag findUserTag(String tagName) {
    // Source tags
    Collection<UserTag> tags = getSourceUserTags();
    for (UserTag userTag : tags) {
        if (tagName.equals(userTag.getName())) {
            return userTag;
        }
    }
    // Binary tags
    tags = getBinaryUserTags().getNow(Collections.emptyList());
    for (UserTag userTag : tags) {
        if (tagName.equals(userTag.getName())) {
            return userTag;
        }
    }
    return null;
}
Also used : UserTag(com.redhat.qute.project.tags.UserTag)

Example 2 with UserTag

use of com.redhat.qute.project.tags.UserTag in project quarkus-ls by redhat-developer.

the class QuteHover method doHoverForSection.

private CompletableFuture<Hover> doHoverForSection(Section section, Template template, HoverRequest hoverRequest, CancelChecker cancelChecker) {
    if (section.hasTag()) {
        // the section defines a tag (e : {#for
        String tagName = section.getTag();
        if (section.getSectionKind() == SectionKind.CUSTOM) {
            // custom tag: search information about user tag
            QuteProject project = template.getProject();
            if (project != null) {
                UserTag userTag = project.findUserTag(tagName);
                if (userTag != null) {
                    Range range = createSectionTagRange(section, hoverRequest);
                    if (range != null) {
                        boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
                        MarkupContent content = DocumentationUtils.getDocumentation(userTag, hasMarkdown);
                        Hover hover = new Hover(content, range);
                        return CompletableFuture.completedFuture(hover);
                    }
                }
            }
        } else {
            // core tag like #for, #if, etc: display document hover for the section
            Optional<Snippet> snippetSection = // 
            snippetRegistryProvider.getSnippetRegistry().getSnippets().stream().filter(// 
            snippet -> tagName.equals(snippet.getLabel())).findFirst();
            if (snippetSection.isPresent()) {
                Snippet snippet = snippetSection.get();
                Range range = createSectionTagRange(section, hoverRequest);
                if (range != null) {
                    boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
                    MarkupContent content = DocumentationUtils.getDocumentation(snippet, hasMarkdown);
                    Hover hover = new Hover(content, range);
                    return CompletableFuture.completedFuture(hover);
                }
            }
        }
    }
    return NO_HOVER;
}
Also used : Template(com.redhat.qute.parser.template.Template) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) MarkupKind(org.eclipse.lsp4j.MarkupKind) UserTagUtils(com.redhat.qute.utils.UserTagUtils) SectionMetadata(com.redhat.qute.parser.template.SectionMetadata) QutePositionUtility(com.redhat.qute.utils.QutePositionUtility) CompletableFuture(java.util.concurrent.CompletableFuture) Range(org.eclipse.lsp4j.Range) QuteProject(com.redhat.qute.project.QuteProject) Hover(org.eclipse.lsp4j.Hover) SnippetRegistryProvider(com.redhat.qute.ls.commons.snippets.SnippetRegistryProvider) Level(java.util.logging.Level) Parameter(com.redhat.qute.parser.template.Parameter) JavaDataModelCache(com.redhat.qute.project.datamodel.JavaDataModelCache) UserTag(com.redhat.qute.project.tags.UserTag) SectionKind(com.redhat.qute.parser.template.SectionKind) Expression(com.redhat.qute.parser.template.Expression) Section(com.redhat.qute.parser.template.Section) Parts(com.redhat.qute.parser.expression.Parts) Position(org.eclipse.lsp4j.Position) JavaMemberInfo(com.redhat.qute.commons.JavaMemberInfo) Snippet(com.redhat.qute.ls.commons.snippets.Snippet) DocumentationUtils(com.redhat.qute.utils.DocumentationUtils) MarkupContent(org.eclipse.lsp4j.MarkupContent) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Node(com.redhat.qute.parser.template.Node) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) Logger(java.util.logging.Logger) CancelChecker(org.eclipse.lsp4j.jsonrpc.CancelChecker) HoverRequest(com.redhat.qute.services.hover.HoverRequest) BadLocationException(com.redhat.qute.ls.commons.BadLocationException) Part(com.redhat.qute.parser.expression.Part) ParameterDeclaration(com.redhat.qute.parser.template.ParameterDeclaration) SharedSettings(com.redhat.qute.settings.SharedSettings) RangeOffset(com.redhat.qute.parser.template.RangeOffset) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) Optional(java.util.Optional) Hover(org.eclipse.lsp4j.Hover) QuteProject(com.redhat.qute.project.QuteProject) Snippet(com.redhat.qute.ls.commons.snippets.Snippet) UserTag(com.redhat.qute.project.tags.UserTag) Range(org.eclipse.lsp4j.Range) MarkupContent(org.eclipse.lsp4j.MarkupContent)

Example 3 with UserTag

use of com.redhat.qute.project.tags.UserTag in project quarkus-ls by redhat-developer.

the class QuteDiagnosticsForSyntax method addUserTag.

private static void addUserTag(Collection<UserTag> tags, EngineBuilder engineBuilder) {
    for (UserTag userTag : tags) {
        String tagName = userTag.getName();
        String tagTemplateId = userTag.getTemplateId();
        engineBuilder.addSectionHelper(new UserTagSectionHelper.Factory(tagName, tagTemplateId));
    }
}
Also used : UserTagSectionHelper(io.quarkus.qute.UserTagSectionHelper) UserTag(com.redhat.qute.project.tags.UserTag)

Example 4 with UserTag

use of com.redhat.qute.project.tags.UserTag 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 5 with UserTag

use of com.redhat.qute.project.tags.UserTag 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

UserTag (com.redhat.qute.project.tags.UserTag)6 QuteProject (com.redhat.qute.project.QuteProject)4 Range (org.eclipse.lsp4j.Range)4 Node (com.redhat.qute.parser.template.Node)3 Section (com.redhat.qute.parser.template.Section)3 SectionKind (com.redhat.qute.parser.template.SectionKind)2 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)2 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)2 QuteIndex (com.redhat.qute.project.indexing.QuteIndex)2 DiagnosticDataFactory.createDiagnostic (com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic)2 Diagnostic (org.eclipse.lsp4j.Diagnostic)2 Position (org.eclipse.lsp4j.Position)2 JavaMemberInfo (com.redhat.qute.commons.JavaMemberInfo)1 JavaTypeInfo (com.redhat.qute.commons.JavaTypeInfo)1 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)1 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)1 Snippet (com.redhat.qute.ls.commons.snippets.Snippet)1 SnippetRegistryProvider (com.redhat.qute.ls.commons.snippets.SnippetRegistryProvider)1 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)1 Part (com.redhat.qute.parser.expression.Part)1