| local G = vim.lsp._snippet_grammar |
| local snippet_group = vim.api.nvim_create_augroup('nvim.snippet', {}) |
| local snippet_ns = vim.api.nvim_create_namespace('nvim.snippet') |
| local hl_group = 'SnippetTabstop' |
|
|
| |
| |
| |
| local function cursor_pos() |
| local cursor = vim.api.nvim_win_get_cursor(0) |
| return cursor[1] - 1, cursor[2] |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| local function resolve_variable(var, default) |
| |
| |
| local function expand_or_default(str) |
| local expansion = vim.fn.expand(str) |
| return expansion == '' and default or expansion |
| end |
|
|
| if var == 'TM_SELECTED_TEXT' then |
| |
| return default |
| elseif var == 'TM_CURRENT_LINE' then |
| return vim.api.nvim_get_current_line() |
| elseif var == 'TM_CURRENT_WORD' then |
| return expand_or_default('<cword>') |
| elseif var == 'TM_LINE_INDEX' then |
| return tostring(vim.fn.line('.') - 1) |
| elseif var == 'TM_LINE_NUMBER' then |
| return tostring(vim.fn.line('.')) |
| elseif var == 'TM_FILENAME' then |
| return expand_or_default('%:t') |
| elseif var == 'TM_FILENAME_BASE' then |
| return expand_or_default('%:t:r') |
| elseif var == 'TM_DIRECTORY' then |
| return expand_or_default('%:p:h:t') |
| elseif var == 'TM_FILEPATH' then |
| return expand_or_default('%:p') |
| end |
|
|
| |
| return nil |
| end |
|
|
| |
| |
| |
| |
| local function text_to_lines(text) |
| text = type(text) == 'string' and { text } or text |
| |
| return vim.split(table.concat(text), '\n', { plain = true }) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| local function compute_tabstop_range(snippet, placeholder) |
| local cursor_row, cursor_col = cursor_pos() |
| local snippet_text = text_to_lines(snippet) |
| local placeholder_text = text_to_lines(placeholder or '') |
| local start_row = cursor_row + #snippet_text - 1 |
| local start_col = #(snippet_text[#snippet_text] or '') |
|
|
| |
| if start_row == cursor_row then |
| start_col = start_col + cursor_col |
| end |
|
|
| local end_row = start_row + #placeholder_text - 1 |
| local end_col = (start_row == end_row and start_col or 0) |
| + #(placeholder_text[#placeholder_text] or '') |
|
|
| return { start_row, start_col, end_row, end_col } |
| end |
|
|
| |
| |
| |
| |
| |
| local function get_extmark_range(bufnr, extmark_id) |
| local mark = vim.api.nvim_buf_get_extmark_by_id(bufnr, snippet_ns, extmark_id, { details = true }) |
|
|
| |
| return { mark[1], mark[2], mark[3].end_row, mark[3].end_col } |
| end |
|
|
| |
| |
| |
| |
| |
| local Tabstop = {} |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function Tabstop.new(index, bufnr, range, choices) |
| local extmark_id = vim.api.nvim_buf_set_extmark(bufnr, snippet_ns, range[1], range[2], { |
| right_gravity = true, |
| end_right_gravity = false, |
| end_line = range[3], |
| end_col = range[4], |
| hl_group = hl_group, |
| }) |
|
|
| local self = setmetatable( |
| { extmark_id = extmark_id, bufnr = bufnr, index = index, choices = choices }, |
| { __index = Tabstop } |
| ) |
|
|
| return self |
| end |
|
|
| |
| |
| |
| |
| function Tabstop:get_range() |
| return get_extmark_range(self.bufnr, self.extmark_id) |
| end |
|
|
| |
| |
| |
| |
| function Tabstop:get_text() |
| local range = self:get_range() |
| return table.concat( |
| vim.api.nvim_buf_get_text(self.bufnr, range[1], range[2], range[3], range[4], {}), |
| '\n' |
| ) |
| end |
|
|
| |
| |
| |
| |
| function Tabstop:set_text(text) |
| local range = self:get_range() |
| vim.api.nvim_buf_set_text(self.bufnr, range[1], range[2], range[3], range[4], text_to_lines(text)) |
| end |
|
|
| |
| |
| |
| |
| function Tabstop:set_right_gravity(right_gravity) |
| local range = self:get_range() |
| self.extmark_id = vim.api.nvim_buf_set_extmark(self.bufnr, snippet_ns, range[1], range[2], { |
| right_gravity = right_gravity, |
| end_right_gravity = not right_gravity, |
| end_line = range[3], |
| end_col = range[4], |
| hl_group = hl_group, |
| }) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| local Session = {} |
|
|
| |
| |
| |
| |
| |
| |
| |
| function Session.new(bufnr, snippet_extmark, tabstop_data) |
| local self = setmetatable({ |
| bufnr = bufnr, |
| extmark_id = snippet_extmark, |
| tabstops = {}, |
| current_tabstop = Tabstop.new(0, bufnr, { 0, 0, 0, 0 }), |
| tab_keymaps = { i = nil, s = nil }, |
| shift_tab_keymaps = { i = nil, s = nil }, |
| }, { __index = Session }) |
|
|
| |
| for index, ranges in pairs(tabstop_data) do |
| for _, data in ipairs(ranges) do |
| self.tabstops[index] = self.tabstops[index] or {} |
| table.insert(self.tabstops[index], Tabstop.new(index, self.bufnr, data.range, data.choices)) |
| end |
| end |
|
|
| return self |
| end |
|
|
| |
| |
| |
| |
| |
| function Session:get_dest_index(direction) |
| local tabstop_indexes = vim.tbl_keys(self.tabstops) |
| table.sort(tabstop_indexes) |
| for i, index in ipairs(tabstop_indexes) do |
| if index == self.current_tabstop.index then |
| local dest_index = tabstop_indexes[i + direction] |
| |
| if not dest_index and direction == 1 then |
| dest_index = 0 |
| end |
| |
| if dest_index == 0 and direction == -1 then |
| dest_index = nil |
| end |
| return dest_index |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| function Session:set_group_gravity(index, right_gravity) |
| for _, tabstop in ipairs(self.tabstops[index]) do |
| tabstop:set_right_gravity(right_gravity) |
| end |
| end |
|
|
| local M = { session = nil } |
|
|
| |
| |
| |
| local function display_choices(tabstop) |
| assert(tabstop.choices, 'Tabstop has no choices') |
|
|
| local text = tabstop:get_text() |
| local found_text = false |
|
|
| local start_col = tabstop:get_range()[2] + 1 |
| local matches = {} |
| for _, choice in ipairs(tabstop.choices) do |
| if choice ~= text then |
| matches[#matches + 1] = { word = choice } |
| else |
| found_text = true |
| end |
| end |
|
|
| if found_text then |
| table.insert(matches, 1, text) |
| end |
|
|
| vim.defer_fn(function() |
| vim.fn.complete(start_col, matches) |
| end, 100) |
| end |
|
|
| |
| |
| |
| local function select_tabstop(tabstop) |
| |
| local function feedkeys(keys) |
| keys = vim.api.nvim_replace_termcodes(keys, true, false, true) |
| vim.api.nvim_feedkeys(keys, 'n', true) |
| end |
|
|
| local range = tabstop:get_range() |
| local mode = vim.fn.mode() |
|
|
| if vim.fn.pumvisible() ~= 0 then |
| |
| vim.fn.complete(vim.fn.col('.'), {}) |
| end |
|
|
| |
| vim.api.nvim_win_set_cursor(0, { range[1] + 1, range[2] }) |
|
|
| |
| if tabstop.choices or tabstop.index == 0 or (range[1] == range[3] and range[2] == range[4]) then |
| if mode ~= 'i' then |
| if mode == 's' then |
| feedkeys('<Esc>') |
| end |
| vim.cmd.startinsert({ bang = range[4] >= #vim.api.nvim_get_current_line() }) |
| end |
| if tabstop.choices then |
| vim.fn.cursor(range[3] + 1, range[4] + 1) |
| display_choices(tabstop) |
| end |
| else |
| |
| |
| |
| local keys = { |
| mode ~= 'n' and '<Esc>' or '', |
| ('<Cmd>call cursor(%s,%s)<CR>'):format(range[1] + 1, range[2] + 1), |
| 'v', |
| ('<Cmd>call cursor(%s,%s)<CR>'):format(range[3] + 1, range[4]), |
| 'o<c-g><c-r>_', |
| } |
| feedkeys(table.concat(keys)) |
| end |
| end |
|
|
| |
| |
| |
| local function setup_autocmds(bufnr) |
| vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { |
| group = snippet_group, |
| desc = 'Update snippet state when the cursor moves', |
| buffer = bufnr, |
| callback = function() |
| |
| if not vim.fn.mode():match('^[isS]') then |
| return |
| end |
|
|
| local cursor_row, cursor_col = cursor_pos() |
|
|
| |
| local snippet_range = get_extmark_range(bufnr, M._session.extmark_id) |
| if |
| cursor_row < snippet_range[1] |
| or (cursor_row == snippet_range[1] and cursor_col < snippet_range[2]) |
| or cursor_row > snippet_range[3] |
| or (cursor_row == snippet_range[3] and cursor_col > snippet_range[4]) |
| then |
| M.stop() |
| return true |
| end |
|
|
| for tabstop_index, tabstops in pairs(M._session.tabstops) do |
| for _, tabstop in ipairs(tabstops) do |
| local range = tabstop:get_range() |
| if |
| (cursor_row > range[1] or (cursor_row == range[1] and cursor_col >= range[2])) |
| and (cursor_row < range[3] or (cursor_row == range[3] and cursor_col <= range[4])) |
| then |
| if tabstop_index ~= 0 then |
| return |
| end |
| end |
| end |
| end |
|
|
| |
| M.stop() |
| return true |
| end, |
| }) |
|
|
| vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, { |
| group = snippet_group, |
| desc = 'Update active tabstops when buffer text changes', |
| buffer = bufnr, |
| callback = function() |
| |
| local snippet_range = get_extmark_range(M._session.bufnr, M._session.extmark_id) |
| if |
| (snippet_range[1] == snippet_range[3] and snippet_range[2] == snippet_range[4]) |
| or snippet_range[3] + 1 > vim.fn.line('$') |
| then |
| M.stop() |
| end |
|
|
| if not M.active() then |
| return true |
| end |
|
|
| |
| local current_tabstop = M._session.current_tabstop |
| local current_text = current_tabstop:get_text() |
| for _, tabstop in ipairs(M._session.tabstops[current_tabstop.index]) do |
| if tabstop.extmark_id ~= current_tabstop.extmark_id then |
| tabstop:set_text(current_text) |
| end |
| end |
| end, |
| }) |
|
|
| vim.api.nvim_create_autocmd('BufLeave', { |
| group = snippet_group, |
| desc = 'Stop the snippet session when leaving the buffer', |
| buffer = bufnr, |
| callback = function() |
| M.stop() |
| end, |
| }) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| function M.expand(input) |
| local snippet = G.parse(input) |
| local snippet_text = {} |
| |
| local base_indent = vim.api.nvim_get_current_line():match('^%s*') or '' |
|
|
| |
| |
| local placeholders = {} |
| for _, child in ipairs(snippet.data.children) do |
| local type, data = child.type, child.data |
| if type == G.NodeType.Placeholder then |
| |
| local tabstop, value = data.tabstop, tostring(data.value) |
| if placeholders[tabstop] and placeholders[tabstop] ~= value then |
| error('Snippet has multiple placeholders for tabstop $' .. tabstop) |
| end |
| placeholders[tabstop] = value |
| end |
| end |
|
|
| |
| |
| local tabstop_data = {} |
|
|
| |
| |
| |
| local function add_tabstop(index, placeholder, choices) |
| tabstop_data[index] = tabstop_data[index] or {} |
| local range = compute_tabstop_range(snippet_text, placeholder) |
| table.insert(tabstop_data[index], { range = range, choices = choices }) |
| end |
|
|
| |
| |
| |
| local function append_to_snippet(text) |
| local shiftwidth = vim.fn.shiftwidth() |
| local curbuf = vim.api.nvim_get_current_buf() |
| local expandtab = vim.bo[curbuf].expandtab |
|
|
| local lines = {} |
| for i, line in ipairs(text_to_lines(text)) do |
| |
| if expandtab then |
| line = line:gsub('\t', (' '):rep(shiftwidth)) |
| end |
| |
| if i > 1 then |
| line = base_indent .. line |
| end |
| lines[#lines + 1] = line |
| end |
|
|
| table.insert(snippet_text, table.concat(lines, '\n')) |
| end |
|
|
| for _, child in ipairs(snippet.data.children) do |
| local type, data = child.type, child.data |
| if type == G.NodeType.Tabstop then |
| |
| local placeholder = placeholders[data.tabstop] |
| add_tabstop(data.tabstop, placeholder) |
| if placeholder then |
| append_to_snippet(placeholder) |
| end |
| elseif type == G.NodeType.Placeholder then |
| |
| local value = placeholders[data.tabstop] |
| add_tabstop(data.tabstop, value) |
| append_to_snippet(value) |
| elseif type == G.NodeType.Choice then |
| |
| add_tabstop(data.tabstop, nil, data.values) |
| elseif type == G.NodeType.Variable then |
| |
| |
| local value = resolve_variable(data.name, data.default and tostring(data.default) or '') |
| if not value then |
| |
| value = data.name |
| local tabstop_indexes = vim.tbl_keys(tabstop_data) |
| local index = math.max(unpack((#tabstop_indexes == 0 and { 0 }) or tabstop_indexes)) + 1 |
| add_tabstop(index, value) |
| end |
| append_to_snippet(value) |
| elseif type == G.NodeType.Text then |
| |
| append_to_snippet(data.text) |
| end |
| end |
|
|
| |
| |
| if vim.tbl_contains(vim.tbl_keys(tabstop_data), 0) then |
| assert(#tabstop_data[0] == 1, 'Snippet has multiple $0 tabstops') |
| else |
| add_tabstop(0) |
| end |
|
|
| snippet_text = text_to_lines(snippet_text) |
|
|
| |
| local bufnr = vim.api.nvim_get_current_buf() |
| local cursor_row, cursor_col = cursor_pos() |
| vim.api.nvim_buf_set_text(bufnr, cursor_row, cursor_col, cursor_row, cursor_col, snippet_text) |
|
|
| |
| local snippet_extmark = vim.api.nvim_buf_set_extmark(bufnr, snippet_ns, cursor_row, cursor_col, { |
| end_line = cursor_row + #snippet_text - 1, |
| end_col = #snippet_text > 1 and #snippet_text[#snippet_text] or cursor_col + #snippet_text[1], |
| right_gravity = false, |
| end_right_gravity = true, |
| }) |
| M._session = Session.new(bufnr, snippet_extmark, tabstop_data) |
|
|
| |
| M.jump(1) |
| end |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.jump(direction) |
| |
| local dest_index = M._session and M._session:get_dest_index(direction) |
| if not dest_index then |
| return |
| end |
|
|
| |
| local tabstops = M._session.tabstops[dest_index] |
| local dest = tabstops[1] |
| for _, tabstop in ipairs(tabstops) do |
| local dest_range, range = dest:get_range(), tabstop:get_range() |
| if (range[1] < dest_range[1]) or (range[1] == dest_range[1] and range[2] < dest_range[2]) then |
| dest = tabstop |
| end |
| end |
|
|
| |
| vim.api.nvim_clear_autocmds({ group = snippet_group, buffer = M._session.bufnr }) |
|
|
| |
| M._session:set_group_gravity(M._session.current_tabstop.index, true) |
|
|
| M._session.current_tabstop = dest |
| select_tabstop(dest) |
|
|
| |
| M._session:set_group_gravity(dest.index, false) |
|
|
| |
| setup_autocmds(M._session.bufnr) |
| end |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| function M.active(filter) |
| local active = M._session ~= nil and M._session.bufnr == vim.api.nvim_get_current_buf() |
|
|
| local in_direction = true |
| if active and filter and filter.direction then |
| in_direction = M._session:get_dest_index(filter.direction) ~= nil |
| end |
|
|
| return active and in_direction |
| end |
|
|
| |
| function M.stop() |
| if not M.active() then |
| return |
| end |
|
|
| 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) |
|
|
| M._session = nil |
| end |
|
|
| return M |
|
|