diff --git a/NOTICE b/NOTICE index 437e478..f7f5ab2 100644 --- a/NOTICE +++ b/NOTICE @@ -92,3 +92,30 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +-------------------------------------------------------------------------------- + +This software includes tree-sitter queries derived from tree-sitter-mustache +(https://github.com/TheLeoP/tree-sitter-mustache) + +The MIT License (MIT) + +Copyright (c) 2024-2025 TheLeoP + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/init.lua b/init.lua index 1912fb4..82d2c6d 100644 --- a/init.lua +++ b/init.lua @@ -63,6 +63,12 @@ require("ts").setup({ "https://github.com/tree-sitter/tree-sitter-html", "https://github.com/tree-sitter/tree-sitter-json", "https://github.com/tree-sitter-grammars/tree-sitter-luadoc", + { + src = "https://github.com/TheLeoP/tree-sitter-mustache", + aliases = { + { lang = "cloudinit", filetypes = { "cloudinit" } }, + }, + }, { src = "https://github.com/tree-sitter/tree-sitter-php", parsers = { diff --git a/lua/core/filetypes.lua b/lua/core/filetypes.lua index e6a5bbb..3fe84cd 100644 --- a/lua/core/filetypes.lua +++ b/lua/core/filetypes.lua @@ -1,5 +1,6 @@ vim.filetype.add({ pattern = { [".*%.xml%.tmpl"] = "gotmpl", + [".*%.cloud%-init%.yml"] = "cloudinit", }, }) diff --git a/lua/ts.lua b/lua/ts.lua index 37d73fd..5d59826 100644 --- a/lua/ts.lua +++ b/lua/ts.lua @@ -2,12 +2,17 @@ local log = require("log") local M = {} +---@class ow.TS.Alias +---@field lang string +---@field filetypes? string[] + ---@class ow.TS.Parser ---@field lang string ---@field location? string ---@field generate? boolean ---@field from_json? boolean ---@field filetypes? string[] +---@field aliases? ow.TS.Alias[] ---@class ow.TS.RepoBase ---@field src string @@ -24,6 +29,7 @@ local M = {} ---@field generate? boolean ---@field from_json? boolean ---@field filetypes? string[] +---@field aliases? ow.TS.Alias[] ---@class ow.TS.MultiSpec : ow.TS.RepoBase ---@field parsers ow.TS.Parser[] @@ -52,28 +58,62 @@ local function start_treesitter(buf) end end ----@param parser ow.TS.Parser +---@param lang string ---@param so string -local function register(parser, so) - vim.treesitter.language.add(parser.lang, { path = so }) - local filetypes = { parser.lang } - if parser.filetypes then - vim.treesitter.language.register(parser.lang, parser.filetypes) - vim.list_extend(filetypes, parser.filetypes) +---@param filetypes? string[] +---@param symbol_name? string +local function register_lang(lang, so, filetypes, symbol_name) + vim.treesitter.language.add( + lang, + { path = so, symbol_name = symbol_name } + ) + local fts = { lang } + if filetypes then + vim.treesitter.language.register(lang, filetypes) + vim.list_extend(fts, filetypes) end vim.api.nvim_create_autocmd("FileType", { group = vim.api.nvim_create_augroup( - "ow.ts.parser." .. parser.lang, + "ow.ts.parser." .. lang, { clear = true } ), - pattern = filetypes, + pattern = fts, callback = function(ev) start_treesitter(ev.buf) end, }) end +---@class ow.TS.LangEntry +---@field lang string +---@field filetypes? string[] +---@field symbol_name? string + +---@param parser ow.TS.Parser +---@return ow.TS.LangEntry[] +local function parser_langs(parser) + local entries = { + { lang = parser.lang, filetypes = parser.filetypes }, + } + for _, alias in ipairs(parser.aliases or {}) do + table.insert(entries, { + lang = alias.lang, + filetypes = alias.filetypes, + symbol_name = parser.lang, + }) + end + return entries +end + +---@param parser ow.TS.Parser +---@param so string +local function register(parser, so) + for _, e in ipairs(parser_langs(parser)) do + register_lang(e.lang, so, e.filetypes, e.symbol_name) + end +end + ---@param lang string local function activate_open_buffers(lang) local fts = vim.treesitter.language.get_filetypes(lang) @@ -110,7 +150,9 @@ local function build(repo, parser) return end register(parser, out) - activate_open_buffers(parser.lang) + for _, e in ipairs(parser_langs(parser)) do + activate_open_buffers(e.lang) + end end local function do_build() @@ -206,6 +248,7 @@ local function spec_to_repo(spec) generate = pick(s.generate, spec.generate), from_json = pick(s.from_json, spec.from_json), filetypes = s.filetypes, + aliases = s.aliases, }) end else @@ -225,6 +268,7 @@ local function spec_to_repo(spec) generate = spec.generate, from_json = spec.from_json, filetypes = spec.filetypes, + aliases = spec.aliases, }) end diff --git a/nvim-pack-lock.json b/nvim-pack-lock.json index 36cf44c..9914827 100644 --- a/nvim-pack-lock.json +++ b/nvim-pack-lock.json @@ -101,6 +101,10 @@ "rev": "873612aadd3f684dd4e631bdf42ea8990c57634e", "src": "https://github.com/tree-sitter-grammars/tree-sitter-luadoc" }, + "tree-sitter-mustache": { + "rev": "0f1f3cf07508a64b84cbff457f1446a787c48a0e", + "src": "https://github.com/TheLeoP/tree-sitter-mustache" + }, "tree-sitter-php": { "rev": "3f2465c217d0a966d41e584b42d75522f2a3149e", "src": "https://github.com/tree-sitter/tree-sitter-php" diff --git a/queries/cloudinit/folds.scm b/queries/cloudinit/folds.scm new file mode 100644 index 0000000..595b957 --- /dev/null +++ b/queries/cloudinit/folds.scm @@ -0,0 +1 @@ +; inherits: mustache diff --git a/queries/cloudinit/highlights.scm b/queries/cloudinit/highlights.scm new file mode 100644 index 0000000..595b957 --- /dev/null +++ b/queries/cloudinit/highlights.scm @@ -0,0 +1 @@ +; inherits: mustache diff --git a/queries/cloudinit/injections.scm b/queries/cloudinit/injections.scm new file mode 100644 index 0000000..90e0e30 --- /dev/null +++ b/queries/cloudinit/injections.scm @@ -0,0 +1,5 @@ +; inherits: mustache + +((text) @injection.content + (#set! injection.language "yaml") + (#set! injection.combined)) diff --git a/queries/mustache/folds.scm b/queries/mustache/folds.scm new file mode 100644 index 0000000..630900d --- /dev/null +++ b/queries/mustache/folds.scm @@ -0,0 +1,4 @@ +[ + (inverted_section) + (section) +] @fold diff --git a/queries/mustache/highlights.scm b/queries/mustache/highlights.scm new file mode 100644 index 0000000..1a5c6a4 --- /dev/null +++ b/queries/mustache/highlights.scm @@ -0,0 +1,26 @@ +; Forked from https://github.com/TheLeoP/tree-sitter-mustache +; Copyright (c) 2024-2025 TheLeoP +; Licensed under the MIT license. +; Modifications Copyright 2026 Oscar Wallberg, Apache-2.0 + +[ + (start_delimiter) + (end_delimiter) + "{" + "}" +] @punctuation.bracket + +(identifier) @variable + +(partial_content) @module + +"." @punctuation.delimiter + +[ + "#" + "/" + "^" + ">" +] @punctuation.special + +(comment_statement) @comment @spell diff --git a/queries/mustache/injections.scm b/queries/mustache/injections.scm new file mode 100644 index 0000000..d0400c3 --- /dev/null +++ b/queries/mustache/injections.scm @@ -0,0 +1,2 @@ +((comment_statement) @injection.content + (#set! injection.language "comment"))