mini.nvim icon indicating copy to clipboard operation
mini.nvim copied to clipboard

Add parameter to override treesitter parser in favor of filetype

Open thmsaurel opened this issue 1 year ago • 1 comments

Contributing guidelines

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

  1. Create separate 'nvim-repro' config directory:

    • '~/.config/nvim-repro/' on Unix
    • '~/AppData/Local/nvim-repro/' on Windows
  2. 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 },
    })
    
  3. Create a snippets directory inside nvim-repro and create the two following files: ansible.json and yaml.json Populate ansible.json as follow:

    {
      "test": {
        "prefix": "test",
        "body": "this is an ansible snippet",
        "description": "'test' snippet for ansible"
      }
    }
    

    Populate yaml.json as follow:

    {
      "test": {
        "prefix": "test",
        "body": "this is a yaml snippet",
        "description": "'test' snippet for yaml"
      }
    }
    
  4. Run NVIM_APPNAME=nvim-repro nvim (i.e. execute nvim with NVIM_APPNAME environment variable set to "nvim-repro"). Wait for all dependencies to install.

  5. Open an ansible file (or test.yml and force filetype to yaml.ansible). Try to expand the test snippet.

thmsaurel avatar Mar 14 '25 15:03 thmsaurel

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.

echasnovski avatar Mar 14 '25 17:03 echasnovski