Add parameter to override treesitter parser in favor of filetype
Contributing guidelines
- [x] I have read CONTRIBUTING.md
- [x] I have read CODE_OF_CONDUCT.md
- [x] I have updated 'mini.nvim' to latest version of the
mainbranch
Module(s)
mini.snippets
Neovim version
0.11 (!at least latest Nightly build!)
Description
TL;DR: It would be nice to force mini.snippets to use filetype instead of tree-sitter parser through buffer options.
Context:
I'm using mfussenegger/nvim-ansible to detect ansible files. It set the filetype to yaml.ansible while preserving yaml syntax. So the tree-sitter parser for YAML is used for syntax.
When I'm trying to expand a snippet inside an ansible file, YAML snippets are used.
When disabling tree-sitter, the behavior is the expected one.
I'm pretty sure I have to do something with the MiniSnippets.default_* functions to override this behavior but I'ld prefer use the default behavior (using snippets/*.json files).
Reproduction
-
Create separate 'nvim-repro' config directory:
- '~/.config/nvim-repro/' on Unix
- '~/AppData/Local/nvim-repro/' on Windows
-
Inside 'nvim-repro' directory create a file named 'init.lua'. Populate it with the following content:
-- Clone latest 'mini.nvim' (requires Git CLI installed) vim.cmd('echo "Installing `mini.nvim`" | redraw') local mini_path = vim.fn.stdpath("data") .. "/site/pack/deps/start/mini.nvim" local clone_cmd = { "git", "clone", "--depth=1", "https://github.com/echasnovski/mini.nvim", mini_path } vim.fn.system(clone_cmd) vim.cmd('echo "`mini.nvim` is installed" | redraw') -- Make sure 'mini.nvim' is available vim.cmd("packadd mini.nvim") require("mini.deps").setup() -- Add extra setup steps needed to reproduce the behavior -- Use `MiniDeps.add('user/repo')` to install another plugin from GitHub MiniDeps.add("mfussenegger/nvim-ansible") MiniDeps.add({ source = "nvim-treesitter/nvim-treesitter", hooks = { post_checkout = function() vim.cmd("TSUpdate") end, }, }) require("mini.snippets").setup({ snippets = { require("mini.snippets").gen_loader.from_file(vim.fn.stdpath("config") .. "/snippets/global.json"), require("mini.snippets").gen_loader.from_lang({ lang_patterns = { ["yaml.ansible"] = { "ansible.json" } } }), }, }) require("nvim-treesitter.configs").setup({ ensure_installed = { "yaml" }, highlight = { enable = true }, }) -
Create a
snippetsdirectory insidenvim-reproand create the two following files:ansible.jsonandyaml.jsonPopulateansible.jsonas follow:{ "test": { "prefix": "test", "body": "this is an ansible snippet", "description": "'test' snippet for ansible" } }Populate
yaml.jsonas follow:{ "test": { "prefix": "test", "body": "this is a yaml snippet", "description": "'test' snippet for yaml" } } -
Run
NVIM_APPNAME=nvim-repro nvim(i.e. executenvimwithNVIM_APPNAMEenvironment variable set to "nvim-repro"). Wait for all dependencies to install. -
Open an ansible file (or
test.ymland forcefiletypetoyaml.ansible). Try to expand thetestsnippet.
Thanks for the issue!
This is already possible and indeed requires to use default_prepare(). Here is one approach:
local my_prepare = function(raw_snippets)
local _, cont = MiniSnippets.default_prepare({})
cont.lang = vim.bo.filetype == "yaml.ansible" and "yaml.ansible" or cont.lang
return MiniSnippets.default_prepare(raw_snippets, { context = cont })
end
require("mini.snippets").setup({
snippets = {
require("mini.snippets").gen_loader.from_file(vim.fn.stdpath("config") .. "/snippets/global.json"),
require("mini.snippets").gen_loader.from_lang({ lang_patterns = { ["yaml.ansible"] = { "ansible.json" } } }),
},
expand = { prepare = my_prepare },
})
This is a documented way of adjusting context used for snippet preparation and I think is a fairly reasonable way to provide flexible customization here.
Providing an option to disable usage of tree-sitter language as lang in context doesn't look like a good idea. Mostly because it will make impossible to have "local snippet context" inside injected languages (like in markdown code blocks). As there are reasonably concise workarounds for handful (at the moment) cases, I think the current design is okay.
Closing as already possible.