如何为不同后缀的Go模板文件配置不同的Tree-sitter注入?
实现按文件名后缀匹配Treesitter注入规则
可以实现仅针对特定后缀的模板文件应用对应语法注入,只需在现有注入规则中添加文件名匹配条件即可:
修改后的HTML注入规则(仅对.tmpl.html生效)
((text) @injection.content #match? @filename "\.tmpl\.html$" (#set! injection.language "html") (#set! injection.combined) (#set! injection.include-children))
修改后的TypeScript注入规则(仅对.tmpl.ts生效)
((text) @injection.content #match? @filename "\.tmpl\.ts$" (#set! injection.language "typescript") (#set! injection.combined) (#set! injection.include-children))
说明
#match? @filename "\.tmpl\.html$"是核心条件:通过正则匹配当前文件名,仅当文件名以.tmpl.html结尾时,这条注入规则才会触发。- 同理,TypeScript规则中的
#match? @filename "\.tmpl\.ts$"会仅匹配.tmpl.ts后缀的文件。 - 将上述规则保存到Neovim的Treesitter注入查询目录中,通常路径为
~/.config/nvim/after/queries/go-template/injections.scm,该路径下的规则会覆盖插件默认配置。
内容的提问来源于stack exchange,提问作者n0nvme
相关产品推荐
相关产品推荐

