diff --git a/lua/luasnip/config.lua b/lua/luasnip/config.lua index 41518b050..c9eb9f6df 100644 --- a/lua/luasnip/config.lua +++ b/lua/luasnip/config.lua @@ -101,17 +101,14 @@ c = { require("luasnip").unlink_current_if_deleted ) end - ls_autocmd( - session.config.update_events, - function() - -- don't update due to events if an update due to luasnip is pending anyway. - -- (Also, this would be bad because luasnip may not be in an - -- consistent state whenever an autocommand is triggered) - if not session.jump_active then - require("luasnip").active_update_dependents() - end + ls_autocmd(session.config.update_events, function() + -- don't update due to events if an update due to luasnip is pending anyway. + -- (Also, this would be bad because luasnip may not be in an + -- consistent state whenever an autocommand is triggered) + if not session.jump_active then + require("luasnip").active_update_dependents() end - ) + end) if session.config.region_check_events ~= nil then ls_autocmd(session.config.region_check_events, function() require("luasnip").exit_out_of_region( diff --git a/lua/luasnip/extras/select_choice.lua b/lua/luasnip/extras/select_choice.lua index b7b55e1a2..b1c776ab3 100644 --- a/lua/luasnip/extras/select_choice.lua +++ b/lua/luasnip/extras/select_choice.lua @@ -9,7 +9,7 @@ local function set_choice_callback(data) end -- feed+immediately execute i to enter INSERT after vim.ui.input closes. -- vim.api.nvim_feedkeys("i", "x", false) - ls.set_choice(indx, {cursor_restore_data = data}) + ls.set_choice(indx, { cursor_restore_data = data }) end end diff --git a/lua/luasnip/init.lua b/lua/luasnip/init.lua index 6d5c090f2..bbe12903c 100644 --- a/lua/luasnip/init.lua +++ b/lua/luasnip/init.lua @@ -31,7 +31,6 @@ local function no_region_check_wrap(fn, ...) return fn_res end - local function get_active_snip() local node = session.current_nodes[vim.api.nvim_get_current_buf()] if not node then @@ -153,7 +152,7 @@ local function get_corresponding_node(parent, data) return parent:find_node(function(test_node) return (test_node.store_id == data.store_id) or (data.key ~= nil and test_node.key == data.key) - end, {find_in_child_snippets = true}) + end, { find_in_child_snippets = true }) end local function node_update_dependents_preserve_position(node, current, opts) @@ -182,7 +181,10 @@ local function node_update_dependents_preserve_position(node, current, opts) if not opts.no_move and opts.restore_position then -- node is visible: restore position. local active_snippet = current:get_snippet() - node_util.restore_cursor_pos_relative(current, restore_data[active_snippet]) + node_util.restore_cursor_pos_relative( + current, + restore_data[active_snippet] + ) end return { jump_done = false, new_current = current } @@ -227,7 +229,10 @@ local function node_update_dependents_preserve_position(node, current, opts) if not opts.no_move and opts.restore_position then -- node is visible: restore position - node_util.restore_cursor_pos_relative(new_current, snip_restore_data) + node_util.restore_cursor_pos_relative( + new_current, + snip_restore_data + ) end return { jump_done = false, new_current = new_current } @@ -253,7 +258,8 @@ local function update_dependents(node) { no_move = false, restore_position = true } ) upd_res.new_current:focus() - session.current_nodes[vim.api.nvim_get_current_buf()] = upd_res.new_current + session.current_nodes[vim.api.nvim_get_current_buf()] = + upd_res.new_current end end local function active_update_dependents() @@ -581,7 +587,8 @@ local function change_choice(val, opts) -- if the active choice exists current_node still does. local current_node = session.current_nodes[vim.api.nvim_get_current_buf()] - local restore_data = opts and opts.cursor_restore_data or node_util.store_cursor_node_relative(current_node) + local restore_data = opts and opts.cursor_restore_data + or node_util.store_cursor_node_relative(current_node) local new_active = no_region_check_wrap( safe_choice_action, @@ -598,7 +605,8 @@ end local function set_choice(choice_indx, opts) local current_node = session.current_nodes[vim.api.nvim_get_current_buf()] - local restore_data = opts and opts.cursor_restore_data or node_util.store_cursor_node_relative(current_node) + local restore_data = opts and opts.cursor_restore_data + or node_util.store_cursor_node_relative(current_node) local active_choice = session.active_choice_nodes[vim.api.nvim_get_current_buf()] @@ -995,7 +1003,7 @@ ls = lazy_table({ extend_decorator = extend_decorator, log = require("luasnip.util.log"), activate_node = activate_node, - no_region_check_wrap = no_region_check_wrap + no_region_check_wrap = no_region_check_wrap, }, ls_lazy) return ls diff --git a/lua/luasnip/nodes/choiceNode.lua b/lua/luasnip/nodes/choiceNode.lua index 777c56070..22e95938f 100644 --- a/lua/luasnip/nodes/choiceNode.lua +++ b/lua/luasnip/nodes/choiceNode.lua @@ -169,10 +169,7 @@ function ChoiceNode:get_static_text() end function ChoiceNode:get_docstring() - return util.string_wrap( - self.choices[1]:get_docstring(), - self.pos - ) + return util.string_wrap(self.choices[1]:get_docstring(), self.pos) end function ChoiceNode:jump_into(dir, no_move, dry_run) @@ -276,7 +273,7 @@ function ChoiceNode:set_choice(choice, current_node, cursor_restore_data) if self.restore_cursor then local target_node = self:find_node(function(test_node) return test_node.change_choice_id == change_choice_id - end, {find_in_child_snippets = true}) + end, { find_in_child_snippets = true }) if target_node then -- the node that the cursor was in when changeChoice was called @@ -284,7 +281,10 @@ function ChoiceNode:set_choice(choice, current_node, cursor_restore_data) -- and this choiceNode, then set the cursor. node_util.refocus(self, target_node) - node_util.restore_cursor_pos_relative(target_node, cursor_restore_data[target_node.parent.snippet]) + node_util.restore_cursor_pos_relative( + target_node, + cursor_restore_data[target_node.parent.snippet] + ) return target_node end diff --git a/lua/luasnip/nodes/dynamicNode.lua b/lua/luasnip/nodes/dynamicNode.lua index d0ccc72a4..21941c90d 100644 --- a/lua/luasnip/nodes/dynamicNode.lua +++ b/lua/luasnip/nodes/dynamicNode.lua @@ -168,7 +168,12 @@ function DynamicNode:update() tmp = SnippetNode(nil, {}) else -- also enter node here. - tmp = self.fn(effective_args, self.parent, nil, unpack(self.user_args)) + tmp = self.fn( + effective_args, + self.parent, + nil, + unpack(self.user_args) + ) end end @@ -270,8 +275,13 @@ function DynamicNode:update_static() tmp = SnippetNode(nil, {}) else -- also enter node here. - ok, tmp = - pcall(self.fn, effective_args, self.parent, nil, unpack(self.user_args)) + ok, tmp = pcall( + self.fn, + effective_args, + self.parent, + nil, + unpack(self.user_args) + ) end end if not ok then @@ -323,7 +333,11 @@ function DynamicNode:update_static() tmp:update_static() -- updates own dependents. - self:update_dependents_static({ own = true, parents = true, children = true }) + self:update_dependents_static({ + own = true, + parents = true, + children = true, + }) end function DynamicNode:exit() @@ -358,7 +372,11 @@ function DynamicNode:update_restore() local str_args = node_util.str_args(args) -- only insert snip if it is not currently visible! - if self.snip and not self.snip.visible and vim.deep_equal(str_args, self.last_args) then + if + self.snip + and not self.snip.visible + and vim.deep_equal(str_args, self.last_args) + then local tmp = self.snip -- position might (will probably!!) still have changed, so update it diff --git a/lua/luasnip/nodes/insertNode.lua b/lua/luasnip/nodes/insertNode.lua index 24b71ffb2..121449a88 100644 --- a/lua/luasnip/nodes/insertNode.lua +++ b/lua/luasnip/nodes/insertNode.lua @@ -343,7 +343,15 @@ function InsertNode:get_snippetstring() local self_from, self_to = self.mark:pos_begin_end_raw() -- only do one get_text, and establish relative offsets partition this -- text. - local ok, text = pcall(vim.api.nvim_buf_get_text, 0, self_from[1], self_from[2], self_to[1], self_to[2], {}) + local ok, text = pcall( + vim.api.nvim_buf_get_text, + 0, + self_from[1], + self_from[2], + self_to[1], + self_to[2], + {} + ) local snippetstring = snippet_string.new() @@ -353,17 +361,32 @@ function InsertNode:get_snippetstring() return snippetstring end - local current = {0,0} + local current = { 0, 0 } for _, snip in ipairs(self:child_snippets()) do local snip_from, snip_to = snip.mark:pos_begin_end_raw() local snip_from_base_rel = util.pos_offset(self_from, snip_from) - local snip_to_base_rel = util.pos_offset(self_from, snip_to) - - snippetstring:append_text(str_util.multiline_substr(text, current, snip_from_base_rel)) - snippetstring:append_snip(snip, str_util.multiline_substr(text, snip_from_base_rel, snip_to_base_rel)) + local snip_to_base_rel = util.pos_offset(self_from, snip_to) + + snippetstring:append_text( + str_util.multiline_substr(text, current, snip_from_base_rel) + ) + snippetstring:append_snip( + snip, + str_util.multiline_substr( + text, + snip_from_base_rel, + snip_to_base_rel + ) + ) current = snip_to_base_rel end - snippetstring:append_text(str_util.multiline_substr(text, current, util.pos_offset(self_from, self_to))) + snippetstring:append_text( + str_util.multiline_substr( + text, + current, + util.pos_offset(self_from, self_to) + ) + ) return snippetstring end @@ -397,12 +420,18 @@ function InsertNode:put_initial(pos) -- index. true, -- don't enter snippets, we want to find the position of this node. - node_util.binarysearch_preference.outside) + node_util.binarysearch_preference.outside + ) for snip in self.static_text:iter_snippets() do -- don't have to pass a current_node, we don't need it since we can -- certainly link the snippet into this insertNode. - snip:insert_into_jumplist(nil, self, self.parent.snippet.child_snippets, child_snippet_idx) + snip:insert_into_jumplist( + nil, + self, + self.parent.snippet.child_snippets, + child_snippet_idx + ) child_snippet_idx = child_snippet_idx + 1 end diff --git a/lua/luasnip/nodes/node.lua b/lua/luasnip/nodes/node.lua index 1b12c6cc2..0c750627d 100644 --- a/lua/luasnip/nodes/node.lua +++ b/lua/luasnip/nodes/node.lua @@ -255,7 +255,13 @@ local function get_args(node, get_text_func_name, static) -- * we are doing a static update and it is not static_visible or -- visible (this second condition is to allow the docstring-generation -- to be improved by data provided after the expansion) - if argnode and ((static and (argnode.static_visible or argnode.visible)) or (not static and argnode.visible)) then + if + argnode + and ( + (static and (argnode.static_visible or argnode.visible)) + or (not static and argnode.visible) + ) + then if not static then -- Don't store (aka call get_snippetstring) if this is a static -- update (there will be no associated buffer-region!) and @@ -576,17 +582,12 @@ end function Node:linkable() -- linkable if insert or exitNode. - return vim.tbl_contains( - { types.insertNode, types.exitNode }, - self.type - ) + return vim.tbl_contains({ types.insertNode, types.exitNode }, self.type) end function Node:interactive() -- interactive if immediately inside choiceNode. - return vim.tbl_contains( - { types.insertNode, types.exitNode }, - self.type - ) or self.choice ~= nil + return vim.tbl_contains({ types.insertNode, types.exitNode }, self.type) + or self.choice ~= nil end function Node:leaf() return vim.tbl_contains( diff --git a/lua/luasnip/nodes/util.lua b/lua/luasnip/nodes/util.lua index 8a1ef1d31..ab11774fe 100644 --- a/lua/luasnip/nodes/util.lua +++ b/lua/luasnip/nodes/util.lua @@ -185,10 +185,7 @@ end local function linkable_node(node) -- node.type has to be one of insertNode, exitNode. - return vim.tbl_contains( - { types.insertNode, types.exitNode }, - node.type - ) + return vim.tbl_contains({ types.insertNode, types.exitNode }, node.type) end -- mainly used internally, by binarysearch_pos. @@ -198,10 +195,7 @@ end -- feel appropriate (higher runtime), most cases should be served well by this -- heuristic. local function non_linkable_node(node) - return vim.tbl_contains( - { types.textNode, types.functionNode }, - node.type - ) + return vim.tbl_contains({ types.textNode, types.functionNode }, node.type) end -- return whether a node is certainly (not) interactive. -- Coincindentially, the same nodes as (non-)linkable ones, but since there is a @@ -857,9 +851,11 @@ local function collect_dependents(node, which, static) end local function str_args(args) - return args and vim.tbl_map(function(arg) - return snippet_string.isinstance(arg) and vim.split(arg:str(), "\n") or arg - end, args) + return args + and vim.tbl_map(function(arg) + return snippet_string.isinstance(arg) and vim.split(arg:str(), "\n") + or arg + end, args) end local store_id = 0 @@ -891,7 +887,8 @@ local function store_cursor_node_relative(node) snip_data.mode = cursor_state.mode if cursor_state.pos_v then - snip_data.selection_end_start_relative = util.pos_offset(node.mark:get_endpoint(-1), cursor_state.pos_v) + snip_data.selection_end_start_relative = + util.pos_offset(node.mark:get_endpoint(-1), cursor_state.pos_v) end data[snip] = snip_data @@ -904,14 +901,30 @@ end local function restore_cursor_pos_relative(node, data) if data.mode == "i" then - feedkeys.insert_at(util.pos_from_offset(node.mark:get_endpoint(-1), data.cursor_start_relative)) + feedkeys.insert_at( + util.pos_from_offset( + node.mark:get_endpoint(-1), + data.cursor_start_relative + ) + ) elseif data.mode == "s" then -- is a selection => restore it. - local selection_from = util.pos_from_offset(node.mark:get_endpoint(-1), data.cursor_start_relative) - local selection_to = util.pos_from_offset(node.mark:get_endpoint(-1), data.selection_end_start_relative) + local selection_from = util.pos_from_offset( + node.mark:get_endpoint(-1), + data.cursor_start_relative + ) + local selection_to = util.pos_from_offset( + node.mark:get_endpoint(-1), + data.selection_end_start_relative + ) feedkeys.select_range(selection_from, selection_to) else - feedkeys.move_to_normal(util.pos_from_offset(node.mark:get_endpoint(-1), data.cursor_start_relative)) + feedkeys.move_to_normal( + util.pos_from_offset( + node.mark:get_endpoint(-1), + data.cursor_start_relative + ) + ) end end @@ -942,5 +955,5 @@ return { node_subtree_do = node_subtree_do, str_args = str_args, store_cursor_node_relative = store_cursor_node_relative, - restore_cursor_pos_relative = restore_cursor_pos_relative + restore_cursor_pos_relative = restore_cursor_pos_relative, } diff --git a/lua/luasnip/nodes/util/snippet_string.lua b/lua/luasnip/nodes/util/snippet_string.lua index bba59c7cf..d2f04d7a6 100644 --- a/lua/luasnip/nodes/util/snippet_string.lua +++ b/lua/luasnip/nodes/util/snippet_string.lua @@ -13,7 +13,7 @@ local M = {} ---@param initial_str string[]?, optional initial multiline string. ---@return SnippetString function M.new(initial_str) - local o = {initial_str and table.concat(initial_str, "\n")} + local o = { initial_str and table.concat(initial_str, "\n") } return setmetatable(o, SnippetString_mt) end @@ -22,7 +22,7 @@ function M.isinstance(o) end function SnippetString:append_snip(snip) - table.insert(self, {snip = snip}) + table.insert(self, { snip = snip }) end function SnippetString:append_text(str) table.insert(self, table.concat(str, "\n")) @@ -45,14 +45,19 @@ local function gen_snipstr_map(self, map, from_offset) pre = function(node) if node.static_text then if M.isinstance(node.static_text) then - local nested_str = gen_snipstr_map(node.static_text, map, from_offset + #str + #snip_str) + local nested_str = gen_snipstr_map( + node.static_text, + map, + from_offset + #str + #snip_str + ) snip_str = snip_str .. nested_str else - snip_str = snip_str .. table.concat(node.static_text, "\n") + snip_str = snip_str + .. table.concat(node.static_text, "\n") end end end, - post = util.nop + post = util.nop, }) map[v.snip] = snip_str str = str .. snip_str @@ -99,11 +104,11 @@ function SnippetString:iter_snippets() local i = 1 return function() -- find the next snippet. - while self[i] and (not self[i].snip) do - i = i+1 + while self[i] and not self[i].snip do + i = i + 1 end local res = self[i] and self[i].snip - i = i+1 + i = i + 1 return res end end @@ -121,46 +126,48 @@ end function SnippetString:copy() -- on 0.7 vim.deepcopy does not behave correctly => have to manually copy. - return setmetatable(vim.tbl_map(function(snipstr_or_str) - if snipstr_or_str.snip then - local snip = snipstr_or_str.snip - - -- remove associations with objects beyond this snippet. - -- This is so we can easily deepcopy it without copying too much data. - -- We could also do this copy in - local prevprev = snip.prev.prev - local i0next = snip.insert_nodes[0].next - local parentnode = snip.parent_node - - snip.prev.prev = nil - snip.insert_nodes[0].next = nil - snip.parent_node = nil - - local snipcop = snip:copy() - - snip.prev.prev = prevprev - snip.insert_nodes[0].next = i0next - snip.parent_node = parentnode - - - -- bring into inactive mode, so that we will jump into it correctly when it - -- is expanded again. - snipcop:subtree_do({ - pre = function(node) - node.mark:invalidate() - end, - post = util.nop, - do_child_snippets = true - }) - -- snippet may have been active (for example if captured as an - -- argnode), so finally exit here (so we can put_initial it again!) - snipcop:exit() - - return {snip = snipcop} - else - return snipstr_or_str - end - end, self), SnippetString_mt) + return setmetatable( + vim.tbl_map(function(snipstr_or_str) + if snipstr_or_str.snip then + local snip = snipstr_or_str.snip + + -- remove associations with objects beyond this snippet. + -- This is so we can easily deepcopy it without copying too much data. + -- We could also do this copy in + local prevprev = snip.prev.prev + local i0next = snip.insert_nodes[0].next + local parentnode = snip.parent_node + + snip.prev.prev = nil + snip.insert_nodes[0].next = nil + snip.parent_node = nil + + local snipcop = snip:copy() + + snip.prev.prev = prevprev + snip.insert_nodes[0].next = i0next + snip.parent_node = parentnode + + -- bring into inactive mode, so that we will jump into it correctly when it + -- is expanded again. + snipcop:subtree_do({ + pre = function(node) + node.mark:invalidate() + end, + post = util.nop, + do_child_snippets = true, + }) + -- snippet may have been active (for example if captured as an + -- argnode), so finally exit here (so we can put_initial it again!) + snipcop:exit() + + return { snip = snipcop } + else + return snipstr_or_str + end + end, self), + SnippetString_mt + ) end -- copy without copying snippets. @@ -175,7 +182,7 @@ end -- where o is string, string[] or SnippetString. local function to_snippetstring(o) if type(o) == "string" then - return M.new({o}) + return M.new({ o }) elseif getmetatable(o) == SnippetString_mt then return o else @@ -219,7 +226,7 @@ local function find(self, start_i, i_inc, char_i, snipstr_map) v_str = v end - local current_str_to = current_str_from + #v_str-1 + local current_str_to = current_str_from + #v_str - 1 if char_i >= current_str_from and char_i <= current_str_to then return i end @@ -237,7 +244,7 @@ local function nodetext_len(node, snipstr_map) return #snipstr_map[node.static_text].str else -- +1 for each newline. - local len = #node.static_text-1 + local len = #node.static_text - 1 for _, v in ipairs(node.static_text) do len = len + #v end @@ -253,7 +260,7 @@ local function _replace(self, replacements, snipstr_map) for i = #replacements, 1, -1 do local repl = replacements[i] - local v_i_to = find(self, v_i_search_from, -1 , repl.to, snipstr_map) + local v_i_to = find(self, v_i_search_from, -1, repl.to, snipstr_map) local v_i_from = find(self, v_i_to, -1, repl.from, snipstr_map) -- next range may begin in v_i_from, before the currently inserted @@ -275,10 +282,15 @@ local function _replace(self, replacements, snipstr_map) pre = function(node) local node_len = nodetext_len(node, snipstr_map) if node_len > 0 then - local node_relative_repl_from = repl.from - node_from+1 - local node_relative_repl_to = repl.to - node_from+1 - - if node_relative_repl_from >= 1 and node_relative_repl_from <= node_len then + local node_relative_repl_from = repl.from + - node_from + + 1 + local node_relative_repl_to = repl.to - node_from + 1 + + if + node_relative_repl_from >= 1 + and node_relative_repl_from <= node_len + then if node_relative_repl_to <= node_len then if M.isinstance(node.static_text) then -- node contains a snippetString, recurse! @@ -286,7 +298,11 @@ local function _replace(self, replacements, snipstr_map) -- snipstr_map, we don't even have to -- modify repl to be defined based on the -- other snippetString. (ie. shift from and to) - _replace(node.static_text, {repl}, snipstr_map) + _replace( + node.static_text, + { repl }, + snipstr_map + ) else -- simply manipulate the node-static-text -- manually. @@ -297,12 +313,24 @@ local function _replace(self, replacements, snipstr_map) -- the only data in snipstr_map we may -- access that is inaccurate), the queries -- will still be answered correctly. - local str = table.concat(node.static_text, "\n") + local str = + table.concat(node.static_text, "\n") node.static_text = vim.split( - str:sub(1, node_relative_repl_from-1) .. repl.str .. str:sub(node_relative_repl_to+1), "\n") + str:sub(1, node_relative_repl_from - 1) + .. repl.str + .. str:sub( + node_relative_repl_to + 1 + ), + "\n" + ) end -- update string in snipstr_map. - snipstr_map[snip] = snipstr_map[snip]:sub(1, repl.from - v_from_from-1) .. repl.str .. snipstr_map[snip]:sub(repl.to - v_to_from+1) + snipstr_map[snip] = snipstr_map[snip]:sub( + 1, + repl.from - v_from_from - 1 + ) .. repl.str .. snipstr_map[snip]:sub( + repl.to - v_to_from + 1 + ) error(true) else -- range begins in, but ends outside this node @@ -315,16 +343,21 @@ local function _replace(self, replacements, snipstr_map) node_from = node_from + node_len end end, - post = util.nop + post = util.nop, }) end -- in lieu of `continue`, we need this bool to check whether we did a replacement yet. if not repl_in_node then - local from_str = self[v_i_from].snip and snipstr_map[self[v_i_from].snip] or self[v_i_from] - local to_str = self[v_i_to].snip and snipstr_map[self[v_i_to].snip] or self[v_i_to] + local from_str = self[v_i_from].snip + and snipstr_map[self[v_i_from].snip] + or self[v_i_from] + local to_str = self[v_i_to].snip and snipstr_map[self[v_i_to].snip] + or self[v_i_to] -- +1 to get the char of to, +1 to start beyond it. - self[v_i_from] = from_str:sub(1, repl.from - v_from_from) .. repl.str .. to_str:sub(repl.to - v_to_from+1+1) + self[v_i_from] = from_str:sub(1, repl.from - v_from_from) + .. repl.str + .. to_str:sub(repl.to - v_to_from + 1 + 1) -- start-position of string has to be updated. snipstr_map[self][v_i_from] = v_from_from end @@ -351,7 +384,7 @@ local function upper(self) end end end, - post = util.nop + post = util.nop, }) else self[i] = v:upper() @@ -372,7 +405,7 @@ local function lower(self) end end end, - post = util.nop + post = util.nop, }) else self[i] = v:lower() @@ -416,7 +449,7 @@ function SnippetString:gsub(pattern, repl) table.insert(replacements, { from = match_from, to = match_to, - str = str:sub(match_from, match_to):gsub(pattern, repl) + str = str:sub(match_from, match_to):gsub(pattern, repl), }) end find_from = match_to + 1 @@ -444,7 +477,7 @@ function SnippetString:sub(from, to) -- empty range => return empty snippetString. if from > #str or to < from or to < 1 then - return M.new({""}) + return M.new({ "" }) end from = math.max(from, 1) @@ -453,16 +486,15 @@ function SnippetString:sub(from, to) local replacements = {} -- from <= 1 => don't need to remove from beginning. if from > 1 then - table.insert(replacements, { from=1, to=from-1, str = "" }) + table.insert(replacements, { from = 1, to = from - 1, str = "" }) end -- to >= #str => don't need to remove from end. if to < #str then - table.insert(replacements, { from=to+1, to=#str, str = "" }) + table.insert(replacements, { from = to + 1, to = #str, str = "" }) end _replace(self, replacements, snipstr_map) return self end - return M diff --git a/lua/luasnip/util/feedkeys.lua b/lua/luasnip/util/feedkeys.lua index 6ed6c1699..326fa7515 100644 --- a/lua/luasnip/util/feedkeys.lua +++ b/lua/luasnip/util/feedkeys.lua @@ -75,7 +75,8 @@ end local function cursor_set_keys(pos, before) if before then if pos[2] == 0 then - local prev_line_str = vim.api.nvim_buf_get_lines(0, pos[1]-1, pos[1], false)[1] + local prev_line_str = + vim.api.nvim_buf_get_lines(0, pos[1] - 1, pos[1], false)[1] if prev_line_str then -- set onto last column of previous line, if possible. pos[1] = pos[1] - 1 @@ -99,7 +100,8 @@ end function M.select_range(b, e) local id = next_id() - enqueued_cursor_state = {pos = vim.deepcopy(b), pos_v = vim.deepcopy(e), mode = "s", id = id} + enqueued_cursor_state = + { pos = vim.deepcopy(b), pos_v = vim.deepcopy(e), mode = "s", id = id } enqueue_action(function() -- stylua: ignore _feedkeys_insert(id, @@ -132,7 +134,7 @@ end -- move the cursor to a position and enter insert-mode (or stay in it). function M.insert_at(pos) local id = next_id() - enqueued_cursor_state = {pos = pos, mode = "i", id = id} + enqueued_cursor_state = { pos = pos, mode = "i", id = id } enqueue_action(function() -- if current and target mode is INSERT, there's no reason to leave it. @@ -155,10 +157,10 @@ end function M.move_to_normal(pos) local id = next_id() -- preserve mode. - enqueued_cursor_state = {pos = pos, mode = "n", id = id} + enqueued_cursor_state = { pos = pos, mode = "n", id = id } enqueue_action(function() - if vim.fn.mode():sub(1,1) == "n" then + if vim.fn.mode():sub(1, 1) == "n" then util.set_cursor_0ind(pos) M.confirm(id) else @@ -194,16 +196,16 @@ function M.last_state() local state = {} local getposdot = vim.fn.getpos(".") - state.pos = {getposdot[2]-1, getposdot[3]-1} + state.pos = { getposdot[2] - 1, getposdot[3] - 1 } local getposv = vim.fn.getpos("v") -- store selection-range with end-position one column after the cursor -- at the end (so -1 to make getpos-position 0-based, +1 to move it one -- beyond the last character of the range) - state.pos_v = {getposv[2]-1, getposv[3]} + state.pos_v = { getposv[2] - 1, getposv[3] } -- only store first component. - state.mode = vim.fn.mode():sub(1,1) + state.mode = vim.fn.mode():sub(1, 1) return state end diff --git a/lua/luasnip/util/str.lua b/lua/luasnip/util/str.lua index 628da7a7d..ef44c386c 100644 --- a/lua/luasnip/util/str.lua +++ b/lua/luasnip/util/str.lua @@ -122,7 +122,7 @@ function M.multiline_substr(str, from, to) -- include all rows for i = from[1], to[1] do - table.insert(res, str[i+1]) + table.insert(res, str[i + 1]) end -- trim text before from and after to. @@ -130,7 +130,7 @@ function M.multiline_substr(str, from, to) -- on the same line. If res[1] was trimmed first, we'd have to adjust the -- trim-point of `to`. res[#res] = res[#res]:sub(1, to[2]) - res[1] = res[1]:sub(from[2]+1) + res[1] = res[1]:sub(from[2] + 1) return res end diff --git a/lua/luasnip/util/util.lua b/lua/luasnip/util/util.lua index 90010efa9..cfd2db910 100644 --- a/lua/luasnip/util/util.lua +++ b/lua/luasnip/util/util.lua @@ -394,7 +394,7 @@ end -- Assumption: `pos` occurs after `base_pos`. local function pos_offset(base_pos, pos) local row_offset = pos[1] - base_pos[1] - return {row_offset, row_offset == 0 and pos[2] - base_pos[2] or pos[2]} + return { row_offset, row_offset == 0 and pos[2] - base_pos[2] or pos[2] } end -- compute offset of `pos` into multiline string starting at `base_pos`. @@ -402,7 +402,10 @@ end -- when `pos` is on a line different from `base_pos`. -- Assumption: `pos` occurs after `base_pos`. local function pos_from_offset(base_pos, offset) - return {base_pos[1]+offset[1], offset[1] == 0 and base_pos[2] + offset[2] or offset[2]} + return { + base_pos[1] + offset[1], + offset[1] == 0 and base_pos[2] + offset[2] or offset[2], + } end local function shallow_copy(t) @@ -458,5 +461,5 @@ return { pos_cmp = pos_cmp, pos_offset = pos_offset, pos_from_offset = pos_from_offset, - shallow_copy = shallow_copy + shallow_copy = shallow_copy, } diff --git a/tests/integration/choice_spec.lua b/tests/integration/choice_spec.lua index c027bce02..307a3c4c9 100644 --- a/tests/integration/choice_spec.lua +++ b/tests/integration/choice_spec.lua @@ -311,7 +311,9 @@ describe("ChoiceNode", function() end) it("correctly gives current content of choices.", function() - assert.are.same({"${1:asdf}", "qwer"}, exec_lua[[ + assert.are.same( + { "${1:asdf}", "qwer" }, + exec_lua([[ ls.snip_expand(s("trig", { c(1, { i(1, "asdf"), @@ -321,10 +323,13 @@ describe("ChoiceNode", function() ls.change_choice() return ls.get_current_choices() ]]) + ) end) it("correctly restores the generated node of a dynamicNode.", function() - assert.are.same({ "${1:${${1:aaa}${2:${1:aaa}}}}$0" }, exec_lua[[ + assert.are.same( + { "${1:${${1:aaa}${2:${1:aaa}}}}$0" }, + exec_lua([[ snip = s("trig", { c(1, { r(nil, "restore_key", { @@ -339,24 +344,25 @@ describe("ChoiceNode", function() }) return snip:get_docstring() ]]) + ) exec_lua("ls.snip_expand(snip)") feed("qwer") exec_lua("ls.jump(1)") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ qwer^q{3:wer} | {0:~ }| {2:-- SELECT --} | - ]] -}) - exec_lua("ls.change_choice(1)") -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.change_choice(1)") + screen:expect({ + grid = [[ a^q{3:wer}qwera | {0:~ }| {2:-- SELECT --} | - ]] -}) + ]], + }) end) it("cursor is correctly restored after change", function() @@ -374,7 +380,7 @@ screen:expect({ [3] = { background = Screen.colors.LightGray }, }) - exec_lua[=[ + exec_lua([=[ ls.snip_expand(s("trig", { c(1, { fmt([[ @@ -389,13 +395,13 @@ screen:expect({ ]], {r(1, "name", i(1, "fname")), r(2, "body", i(1, "fbody"))}) }, {restore_cursor = true}) })) - ]=] + ]=]) exec_lua("vim.wait(10, function() end)") - exec_lua"ls.jump(1)" + exec_lua("ls.jump(1)") feed("asdfasdfqweraaaa") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ local fname = function() | aaaa | bbbbasdf | @@ -403,11 +409,11 @@ screen:expect({ qwer | aa^aa | {2:-- INSERT --} | - ]] -}) - exec_lua"ls.change_choice(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.change_choice(1)") + screen:expect({ + grid = [[ local function fname() | asdf | asdf | @@ -415,12 +421,12 @@ screen:expect({ aa^aa | end | {2:-- INSERT --} | - ]] -}) - exec_lua"ls.jump(-1)" - exec_lua"ls.jump(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.jump(-1)") + exec_lua("ls.jump(1)") + screen:expect({ + grid = [[ local function fname() | ^a{3:sdf} | {3:asdf} | @@ -428,11 +434,11 @@ screen:expect({ {3: aaaa} | end | {2:-- SELECT --} | - ]] -}) - exec_lua"ls.change_choice(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.change_choice(1)") + screen:expect({ + grid = [[ aaaa | bbbb^a{3:sdf} | {3:asdf} | @@ -440,11 +446,11 @@ screen:expect({ {3: aaaa} | end | {2:-- SELECT --} | - ]] -}) + ]], + }) feed("i") - exec_lua"ls.change_choice(1)" - exec_lua[=[ + exec_lua("ls.change_choice(1)") + exec_lua([=[ ls.snip_expand(s("for", { t"for ", c(1, { sn(nil, {i(1, "k"), t", ", i(2, "v"), t" in ", c(3, {{t"pairs(",i(1),t")"}, {t"ipairs(",i(1),t")"}, i(nil)}, {restore_cursor = true}) }), @@ -453,9 +459,9 @@ screen:expect({ fmt([[{} in vim.gsplit({})]], {i(1, "str"), i(2)}) }, {restore_cursor = true}), t{" do", "\t"}, isn(2, {dl(1, l.LS_SELECT_DEDENT)}, "$PARENT_INDENT\t"), t{"", "end"} })) - ]=] -screen:expect({ - grid = [[ + ]=]) + screen:expect({ + grid = [[ local function fname() | for ^k, v in pairs() do | | @@ -463,11 +469,11 @@ screen:expect({ end | {0:~ }| {2:-- SELECT --} | - ]] -}) - exec_lua"ls.change_choice(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.change_choice(1)") + screen:expect({ + grid = [[ local function fname() | for ^v{3:al} in do | | @@ -475,12 +481,12 @@ screen:expect({ end | {0:~ }| {2:-- SELECT --} | - ]] -}) - exec_lua"ls.jump(1)" - exec_lua"ls.jump(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.jump(1)") + exec_lua("ls.jump(1)") + screen:expect({ + grid = [[ local function fname() | for val in do | ^ | @@ -488,11 +494,11 @@ screen:expect({ end | {0:~ }| {2:-- INSERT --} | - ]] -}) - exec_lua"ls.change_choice(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.change_choice(1)") + screen:expect({ + grid = [[ local fname = function() | aaaa | bbbbfor val in do | @@ -500,12 +506,12 @@ screen:expect({ endi | end | {2:-- INSERT --} | - ]] -}) + ]], + }) end) it("select_choice works.", function() - exec_lua[=[ + exec_lua([=[ ls.snip_expand(s("for", { t"for ", c(1, { sn(nil, {i(1, "k"), t", ", i(2, "v"), t" in ", c(3, {{t"pairs(",i(1),t")"}, {t"ipairs(",i(1),t")"}, i(nil)}, {restore_cursor = true}) }), @@ -514,23 +520,23 @@ screen:expect({ fmt([[{} in vim.gsplit({})]], {i(1, "str"), i(2)}) }, {restore_cursor = true}), t{" do", "\t"}, isn(2, {dl(1, l.LS_SELECT_DEDENT)}, "$PARENT_INDENT\t"), t{"", "end"} })) - ]=] + ]=]) feed("lua require('luasnip.extras.select_choice')()2") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ for ^v{3:al} in do | | {2:-- SELECT --} | - ]] -}) + ]], + }) -- re-selecting correctly highlights text again. feed("lua require('luasnip.extras.select_choice')()2") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ for ^v{3:al} in do | | {2:-- SELECT --} | - ]] -}) + ]], + }) end) end) diff --git a/tests/integration/dynamic_spec.lua b/tests/integration/dynamic_spec.lua index afd2d3282..afa2742bd 100644 --- a/tests/integration/dynamic_spec.lua +++ b/tests/integration/dynamic_spec.lua @@ -383,26 +383,29 @@ describe("DynamicNode", function() end, {opt(k("ins"))}) })) ]]) -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ ^e{3:sdf} | {0:~ }| {2:-- SELECT --} | - ]] -}) + ]], + }) feed("aaaaa") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ eeeee^ | {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) end) - it("selected text is selected again after updating (when possible).", function() - - assert.are.same({"${1:${1:esdf}}$0"}, exec_lua[[ + it( + "selected text is selected again after updating (when possible).", + function() + assert.are.same( + { "${1:${1:esdf}}$0" }, + exec_lua([[ snip = s("trig", { d(1, function(args) if not args[1] then @@ -414,17 +417,19 @@ screen:expect({ }) return snip:get_docstring() ]]) - exec_lua[[ + ) + exec_lua([[ ls.snip_expand(snip) - ]] - feed("a") - exec_lua("ls.lsp_expand('${1:asdf}')") -screen:expect({ - grid = [[ + ]]) + feed("a") + exec_lua("ls.lsp_expand('${1:asdf}')") + screen:expect({ + grid = [[ e^e{3:sdf}sdf | {0:~ }| {2:-- SELECT --} | - ]] -}) - end) + ]], + }) + end + ) end) diff --git a/tests/integration/restore_spec.lua b/tests/integration/restore_spec.lua index 9a77cd5cf..69ced3fb9 100644 --- a/tests/integration/restore_spec.lua +++ b/tests/integration/restore_spec.lua @@ -367,25 +367,24 @@ describe("RestoreNode", function() feed(". .") exec_lua("ls.lsp_expand('($1)')") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ a: . (^) . | {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) exec_lua("ls.change_choice(1)") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ b: . (^) . | {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) end) it("correctly restores snippets (2).", function() - exec_lua([[ ls.setup({link_children = true}) ls.snip_expand(s("trig", { @@ -397,32 +396,31 @@ screen:expect({ end, {1}) })) ]]) - exec_lua[[ls.jump(1)]] + exec_lua([[ls.jump(1)]]) feed(". .") exec_lua("ls.lsp_expand('($1)')") feed("i") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ asdf . (i^) .asdf | {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) exec_lua("ls.jump(-1) ls.jump(-1)") feed("qwer") exec_lua("ls.jump(1) ls.jump(1)") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ qwer . (^i) .qwer | {0:~ }| {2:-- SELECT --} | - ]] -}) + ]], + }) end) -- make sure store and update_restore propagate. it("correctly restores snippets (3).", function() - exec_lua([[ ls.setup({link_children = true}) ls.snip_expand(s("trig", { @@ -434,7 +432,7 @@ screen:expect({ end, {1}) })) ]]) - exec_lua[[ls.jump(1)]] + exec_lua([[ls.jump(1)]]) feed(". .") exec_lua([[ ls.snip_expand(s("trig", { @@ -442,28 +440,27 @@ screen:expect({ })) ]]) feed("i") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ asdf . (i^) .asdf | {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) exec_lua("ls.jump(-1) ls.jump(-1)") feed("qwer") exec_lua("ls.jump(1) ls.jump(1)") -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ qwer . (^i) .qwer | {0:~ }| {2:-- SELECT --} | - ]] -}) + ]], + }) end) -- make sure store and update_restore propagate. it("correctly restores snippets (3).", function() - exec_lua([[ ls.setup({link_children = true}) ls.snip_expand(s("trig", { @@ -475,7 +472,7 @@ screen:expect({ end, {1}) })) ]]) - exec_lua[[ls.jump(1)]] + exec_lua([[ls.jump(1)]]) local function exp() exec_lua([[ @@ -487,66 +484,66 @@ screen:expect({ end exp() - exec_lua"ls.jump(1)" + exec_lua("ls.jump(1)") exp() - exec_lua"ls.jump(1)" + exec_lua("ls.jump(1)") exp() feed("i") exp() exp() exp() -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ asdf (i)(i)(i (i(i(i^))) i)asdf | {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) -- 11x to get back to the i1. - exec_lua"ls.jump(-1) ls.jump(-1) ls.jump(-1)" - exec_lua"ls.jump(-1) ls.jump(-1) ls.jump(-1)" - exec_lua"ls.jump(-1) ls.jump(-1) ls.jump(-1)" - exec_lua"ls.jump(-1) ls.jump(-1)" + exec_lua("ls.jump(-1) ls.jump(-1) ls.jump(-1)") + exec_lua("ls.jump(-1) ls.jump(-1) ls.jump(-1)") + exec_lua("ls.jump(-1) ls.jump(-1) ls.jump(-1)") + exec_lua("ls.jump(-1) ls.jump(-1)") feed("qwer") - exec_lua"ls.jump(1)" -screen:expect({ - grid = [[ + exec_lua("ls.jump(1)") + screen:expect({ + grid = [[ qwer ^({3:i)(i)(i (i(i(i))) i)}qwer | {0:~ }| {2:-- SELECT --} | - ]] -}) - exec_lua"ls.jump(1) ls.jump(1) ls.jump(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.jump(1) ls.jump(1) ls.jump(1)") + screen:expect({ + grid = [[ qwer (i)(^i)(i (i(i(i))) i)qwer | {0:~ }| {2:-- SELECT --} | - ]] -}) - exec_lua"ls.jump(1) ls.jump(1) ls.jump(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.jump(1) ls.jump(1) ls.jump(1)") + screen:expect({ + grid = [[ qwer (i)(i)(i (^i{3:(i(i))}) i)qwer | {0:~ }| {2:-- SELECT --} | - ]] -}) - exec_lua"ls.jump(1) ls.jump(1) ls.jump(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.jump(1) ls.jump(1) ls.jump(1)") + screen:expect({ + grid = [[ qwer (i)(i)(i (i(i(i)^)) i)qwer | {0:~ }| {2:-- INSERT --} | - ]] -}) - exec_lua"ls.jump(1) ls.jump(1) ls.jump(1) ls.jump(1)" -screen:expect({ - grid = [[ + ]], + }) + exec_lua("ls.jump(1) ls.jump(1) ls.jump(1) ls.jump(1)") + screen:expect({ + grid = [[ qwer (i)(i)(i (i(i(i))) i)^q{3:wer} | {0:~ }| {2:-- SELECT --} | - ]] -}) + ]], + }) end) end) diff --git a/tests/integration/session_spec.lua b/tests/integration/session_spec.lua index f99fc684b..dac58df96 100644 --- a/tests/integration/session_spec.lua +++ b/tests/integration/session_spec.lua @@ -386,11 +386,11 @@ describe("session", function() -- this fail only here specifically (IIRC there are enough tests that -- do something similar)), and since it's fine on 0.9 and master (which -- matter much more) there shouldn't be an issue in practice. - exec_lua[[ + exec_lua([[ if require("luasnip.util.vimversion").ge(0,8,0) then ls.jump(1) end - ]] + ]]) end) it("Deleting nested snippet only removes it.", function() feed("ofn") @@ -2294,8 +2294,8 @@ describe("session", function() change(1) change(1) -- currently wrong! -screen:expect({ - grid = [[ + screen:expect({ + grid = [[ /** | * A short Description | */ | @@ -2326,7 +2326,7 @@ screen:expect({ {0:~ }| {0:~ }| {2:-- INSERT --} | - ]] -}) + ]], + }) end) end) diff --git a/tests/unit/str_spec.lua b/tests/unit/str_spec.lua index 5f90aa1db..ae8405134 100644 --- a/tests/unit/str_spec.lua +++ b/tests/unit/str_spec.lua @@ -68,16 +68,48 @@ describe("str.multiline_substr", function() local function check(dscr, str, from, to, expected) it(dscr, function() - assert.are.same(expected, exec_lua([[ + assert.are.same( + expected, + exec_lua( + [[ local str, from, to = ... return require("luasnip.util.str").multiline_substr(str, from, to) - ]], str, from, to)) + ]], + str, + from, + to + ) + ) end) end - check("entire range", {"asdf", "qwer"}, {0,0}, {1,4}, {"asdf", "qwer"}) - check("partial range", {"asdf", "qwer"}, {0,3}, {1,2}, {"f", "qw"}) - check("another partial range", {"asdf", "qwer"}, {1,2}, {1,3}, {"e"}) - check("one last partial range", {"asdf", "qwer", "zxcv"}, {0,2}, {2,4}, {"df", "qwer", "zxcv"}) - check("empty range", {"asdf", "qwer", "zxcv"}, {0,2}, {0,2}, {""}) + check( + "entire range", + { "asdf", "qwer" }, + { 0, 0 }, + { 1, 4 }, + { "asdf", "qwer" } + ) + check( + "partial range", + { "asdf", "qwer" }, + { 0, 3 }, + { 1, 2 }, + { "f", "qw" } + ) + check( + "another partial range", + { "asdf", "qwer" }, + { 1, 2 }, + { 1, 3 }, + { "e" } + ) + check( + "one last partial range", + { "asdf", "qwer", "zxcv" }, + { 0, 2 }, + { 2, 4 }, + { "df", "qwer", "zxcv" } + ) + check("empty range", { "asdf", "qwer", "zxcv" }, { 0, 2 }, { 0, 2 }, { "" }) end)