Skip to content

Commit

Permalink
Provide #fragment snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Jan 16, 2023
1 parent 1600156 commit 9784c45
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 33 deletions.
2 changes: 1 addition & 1 deletion qute.ls/com.redhat.qute.ls/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
<dev.build.timestamp>${maven.build.timestamp}</dev.build.timestamp>
<lsp4j.version>0.14.0</lsp4j.version>
<qute.version>2.7.0.Final</qute.version>
<qute.version>2.15.3.Final</qute.version>
<junit.version>5.6.1</junit.version>
<jboss.releases.repo.id>jboss-releases-repository</jboss.releases.repo.id>
<jboss.releases.repo.url>https://repository.jboss.org/nexus/service/local/staging/deploy/maven2/</jboss.releases.repo.url>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.redhat.qute.ls.commons.snippets;

public class Link {

private final String url;
private final String label;

public Link(String url, String label) {
this.url = url;
this.label = label;
}

public String getUrl() {
return url;
}

public String getLabel() {
return label;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Snippet {

private ISnippetContext<?> context;

private List<Link> links;

public String getLabel() {
return label;
}
Expand Down Expand Up @@ -95,6 +97,14 @@ public void setSortText(String sortText) {
this.sortText = sortText;
}

public List<Link> getLinks() {
return links;
}

public void setLinks(List<Link> links) {
this.links = links;
}

public ISnippetContext<?> getContext() {
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class SnippetDeserializer implements JsonDeserializer<Snippet> {
private static final String SORTTEXT_ELT = "sortText";
private static final String BODY_ELT = "body";
private static final String CONTEXT_ELT = "context";
private static final String LINK_ELT = "link";
private static final String LINK_URL_ELT = "url";
private static final String LINK_LABEL_ELT = "label";

private final TypeAdapter<? extends ISnippetContext<?>> contextDeserializer;

Expand Down Expand Up @@ -139,7 +142,43 @@ public Snippet deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
}
}

// link
List<Link> links = new ArrayList<>();
JsonElement linkElt = snippetObj.get(LINK_ELT);
if (linkElt != null) {
if (linkElt.isJsonArray()) {
JsonArray urlArray = (JsonArray) linkElt;
urlArray.forEach(elt -> {
Link link = createLink(elt);
if (link != null) {
links.add(link);
}
});
} else if (linkElt.isJsonObject()) {
Link link = createLink(linkElt);
if (link != null) {
links.add(link);
}
}
}
snippet.setLinks(links);

return snippet;
}

private static Link createLink(JsonElement elt) {
if (!elt.isJsonObject()) {
return null;
}
JsonObject linkObj = elt.getAsJsonObject();
JsonElement urlElt = linkObj.get(LINK_URL_ELT);
if (urlElt == null) {
return null;
}
String url = urlElt.getAsString();
JsonElement labelElt = linkObj.get(LINK_LABEL_ELT);
String label = labelElt != null ? labelElt.getAsString() : null;
return new Link(url, label);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,42 @@ private static MarkupContent createDocumentation(Snippet snippet, Map<String, St
doc.append("```");
doc.append(System.lineSeparator());
}
addLinks(snippet.getLinks(), doc, canSupportMarkdown);
return new MarkupContent(canSupportMarkdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT, doc.toString());
}

public static void addLinks(List<Link> links, StringBuilder documentation, boolean canSupportMarkdown) {
if (links != null && !links.isEmpty()) {
documentation.append(System.lineSeparator());
documentation.append(System.lineSeparator());
documentation.append("See ");
for (int i = 0; i < links.size(); i++) {
Link link = links.get(i);
if (i > 0) {
if (i == links.size() - 1) {
documentation.append(" and ");
} else {
documentation.append(", ");
}
}
addLink(link.getUrl(), link.getLabel(), documentation, canSupportMarkdown);
}
documentation.append(" for more informations.");
}
}

public static void addLink(String url, String label, StringBuilder documentation, boolean canSupportMarkdown) {
if (canSupportMarkdown) {
documentation.append("[");
documentation.append(label);
documentation.append("](");
documentation.append(url);
documentation.append(")");
} else {
documentation.append(url);
}
}

private static String getInsertText(Snippet snippet, Map<String, String> model, boolean replace,
String lineDelimiter, String whitespacesIndent) {
StringBuilder text = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package com.redhat.qute.utils;

import static com.redhat.qute.commons.JavaElementInfo.getSimpleType;
import static com.redhat.qute.ls.commons.snippets.SnippetRegistry.addLink;
import static com.redhat.qute.ls.commons.snippets.SnippetRegistry.addLinks;

import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -282,6 +284,8 @@ public static MarkupContent getDocumentation(Snippet snippet, boolean markdown)
documentation.append(snippet.getDescription());
}

addLinks(snippet.getLinks(), documentation, markdown);

return createMarkupContent(documentation, markdown);
}

Expand Down Expand Up @@ -340,14 +344,9 @@ private static void addUrl(String url, StringBuilder documentation, boolean mark
if (!StringUtils.isEmpty(url)) {
documentation.append(System.lineSeparator());
documentation.append("See ");
if (markdown) {
documentation.append("[here](");
documentation.append(url);
documentation.append(")");
} else {
documentation.append(url);
}
addLink(url, "here", documentation, markdown);
documentation.append(" for more informations.");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
{ "#each": {
"prefix": ["each", "{#"],
{
"#each": {
"prefix": [
"each",
"{#"
],
"body": [
"{#each ${1:items}}",
"\t{it.${2:name}}$0",
"{/each}"
],
"description": "Loop section with implicit alias"
"description": "Loop section with implicit alias",
"link": {
"url": "https://quarkus.io/guides/qute-reference#loop_section",
"label": "Loop section"
}
},
"#eval": {
"prefix": "eval",
"body": [
"{#eval ${1:content} /}$0"
],
"description": "Parse and evaluate a template dynamically"
"description": "Parse and evaluate a template dynamically",
"link": {
"url": "https://quarkus.io/guides/qute-reference#eval_section",
"label": "Eval section"
}
},
"#fragment": {
"prefix": "fragment",
"body": [
"{#fragment id=\"${1:item}\"}",
"\t$0",
"{/fragment}"
],
"description": "A fragment represents a part of the template that can be treated as a separate template, i.e. rendered separately.",
"link": [
{
"url": "https://quarkus.io/guides/qute-reference#fragments",
"label": "Frag"
},
{
"url": "https://quarkus.io/guides/qute-reference#type_safe_fragments",
"label": "Eval section"
}
]
},
"#for": {
"prefix": "for",
Expand All @@ -21,7 +52,11 @@
"\t{${1:item}.${3:name}}$0",
"{/for}"
],
"description": "Loop section with alias"
"description": "Loop section with alias",
"link": {
"url": "https://quarkus.io/guides/qute-reference#loop_section",
"label": "Loop section"
}
},
"#if": {
"prefix": "if",
Expand All @@ -30,7 +65,11 @@
"\t$0",
"{/if}"
],
"description": "If section"
"description": "If section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#if_section",
"label": "If section"
}
},
"#else": {
"prefix": "if-else",
Expand All @@ -41,7 +80,11 @@
"\t$0",
"{/if}"
],
"description": "Else section"
"description": "Else section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#if_section",
"label": "Else section"
}
},
"#elseif": {
"prefix": "if-elseif",
Expand All @@ -54,7 +97,11 @@
"\t$0",
"{/if}"
],
"description": "Else If section"
"description": "Else If section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#if_section",
"label": "Else section"
}
},
"#include": {
"prefix": "include",
Expand All @@ -63,7 +110,11 @@
"\t$0",
"{/include}"
],
"description": "Include section"
"description": "Include section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#include_helper",
"label": "Include section"
}
},
"#insert": {
"prefix": "insert",
Expand All @@ -72,7 +123,11 @@
"\t$0",
"{/insert}"
],
"description": "Insert section"
"description": "Insert section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#include_helper",
"label": "Include section"
}
},
"#let": {
"prefix": "let",
Expand All @@ -81,14 +136,22 @@
"\t$0",
"{/let}"
],
"description": "Let section"
"description": "Let section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#let_section",
"label": "Let section"
}
},
"#parameter": {
"prefix": "parameter",
"body": [
"{@${1:class} ${2:alias}}$0"
],
"description": "Insert parameter declaration"
"description": "Insert parameter declaration",
"link": {
"url": "https://quarkus.io/guides/qute-reference#typesafe_expressions",
"label": "Typesafe expressions"
}
},
"#set": {
"prefix": "set",
Expand All @@ -97,7 +160,11 @@
"\t$0",
"{/set}"
],
"description": "Let section"
"description": "Set section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#let_section",
"label": "Set section"
}
},
"#switch": {
"prefix": "switch",
Expand All @@ -106,7 +173,11 @@
"\t{#case ${2:case}}$0",
"{/switch}"
],
"description": "Switch section"
"description": "Switch section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#when_section",
"label": "Let section"
}
},
"#with": {
"prefix": "with",
Expand All @@ -115,7 +186,11 @@
"\t{${2:name}}$0",
"{/with}"
],
"description": "With section"
"description": "With section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#with_section",
"label": "Let section"
}
},
"#when": {
"prefix": "when",
Expand All @@ -124,6 +199,10 @@
"\t{#is ${2:case}}$0",
"{/when}"
],
"description": "When section"
"description": "When section",
"link": {
"url": "https://quarkus.io/guides/qute-reference#when_section",
"label": "When section"
}
}
}
Loading

0 comments on commit 9784c45

Please sign in to comment.