Search in sources :

Example 11 with Node

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

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

the class QuteDefinition method findDefinition.

public CompletableFuture<List<? extends LocationLink>> findDefinition(Template template, Position position, CancelChecker cancelChecker) {
    try {
        DefinitionRequest definitionRequest = new DefinitionRequest(template, position);
        Node node = definitionRequest.getNode();
        if (node == null) {
            return NO_DEFINITION;
        }
        int offset = definitionRequest.getOffset();
        switch(node.getKind()) {
            case Section:
                // - Java data model definition
                return findDefinitionFromSection(offset, (Section) node, template, cancelChecker);
            case ParameterDeclaration:
                // Return Java class definition
                return findDefinitionFromParameterDeclaration(offset, (ParameterDeclaration) node, template);
            case Expression:
                return findDefinitionFromExpression(offset, (Expression) node, template, cancelChecker);
            case ExpressionPart:
                Part part = (Part) node;
                return findDefinitionFromPart(part, template, cancelChecker);
            default:
                // no definitions
                return NO_DEFINITION;
        }
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "Failed creating DefinitionRequest", e);
        return NO_DEFINITION;
    }
}
Also used : DefinitionRequest(com.redhat.qute.services.definition.DefinitionRequest) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) MethodPart(com.redhat.qute.parser.expression.MethodPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) PropertyPart(com.redhat.qute.parser.expression.PropertyPart) Node(com.redhat.qute.parser.template.Node) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 13 with Node

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

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

Example 15 with Node

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

the class QuteHighlighting method higlightSection.

private static void higlightSection(Section section, int offset, Position position, List<DocumentHighlight> highlights, CancelChecker cancelChecker) throws BadLocationException {
    if (section.isInStartTagName(offset) || section.isInEndTagName(offset)) {
        highlightSectionTag(section, highlights);
        if (!section.getBlockLabels().isEmpty()) {
            // The section can have nested block (ex #for can have #else)
            for (Node node : section.getChildren()) {
                if (node.getKind() == NodeKind.Section && section.getBlockLabels().contains(((Section) node).getSectionKind())) {
                    Section nestedBlock = (Section) node;
                    highlightSectionTag(nestedBlock, highlights);
                }
            }
        } else {
            // Get parent section
            Section parentSection = section.getParentSection();
            if (parentSection != null && parentSection.getBlockLabels().contains(section.getSectionKind())) {
                highlightSectionTag(parentSection, highlights);
            }
        }
    } else {
        highlightReferenceObjectPart(section, offset, highlights, cancelChecker);
    }
}
Also used : Node(com.redhat.qute.parser.template.Node) Section(com.redhat.qute.parser.template.Section)

Aggregations

Node (com.redhat.qute.parser.template.Node)27 Section (com.redhat.qute.parser.template.Section)14 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)10 Parameter (com.redhat.qute.parser.template.Parameter)10 Range (org.eclipse.lsp4j.Range)10 Expression (com.redhat.qute.parser.template.Expression)8 ArrayList (java.util.ArrayList)7 Part (com.redhat.qute.parser.expression.Part)6 ObjectPart (com.redhat.qute.parser.expression.ObjectPart)5 Template (com.redhat.qute.parser.template.Template)5 Parts (com.redhat.qute.parser.expression.Parts)4 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)4 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)4 List (java.util.List)4 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)3 MethodPart (com.redhat.qute.parser.expression.MethodPart)3 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)3 QuteProject (com.redhat.qute.project.QuteProject)3 QuteIndex (com.redhat.qute.project.indexing.QuteIndex)3 JsonObject (com.google.gson.JsonObject)2