Search in sources :

Example 26 with Node

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

the class QuteCodeActions method doCodeActionsForUndefinedSectionTag.

private static void doCodeActionsForUndefinedSectionTag(Template template, Diagnostic diagnostic, List<CodeAction> codeActions) {
    QuteProject project = template.getProject();
    if (project == null) {
        return;
    }
    try {
        String tagName = null;
        JsonObject data = (JsonObject) diagnostic.getData();
        if (data != null) {
            tagName = data.get(DIAGNOSTIC_DATA_TAG).getAsString();
        } else {
            int offset = template.offsetAt(diagnostic.getRange().getStart());
            Node node = template.findNodeAt(offset);
            node = QutePositionUtility.findBestNode(offset, node);
            if (node.getKind() == NodeKind.Section) {
                Section section = (Section) node;
                tagName = section.getTag();
            }
        }
        if (tagName == null) {
            return;
        }
        // TODO : use a settings to know the preferred file extension
        String preferedFileExtension = ".html";
        String tagFileUri = project.getTagsDir().resolve(tagName + preferedFileExtension).toUri().toString();
        String title = MessageFormat.format(UNDEFINED_SECTION_TAG_CODEACTION_TITLE, tagName);
        CodeAction createUserTagFile = CodeActionFactory.createFile(title, tagFileUri, "", diagnostic);
        codeActions.add(createUserTagFile);
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "Creation of undefined user tag code action failed", e);
    }
}
Also used : Node(com.redhat.qute.parser.template.Node) CodeAction(org.eclipse.lsp4j.CodeAction) JsonObject(com.google.gson.JsonObject) QuteProject(com.redhat.qute.project.QuteProject) Section(com.redhat.qute.parser.template.Section) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 27 with Node

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

the class Part method getParentSection.

@Override
public Section getParentSection() {
    Parts parts = getParent();
    Expression expression = parts.getParent();
    Node parent = expression.getParent();
    if (parent.getKind() == NodeKind.Parameter) {
        // Its' a parameter which belongs to a section (ex : items):
        // {#for item in items}
        // In this case we must get the parent of the parameter section
        Node ownerSection = parent.getParent();
        parent = ownerSection.getParent();
    }
    // loop for parent node to retrieve the parent section.
    while (parent != null) {
        if (parent.getKind() == NodeKind.Section) {
            return (Section) parent;
        }
        parent = parent.getParent();
    }
    return null;
}
Also used : Expression(com.redhat.qute.parser.template.Expression) 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