From 2a47b9c5af0fd9ae8bba8198fd66abc6d07fdf10 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Thu, 14 Sep 2023 19:43:43 -0500 Subject: [PATCH] initial commit Signed-off-by: jolheiser --- .helix/languages.toml | 16 ++++++ README.md | 8 +++ go.mod | 5 ++ go.sum | 2 + queries/templ/highlights.scm | 97 ++++++++++++++++++++++++++++++++++++ queries/templ/indents.scm | 9 ++++ queries/templ/injections.scm | 5 ++ queries/templ/structure.scm | 10 ++++ test.templ | 24 +++++++++ 9 files changed, 176 insertions(+) create mode 100644 .helix/languages.toml create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 queries/templ/highlights.scm create mode 100644 queries/templ/indents.scm create mode 100644 queries/templ/injections.scm create mode 100644 queries/templ/structure.scm create mode 100644 test.templ diff --git a/.helix/languages.toml b/.helix/languages.toml new file mode 100644 index 0000000..eb0521e --- /dev/null +++ b/.helix/languages.toml @@ -0,0 +1,16 @@ +use-grammars = { only = ["templ"] } + +[[language]] +name = "templ" +auto-format = true +scope = "source.templ" +injection-regex = "templ" +file-types = ["templ"] +roots = ["go.mod"] +comment-token = "//" +language-server = { command = "templ", args = ["lsp"] } +formatter = { command = "templ", args = ["fmt"] } + +[[grammar]] +name = "templ" +source = { git = "https://github.com/vrischmann/tree-sitter-templ", rev = "9f63037ad08a58050d0582ef1ae0009bd0fbf2f1" } diff --git a/README.md b/README.md new file mode 100644 index 0000000..3af0a20 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# templ helix integration + +1. Add [.helix/languages.toml](.helix/languages.toml) to your Helix languages config. +2. Grab the [queries](queries/templ) and place them in `~/.config/helix/runtime/queries/templ` + +Note: queries were obtained from [tree-sitter-templ](https://github.com/vrischmann/tree-sitter-templ), which likely will be up-to-date compared to this repo. +The only extra query is [indents](queries/indents.scm). + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d98da12 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.jojodev.com/jolheiser/templ-helix + +go 1.20 + +require github.com/a-h/templ v0.2.334 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d0c9a20 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/a-h/templ v0.2.334 h1:/mKupkgHGeSSeC0KiGRvmUoRGQJuku9VGVhRP1CeWgY= +github.com/a-h/templ v0.2.334/go.mod h1:6Lfhsl3Z4/vXl7jjEjkJRCqoWDGjDnuKgzjYMDSddas= diff --git a/queries/templ/highlights.scm b/queries/templ/highlights.scm new file mode 100644 index 0000000..99f7716 --- /dev/null +++ b/queries/templ/highlights.scm @@ -0,0 +1,97 @@ +; TODO(vincent): can we use injection ? I can't get it working + +; A bunch of these are taken from: +; * https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/go/highlights.scm +; * https://github.com/tree-sitter/tree-sitter-go/blob/master/queries/highlights.scm + +(package_identifier) @namespace + +(parameter_declaration (identifier) @parameter) +(variadic_parameter_declaration (identifier) @parameter) + +(function_declaration + name: (identifier) @function) + +(type_spec name: (type_identifier) @type.definition) +(type_identifier) @type +(field_identifier) @property +(identifier) @variable + +; Function calls + +(call_expression + function: (identifier) @function.call) + +(call_expression + function: (selector_expression + field: (field_identifier) @method.call)) + +; +; These are Templ specific +; + +(component_declaration + name: (component_identifier) @function) + +(tag_start) @tag +(tag_end) @tag +(self_closing_tag) @tag +(style_element) @tag + +(attribute + name: (attribute_name) @tag.attribute) +(attribute + value: (quoted_attribute_value) @string) + +(element_text) @string.special +(style_element_text) @string.special + +(css_property + name: (css_property_name) @attribute) + +(expression) @function.method +(dynamic_class_attribute_value) @function.method + +(component_import + name: (component_identifier) @function) + +(component_render) @function.call + +[ + "@" +] @operator + +[ + "func" + "var" + "const" + "templ" + "css" + "type" + "return" + "struct" + "range" + "script" +] @keyword + +[ + "import" + "package" +] @include + +[ + "else" + "case" + "switch" + "if" + "default" +] @conditional + +"for" @repeat + +[ + (interpreted_string_literal) + (raw_string_literal) + (rune_literal) +] @string + diff --git a/queries/templ/indents.scm b/queries/templ/indents.scm new file mode 100644 index 0000000..dfd2231 --- /dev/null +++ b/queries/templ/indents.scm @@ -0,0 +1,9 @@ +[ + (component_declaration) +] @indent + +[ + "]" + ")" +] @outdent + diff --git a/queries/templ/injections.scm b/queries/templ/injections.scm new file mode 100644 index 0000000..d775f7c --- /dev/null +++ b/queries/templ/injections.scm @@ -0,0 +1,5 @@ +((script_block_text) @injection.content (#set! injection.language "javascript")) +((script_element_text) @injection.content (#set! injection.language "javascript")) + +((style_element_text) @injection.content (#set! injection.language "css")) + diff --git a/queries/templ/structure.scm b/queries/templ/structure.scm new file mode 100644 index 0000000..fa06e7e --- /dev/null +++ b/queries/templ/structure.scm @@ -0,0 +1,10 @@ +(component_declaration + "templ" @structure.anchor + (parameter_list + "(" @structure.open + ")" @structure.close + ) +) + + + diff --git a/test.templ b/test.templ new file mode 100644 index 0000000..54bcdd5 --- /dev/null +++ b/test.templ @@ -0,0 +1,24 @@ +package main + +import "strconv" + +var red = "#ff0000"; + +css className() { + background-color: #ffffff; + color: { red }; +} + +script graph(data []any) { + const chart = LightweightCharts.createChart(document.body, { width: 400, height: 300 }); + const lineSeries = chart.addLineSeries(); + lineSeries.setData(data); +} + +templ hello(name string, vars []string) { +
Hello, { name }
+ for i, path := range vars { + lol { strconv.Itoa(i) }/ + } +} +