Skip to content

Commit

Permalink
feat(snippet): add mini.snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Jan 9, 2025
1 parent 3a7337e commit e5b86fe
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lua/astrocommunity/snippet/mini-snippets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# mini.snippets

Manage and expand snippets. Part of 'mini.nvim' library.

**Repository:** <https://github.com/echasnovski/mini.snippets>

Sets up:

- Leaving insert mode while in a snippet session ends the session
- Disable by disabling the `mini_snippets_stop` autocommand `augroup` in AstroCore
- Configures the following snippet locations:
- Global user snippets in `~/.config/nvim/snippets/global.json`
- Language specific user snippets in `~/.config/nvim/snippets/<lang>.json`
- Snippets in the runtime for plugins with a `snippets` folder such as `friendly-snippets`
- Project local snippets including `.vscode/project.code-snippets` and `.vscode/<lang>.code-snippets`

Provided Integrations:

- Adds [`friendly-snippets`](https://github.com/rafamadriz/friendly-snippets)
- Disables LuaSnip
- Configures `blink.cmp` to use `mini.snippets` if it's available
- Configures `nvim-cmp` to use `mini.snippets` if it's available
104 changes: 104 additions & 0 deletions lua/astrocommunity/snippet/mini-snippets/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
return {
"echasnovski/mini.snippets",
dependencies = "rafamadriz/friendly-snippets",
opts_extend = { "snippets" },
opts = function(_, opts)
local mini_snippets = require "mini.snippets"
local gen_loader = mini_snippets.gen_loader
opts.snippets = vim.list_extend({
-- global snippets file
gen_loader.from_file(vim.fn.stdpath "config" .. "/snippets/global.json"),
-- load language specific snippets from runtime
gen_loader.from_lang(),
-- load global, project local snippets
gen_loader.from_file ".vscode/project.code-snippets",
-- load language specific, project local snippets
function(context)
local rel_path = ".vscode/" .. context.lang .. ".code-snippets"
return vim.fn.filereadable(rel_path) == 1 and mini_snippets.read_file(rel_path)
end,
}, opts.snippets or {})
end,
specs = {
{
"AstroNvim/astrocore",
---@param opts AstroCoreOpts
opts = function(_, opts)
local snippet_mode_change
local stop_all_sessions = function()
local mini_snippets = require "mini.snippets"
while mini_snippets.session.get(false) do
mini_snippets.session.stop()
end
snippet_mode_change = nil
end
opts.autocmds.mini_snippets_stop = {
{
event = "User",
pattern = "MiniSnippetsSessionStart",
desc = "Set up snippet stopping when going to normal mode autocmd",
callback = function()
if not snippet_mode_change then
snippet_mode_change = vim.api.nvim_create_autocmd(
"ModeChanged",
{ pattern = "*:n", once = true, callback = stop_all_sessions }
)
end
end,
},
}
end,
},
{
"hrsh7th/nvim-cmp",
optional = true,
dependencies = { { "abeldekat/cmp-mini-snippets", lazy = true } },
opts = function(_, opts)
local mini_snippets, cmp = require "mini.snippets", require "cmp"

if not opts.snippet then opts.snippet = {} end
opts.snippet.expand = function(args)
(mini_snippets.config.expand.insert or mini_snippets.default_insert) { body = args.body }
end

if not opts.sources then opts.sources = {} end
table.insert(opts.sources, { name = "mini_snippets", priority = 750 })

local function has_words_before()
local line, col = (unpack or table.unpack)(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match "%s" == nil
end
local function is_visible(CMP) return CMP.core.view:visible() or vim.fn.pumvisible() == 1 end

if not opts.mappings then opts.mappings = {} end
opts.mapping["<Tab>"] = cmp.mapping(function(fallback)
if is_visible(cmp) then
cmp.select_next_item()
elseif vim.api.nvim_get_mode().mode ~= "c" and mini_snippets.session.get(false) then
mini_snippets.session.jump "next"
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end)
opts.mapping["<S-Tab>"] = cmp.mapping(function(fallback)
if is_visible(cmp) then
cmp.select_prev_item()
elseif vim.api.nvim_get_mode().mode ~= "c" and mini_snippets.session.get(false) then
mini_snippets.session.jump "prev"
else
fallback()
end
end)
end,
},
{
"Saghen/blink.cmp",
dependencies = "echasnovski/mini.snippets",
optional = true,
opts = { snippets = { preset = "mini_snippets" } },
},
{ "L3MON4D3/LuaSnip", enabled = false },
},
}

0 comments on commit e5b86fe

Please sign in to comment.