feat(snippet): support multiple sessions #29340

This commit is contained in:
Maria Solano
2026-04-10 07:08:10 -07:00
committed by GitHub
parent 45f50d238a
commit c1c2648284
3 changed files with 23 additions and 5 deletions

View File

@@ -97,7 +97,7 @@ LSP
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_codeLens
• `textDocument/bar` …
https://microsoft.github.io/language-server-protocol/specification/#textDocument_colorPresentation
todo
Support for nested snippets.
LUA

View File

@@ -305,7 +305,7 @@ function Session:set_gravity()
end
end
local M = { session = nil }
local M = { _sessions = {} }
--- Displays the choices for the given tabstop as completion items.
---
@@ -595,6 +595,7 @@ function M.expand(input)
end_right_gravity = true,
})
M._session = Session.new(bufnr, snippet_extmark, tabstop_data)
table.insert(M._sessions, M._session)
-- Jump to the first tabstop.
M.jump(1)
@@ -678,10 +679,13 @@ function M.stop()
return
end
vim.api.nvim_clear_autocmds({ group = snippet_group, buf = M._session.bufnr })
vim.api.nvim_buf_clear_namespace(M._session.bufnr, snippet_ns, 0, -1)
if #M._sessions == 1 then
vim.api.nvim_clear_autocmds({ group = snippet_group, buffer = M._session.bufnr })
vim.api.nvim_buf_clear_namespace(M._session.bufnr, snippet_ns, 0, -1)
end
M._session = nil
table.remove(M._sessions)
M._session = #M._sessions > 0 and M._sessions[#M._sessions] or nil
end
return M

View File

@@ -403,4 +403,18 @@ describe('vim.snippet', function()
feed('foo')
eq({ '口口function(foo)' }, buf_lines(0))
end)
it('supports multiple snippet sessions', function()
test_expand_success({ 'func $1($3) {', ' $2', '}' }, { 'func () {', ' ', '}' })
feed('foo<Tab>')
test_expand_success({ 'var x = $1 + $2' }, { 'func foo() {', ' var x = + ', '}' })
feed('a<Tab>b')
feed('<Tab><Tab>a, b')
eq({ 'func foo(a, b) {', ' var x = a + b', '}' }, buf_lines(0))
end)
end)