From a5ade3c63d88e93244c43ff0f0635f4774f890ce Mon Sep 17 00:00:00 2001 From: glepnir Date: Thu, 29 Feb 2024 18:50:40 +0800 Subject: fix(snippet): correct indent with newline Problem: snippet newline use before line indent after expand. Solution: it should level + 1. --- runtime/lua/vim/snippet.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 09b7576d97..a660d6f301 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -446,14 +446,18 @@ function M.expand(input) base_indent = base_indent .. (snippet_lines[#snippet_lines]:match('(^%s*)%S') or '') --- @type string end + local shiftwidth = vim.fn.shiftwidth() + local curbuf = vim.api.nvim_get_current_buf() + local expandtab = vim.bo[curbuf].expandtab local lines = vim.iter.map(function(i, line) -- Replace tabs by spaces. - if vim.o.expandtab then - line = line:gsub('\t', (' '):rep(vim.fn.shiftwidth())) --- @type string + if expandtab then + line = line:gsub('\t', (' '):rep(shiftwidth)) --- @type string end -- Add the base indentation. if i > 1 then - line = base_indent .. line + line = #line ~= 0 and base_indent .. line + or (expandtab and (' '):rep(shiftwidth) or '\t'):rep(vim.fn.indent('.') / shiftwidth + 1) end return line end, ipairs(text_to_lines(text))) -- cgit From 14e4b6bbd8640675d7393bdeb3e93d74ab875ff1 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sat, 16 Mar 2024 17:11:42 +0000 Subject: refactor(lua): type annotations --- runtime/lua/vim/snippet.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 2ffd89367f..a1e3360b2d 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -254,9 +254,10 @@ local function display_choices(tabstop) assert(tabstop.choices, 'Tabstop has no choices') local start_col = tabstop:get_range()[2] + 1 - local matches = vim.iter.map(function(choice) - return { word = choice } - end, tabstop.choices) + local matches = {} --- @type table[] + for _, choice in ipairs(tabstop.choices) do + matches[#matches + 1] = { word = choice } + end vim.defer_fn(function() vim.fn.complete(start_col, matches) @@ -449,7 +450,9 @@ function M.expand(input) local shiftwidth = vim.fn.shiftwidth() local curbuf = vim.api.nvim_get_current_buf() local expandtab = vim.bo[curbuf].expandtab - local lines = vim.iter.map(function(i, line) + + local lines = {} --- @type string[] + for i, line in ipairs(text_to_lines(text)) do -- Replace tabs by spaces. if expandtab then line = line:gsub('\t', (' '):rep(shiftwidth)) --- @type string @@ -459,8 +462,8 @@ function M.expand(input) line = #line ~= 0 and base_indent .. line or (expandtab and (' '):rep(shiftwidth) or '\t'):rep(vim.fn.indent('.') / shiftwidth + 1) end - return line - end, ipairs(text_to_lines(text))) + lines[#lines + 1] = line + end table.insert(snippet_text, table.concat(lines, '\n')) end -- cgit From 4625394a767fab311f75ef40f4f15c661156e071 Mon Sep 17 00:00:00 2001 From: Mathias Fußenegger Date: Sun, 28 Apr 2024 12:49:25 +0200 Subject: fix(snippet): do not add extra indent on newlines (#28538) Reverts parts of https://github.com/neovim/neovim/pull/27674 LSP snippets typically do include tabs or spaces to add extra indentation and don't rely on the client using `autoindent` functionality. For example: public static void main(String[] args) {\n\t${0}\n} Notice the `\t` after `{\n` Adding spaces or tabs independent of that breaks snippets for languages like Haskell where you can have snippets like: ${1:name} :: ${2}\n${1:name} ${3}= ${0:undefined} To generate: name :: name = undefined --- runtime/lua/vim/snippet.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index a1e3360b2d..37416c389f 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -459,8 +459,7 @@ function M.expand(input) end -- Add the base indentation. if i > 1 then - line = #line ~= 0 and base_indent .. line - or (expandtab and (' '):rep(shiftwidth) or '\t'):rep(vim.fn.indent('.') / shiftwidth + 1) + line = base_indent .. line end lines[#lines + 1] = line end -- cgit From bc7f86209d3961aa479a8caeb792a8d39de55ece Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Mon, 29 Apr 2024 13:45:53 -0700 Subject: fix(lsp): redundant vim.snippet.jumpable #28560 --- runtime/lua/vim/snippet.lua | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 37416c389f..8447d08d17 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -532,29 +532,6 @@ end --- @alias vim.snippet.Direction -1 | 1 ---- Returns `true` if there is an active snippet which can be jumped in the given direction. ---- You can use this function to navigate a snippet as follows: ---- ---- ```lua ---- vim.keymap.set({ 'i', 's' }, '', function() ---- if vim.snippet.jumpable(1) then ---- return 'lua vim.snippet.jump(1)' ---- else ---- return '' ---- end ---- end, { expr = true }) ---- ``` ---- ---- @param direction (vim.snippet.Direction) Navigation direction. -1 for previous, 1 for next. ---- @return boolean -function M.jumpable(direction) - if not M.active() then - return false - end - - return M._session:get_dest_index(direction) ~= nil -end - --- Jumps within the active snippet in the given direction. --- If the jump isn't possible, the function call does nothing. --- @@ -604,11 +581,37 @@ function M.jump(direction) setup_autocmds(M._session.bufnr) end ---- Returns `true` if there's an active snippet in the current buffer. +--- @class vim.snippet.ActiveFilter +--- @field direction vim.snippet.Direction Navigation direction. -1 for previous, 1 for next. + +--- Returns `true` if there's an active snippet in the current buffer, +--- applying the given filter if provided. +--- +--- You can use this function to navigate a snippet as follows: --- +--- ```lua +--- vim.keymap.set({ 'i', 's' }, '', function() +--- if vim.snippet.active({ direction = 1 }) then +--- return 'lua vim.snippet.jump(1)' +--- else +--- return '' +--- end +--- end, { expr = true }) +--- ``` +--- +--- @param filter? vim.snippet.ActiveFilter Filter to constrain the search with: +--- - `direction` (vim.snippet.Direction): Navigation direction. Will return `true` if the snippet +--- can be jumped in the given direction. --- @return boolean -function M.active() - return M._session ~= nil and M._session.bufnr == vim.api.nvim_get_current_buf() +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 --- Exits the current snippet. -- cgit From 54dfee8f0a68985a196857821af1add287b5991a Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Thu, 2 May 2024 11:25:21 +0300 Subject: docs: add `hl-SnippetTabstop` tag --- runtime/lua/vim/snippet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 8447d08d17..18c613cb85 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -401,7 +401,7 @@ end --- Refer to https://microsoft.github.io/language-server-protocol/specification/#snippet_syntax --- for the specification of valid input. --- ---- Tabstops are highlighted with hl-SnippetTabstop. +--- Tabstops are highlighted with |hl-SnippetTabstop|. --- --- @param input string function M.expand(input) -- cgit From 783c1e596c35e44e9699e5a1881d3c8f5c4fc596 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 6 May 2024 04:42:30 -0700 Subject: refactor(snippet): rename exit() => stop() #28628 --- runtime/lua/vim/snippet.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 18c613cb85..8fe03b3882 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -343,7 +343,7 @@ local function setup_autocmds(bufnr) or cursor_row > snippet_range[3] or (cursor_row == snippet_range[3] and cursor_col > snippet_range[4]) then - M.exit() + M.stop() return true end @@ -362,7 +362,7 @@ local function setup_autocmds(bufnr) end -- The cursor is either not on a tabstop or we reached the end, so exit the session. - M.exit() + M.stop() return true end, }) @@ -378,7 +378,7 @@ local function setup_autocmds(bufnr) (snippet_range[1] == snippet_range[3] and snippet_range[2] == snippet_range[4]) or snippet_range[3] + 1 > vim.fn.line('$') then - M.exit() + M.stop() end if not M.active() then @@ -615,7 +615,7 @@ function M.active(filter) end --- Exits the current snippet. -function M.exit() +function M.stop() if not M.active() then return end -- cgit From 7acf39ddab8ebdb63ebf78ec980149d20783fd4b Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Wed, 15 May 2024 01:18:33 +0200 Subject: docs: misc (#28609) Closes https://github.com/neovim/neovim/issues/28484. Closes https://github.com/neovim/neovim/issues/28719. Co-authored-by: Chris Co-authored-by: Gregory Anders Co-authored-by: Jake B <16889000+jakethedev@users.noreply.github.com> Co-authored-by: Jonathan Raines Co-authored-by: Yi Ming Co-authored-by: Zane Dufour Co-authored-by: zeertzjq --- runtime/lua/vim/snippet.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 8fe03b3882..2d95d4203d 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -539,7 +539,7 @@ end --- --- ```lua --- vim.keymap.set({ 'i', 's' }, '', function() ---- if vim.snippet.jumpable(1) then +--- if vim.snippet.active({ direction = 1 }) then --- return 'lua vim.snippet.jump(1)' --- else --- return '' -- cgit From 01b6bff7e9bc751be20ce7bb68e7ebe3037441de Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 2 May 2024 15:57:21 +0200 Subject: docs: news Set dev_xx.txt help files to use "flow" layout. --- runtime/lua/vim/snippet.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/snippet.lua') diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index 2d95d4203d..3d8f73f362 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -532,10 +532,9 @@ end --- @alias vim.snippet.Direction -1 | 1 ---- Jumps within the active snippet in the given direction. ---- If the jump isn't possible, the function call does nothing. +--- Jumps to the next (or previous) placeholder in the current snippet, if possible. --- ---- You can use this function to navigate a snippet as follows: +--- For example, map `` to jump while a snippet is active: --- --- ```lua --- vim.keymap.set({ 'i', 's' }, '', function() -- cgit