feat(ts): add mustache parser with cloudinit filetype alias

This commit is contained in:
2026-05-12 06:51:52 +02:00
parent ef21d5a7ff
commit 8a88455f87
11 changed files with 131 additions and 10 deletions
+27
View File
@@ -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.
+6
View File
@@ -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 = {
+1
View File
@@ -1,5 +1,6 @@
vim.filetype.add({
pattern = {
[".*%.xml%.tmpl"] = "gotmpl",
[".*%.cloud%-init%.yml"] = "cloudinit",
},
})
+54 -10
View File
@@ -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
+4
View File
@@ -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"
+1
View File
@@ -0,0 +1 @@
; inherits: mustache
+1
View File
@@ -0,0 +1 @@
; inherits: mustache
+5
View File
@@ -0,0 +1,5 @@
; inherits: mustache
((text) @injection.content
(#set! injection.language "yaml")
(#set! injection.combined))
+4
View File
@@ -0,0 +1,4 @@
[
(inverted_section)
(section)
] @fold
+26
View File
@@ -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
+2
View File
@@ -0,0 +1,2 @@
((comment_statement) @injection.content
(#set! injection.language "comment"))