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 ee5ca7b
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 30 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
Expand Up @@ -39,6 +39,8 @@ public class Snippet {

private ISnippetContext<?> context;

private List<String> urls;

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

public List<String> getUrls() {
return urls;
}

public void setUrls(List<String> urls) {
this.urls = urls;
}

public ISnippetContext<?> getContext() {
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 URL_ELT = "url";

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

Expand Down Expand Up @@ -139,6 +140,21 @@ public Snippet deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
}
}

// url
List<String> urls = new ArrayList<>();
JsonElement urlElt = snippetObj.get(URL_ELT);
if (urlElt != null) {
if (urlElt.isJsonArray()) {
JsonArray urlArray = (JsonArray) urlElt;
urlArray.forEach(elt -> {
urls.add(elt.getAsString());
});
} else if (urlElt.isJsonPrimitive()) {
urls.add(urlElt.getAsString());
}
}
snippet.setUrls(urls);

return snippet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ public static MarkupContent getDocumentation(Snippet snippet, boolean markdown)
documentation.append(snippet.getDescription());
}

addUrls(snippet.getUrls(), documentation, markdown);

return createMarkupContent(documentation, markdown);
}

Expand Down Expand Up @@ -340,14 +342,31 @@ 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);
addUrl(url, "here", documentation, markdown);
documentation.append(" for more informations.");
}
}

private static void addUrls(List<String> urls, StringBuilder documentation, boolean markdown) {
if (urls != null && !urls.isEmpty()) {
documentation.append(System.lineSeparator());
documentation.append("See ");
for (String url : urls) {
addUrl(url, "here", documentation, markdown);
}
documentation.append(" for more informations.");
}
}

private static void addUrl(String url, String label, StringBuilder documentation, boolean markdown) {
if (markdown) {
documentation.append("[");
documentation.append(label);
documentation.append("](");
documentation.append(url);
documentation.append(")");
} else {
documentation.append(url);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
{ "#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",
"url": "https://quarkus.io/guides/qute-reference#loop_section"
},
"#eval": {
"prefix": "eval",
"body": [
"{#eval ${1:content} /}$0"
],
"description": "Parse and evaluate a template dynamically"
"description": "Parse and evaluate a template dynamically",
"url": "https://quarkus.io/guides/qute-reference#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.",
"url": [
"https://quarkus.io/guides/qute-reference#fragments",
"https://quarkus.io/guides/qute-reference#type_safe_fragments"
]
},
"#for": {
"prefix": "for",
Expand All @@ -21,7 +40,8 @@
"\t{${1:item}.${3:name}}$0",
"{/for}"
],
"description": "Loop section with alias"
"description": "Loop section with alias",
"url": "https://quarkus.io/guides/qute-reference#loop_section"
},
"#if": {
"prefix": "if",
Expand All @@ -30,7 +50,8 @@
"\t$0",
"{/if}"
],
"description": "If section"
"description": "If section",
"url": "https://quarkus.io/guides/qute-reference#if_section"
},
"#else": {
"prefix": "if-else",
Expand All @@ -41,7 +62,8 @@
"\t$0",
"{/if}"
],
"description": "Else section"
"description": "Else section",
"url": "https://quarkus.io/guides/qute-reference#if_section"
},
"#elseif": {
"prefix": "if-elseif",
Expand All @@ -54,7 +76,8 @@
"\t$0",
"{/if}"
],
"description": "Else If section"
"description": "Else If section",
"url": "https://quarkus.io/guides/qute-reference#if_section"
},
"#include": {
"prefix": "include",
Expand All @@ -63,7 +86,8 @@
"\t$0",
"{/include}"
],
"description": "Include section"
"description": "Include section",
"url": "https://quarkus.io/guides/qute-reference#include_helper"
},
"#insert": {
"prefix": "insert",
Expand All @@ -72,7 +96,8 @@
"\t$0",
"{/insert}"
],
"description": "Insert section"
"description": "Insert section",
"url": "https://quarkus.io/guides/qute-reference#include_helper"
},
"#let": {
"prefix": "let",
Expand All @@ -81,14 +106,16 @@
"\t$0",
"{/let}"
],
"description": "Let section"
"description": "Let section",
"url": "https://quarkus.io/guides/qute-reference#let_section"
},
"#parameter": {
"prefix": "parameter",
"body": [
"{@${1:class} ${2:alias}}$0"
],
"description": "Insert parameter declaration"
"description": "Insert parameter declaration",
"url": "https://quarkus.io/guides/qute-reference#typesafe_expressions"
},
"#set": {
"prefix": "set",
Expand All @@ -97,7 +124,8 @@
"\t$0",
"{/set}"
],
"description": "Let section"
"description": "Let section",
"url": "https://quarkus.io/guides/qute-reference#let_section"
},
"#switch": {
"prefix": "switch",
Expand All @@ -106,7 +134,8 @@
"\t{#case ${2:case}}$0",
"{/switch}"
],
"description": "Switch section"
"description": "Switch section",
"url": "https://quarkus.io/guides/qute-reference#when_section"
},
"#with": {
"prefix": "with",
Expand All @@ -115,7 +144,8 @@
"\t{${2:name}}$0",
"{/with}"
],
"description": "With section"
"description": "With section",
"url": "https://quarkus.io/guides/qute-reference#with_section"
},
"#when": {
"prefix": "when",
Expand All @@ -124,6 +154,7 @@
"\t{#is ${2:case}}$0",
"{/when}"
],
"description": "When section"
"description": "When section",
"url": "https://quarkus.io/guides/qute-reference#when_section"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class QuteAssert {

private static final String FILE_URI = "test.qute";

public static final int SECTION_SNIPPET_SIZE = 14 /* #each, #for */ + 3 /* #user, #formElement */;
public static final int SECTION_SNIPPET_SIZE = 15 /* #each, #for, ... #fragment ... */ + 3 /* #user, #formElement */;

public static String getFileUri(String templateFile) {
return Paths.get(TEMPLATE_BASE_DIR + templateFile).toUri().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public void quoteNoteClosed() throws Exception {
"{foo.getBytes('abcd)}";
testDiagnosticsFor(template, //
d(1, 22, 1, 22, QuteErrorCode.SyntaxError,
"Parser error on line 2: unexpected non-text buffer at the end of the template - unterminated string literal: foo.getBytes('abcd)}",
"Parser error: unexpected non-text buffer at the end of the template - unterminated string literal: foo.getBytes('abcd)}",
DiagnosticSeverity.Error));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void autoClose() throws Exception {

testDiagnosticsFor(template, //
d(2, 6, 2, 6, QuteErrorCode.SyntaxError,
"Parser error on line 3: no section start tag found for {/let}", DiagnosticSeverity.Error), //
"Parser error: no section start tag found for {/let}", DiagnosticSeverity.Error), //
d);
testCodeActionsFor(template, d, //
ca(d, te(0, 0, 0, 0, "{@java.lang.String name}\r\n")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void badNamespace() throws Exception {
Diagnostic d = d(0, 1, 0, 2, QuteErrorCode.UndefinedNamespace, "No namespace resolver found for: `X`.",
DiagnosticSeverity.Warning);
testDiagnosticsFor(template,
d(0, 0, 0, 3, QuteErrorCode.SyntaxError, "Parser error on line 1: empty expression found {X:}",
d(0, 0, 0, 3, QuteErrorCode.SyntaxError, "Parser error on: empty expression found {X:}",
DiagnosticSeverity.Error), //
d);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class QuteDiagnosticsSyntaxErrorTest {
public void emptyParameterDeclaration() {
String template = "{@}";
testDiagnosticsFor(template, //
d(0, 2, 0, 2, QuteErrorCode.SyntaxError, "Parser error on line 1: invalid parameter declaration {@}",
d(0, 2, 0, 2, QuteErrorCode.SyntaxError, "Parser error: invalid parameter declaration {@}",
DiagnosticSeverity.Error));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void templateNotDefined() throws Exception {
testDiagnosticsFor(template, //
// error coming from the real Qute parser
d(0, 11, 0, 11, QuteErrorCode.SyntaxError,
"Parser error on line 1: mandatory section parameters not declared for {#include /}: [template]",
"Parser error: mandatory section parameters not declared for {#include /}: [template]",
DiagnosticSeverity.Error), //
// error coming from Qute LS parser
d(0, 1, 0, 9, QuteErrorCode.TemplateNotDefined, "Template id must be defined as parameter.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public void coreTag() throws Exception {
assertHover(template, "**#for** section tag " + //
System.lineSeparator() + //
System.lineSeparator() + //
"Loop section with alias", //
"Loop section with alias" + //
System.lineSeparator() + //
"See [here](https://quarkus.io/guides/qute-reference#loop_section) for more informations.", //
r(0, 1, 0, 5));
}

Expand Down

0 comments on commit ee5ca7b

Please sign in to comment.