Search in sources :

Example 1 with QuteIndex

use of com.redhat.qute.project.indexing.QuteIndex in project quarkus-ls by redhat-developer.

the class QuteProject method findInsertTagParameter.

public List<QuteIndex> findInsertTagParameter(String templateId, String insertParamater) {
    TemplateProvider provider = openedDocuments.get(templateId);
    if (provider != null) {
        Template template = provider.getTemplate().getNow(null);
        if (template != null) {
            List<QuteIndex> indexes = new ArrayList<>();
            collectInsert(insertParamater, template, template, indexes);
            return indexes;
        }
        return Collections.emptyList();
    }
    indexer.scanAsync();
    return indexer.find(templateId, "insert", insertParamater);
}
Also used : QuteIndex(com.redhat.qute.project.indexing.QuteIndex) ArrayList(java.util.ArrayList) Template(com.redhat.qute.parser.template.Template) DataModelTemplate(com.redhat.qute.commons.datamodel.DataModelTemplate)

Example 2 with QuteIndex

use of com.redhat.qute.project.indexing.QuteIndex in project quarkus-ls by redhat-developer.

the class QuteIndexerTest method referencesOfIncludedTag.

@Test
public void referencesOfIncludedTag() {
    long start = System.currentTimeMillis();
    QuteIndexer indexer = new QuteIndexer(createProject());
    indexer.scan();
    long end = System.currentTimeMillis();
    System.err.println((end - start) + "ms");
    // base.qute.html -->
    // <title>{#insert title}Default Title{/}</title>
    // ...
    // {#insert body}No body!{/}
    // 1. reference
    // BookPage/book.qute.html -->
    // {#include base}
    // {#title}A Book{/title}
    // 2. reference
    // BookPage/books.qute.html -->
    // {#include base}
    // {#title}Books{/title}
    List<QuteIndex> indexes = indexer.find(null, "body", null);
    assertNotNull(indexes);
    assertEquals(2, indexes.size());
    assertEquals(// 
    "[QuteIndex [\n" + // 
    "  tag = \"body\"\n" + // 
    "  parameter = null\n" + // 
    "  position = Position [\n" + // 
    "    line = 2\n" + // 
    "    character = 2\n" + // 
    "  ]\n" + // 
    "  kind = CUSTOM\n" + // 
    "  templateId = \"BookPage/books.qute.html\"\n" + // 
    "], QuteIndex [\n" + // 
    "  tag = \"body\"\n" + // 
    "  parameter = null\n" + // 
    "  position = Position [\n" + // 
    "    line = 2\n" + // 
    "    character = 2\n" + // 
    "  ]\n" + // 
    "  kind = CUSTOM\n" + // 
    "  templateId = \"BookPage/book.qute.html\"\n" + "]]", indexes.toString());
}
Also used : QuteIndex(com.redhat.qute.project.indexing.QuteIndex) QuteIndexer(com.redhat.qute.project.indexing.QuteIndexer) Test(org.junit.jupiter.api.Test)

Example 3 with QuteIndex

use of com.redhat.qute.project.indexing.QuteIndex in project quarkus-ls by redhat-developer.

the class QuteIndexerTest method completion.

@Test
public void completion() {
    long start = System.currentTimeMillis();
    QuteIndexer indexer = new QuteIndexer(createProject());
    indexer.scan();
    long end = System.currentTimeMillis();
    System.err.println((end - start) + "ms");
    // base.qute.html -->
    // <title>{#insert title}Default Title{/}</title>
    // ...
    // {#insert body}No body!{/}
    // BookPage/book.qute.html -->
    // {#include base.qute.html}
    // {#|
    List<QuteIndex> indexes = indexer.find("base.qute.html", "insert", null);
    assertNotNull(indexes);
    assertEquals(2, indexes.size());
    assertEquals(// 
    "[QuteIndex [\n" + // 
    "  tag = \"insert\"\n" + // 
    "  parameter = \"title\"\n" + // 
    "  position = Position [\n" + // 
    "    line = 3\n" + // 
    "    character = 18\n" + // 
    "  ]\n" + // 
    "  kind = INSERT\n" + // 
    "  templateId = \"base.qute.html\"\n" + // 
    "], QuteIndex [\n" + // 
    "  tag = \"insert\"\n" + // 
    "  parameter = \"body\"\n" + // 
    "  position = Position [\n" + // 
    "    line = 8\n" + // 
    "    character = 9\n" + // 
    "  ]\n" + // 
    "  kind = INSERT\n" + // 
    "  templateId = \"base.qute.html\"\n" + "]]", indexes.toString());
}
Also used : QuteIndex(com.redhat.qute.project.indexing.QuteIndex) QuteIndexer(com.redhat.qute.project.indexing.QuteIndexer) Test(org.junit.jupiter.api.Test)

Example 4 with QuteIndex

use of com.redhat.qute.project.indexing.QuteIndex 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 QuteIndex

use of com.redhat.qute.project.indexing.QuteIndex 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

QuteIndex (com.redhat.qute.project.indexing.QuteIndex)8 QuteIndexer (com.redhat.qute.project.indexing.QuteIndexer)4 Test (org.junit.jupiter.api.Test)4 Node (com.redhat.qute.parser.template.Node)3 Section (com.redhat.qute.parser.template.Section)3 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)2 QuteProject (com.redhat.qute.project.QuteProject)2 UserTag (com.redhat.qute.project.tags.UserTag)2 Position (org.eclipse.lsp4j.Position)2 Range (org.eclipse.lsp4j.Range)2 DataModelParameter (com.redhat.qute.commons.datamodel.DataModelParameter)1 DataModelTemplate (com.redhat.qute.commons.datamodel.DataModelTemplate)1 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)1 Parameter (com.redhat.qute.parser.template.Parameter)1 SectionKind (com.redhat.qute.parser.template.SectionKind)1 Template (com.redhat.qute.parser.template.Template)1 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)1 QuteTemplateIndex (com.redhat.qute.project.indexing.QuteTemplateIndex)1 DiagnosticDataFactory.createDiagnostic (com.redhat.qute.services.diagnostics.DiagnosticDataFactory.createDiagnostic)1 FileUtils.createPath (com.redhat.qute.utils.FileUtils.createPath)1