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/init.lua b/lua/luasnip/init.lua index 260cb28a3..66855c2f6 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 @@ -175,7 +174,8 @@ local function store_cursor_node_relative(node) util.pos_sub(cursor_state.pos, node.mark:get_endpoint(1)) if cursor_state.pos_end then - snip_data.selection_other_end_end_relative = util.pos_sub(cursor_state.pos_end, node.mark:get_endpoint(1)) + snip_data.selection_other_end_end_relative = + util.pos_sub(cursor_state.pos_end, node.mark:get_endpoint(1)) end data[snip] = snip_data @@ -190,14 +190,18 @@ 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 restore_cursor_pos_relative(node, data) if data.selection_other_end_end_relative then -- is a selection => restore it. - local selection_from = util.pos_add(node.mark:get_endpoint(1), data.cursor_end_relative) - local selection_to = util.pos_add(node.mark:get_endpoint(1), data.selection_other_end_end_relative) + local selection_from = + util.pos_add(node.mark:get_endpoint(1), data.cursor_end_relative) + local selection_to = util.pos_add( + node.mark:get_endpoint(1), + data.selection_other_end_end_relative + ) feedkeys.select_range(selection_from, selection_to) else util.set_cursor_0ind( @@ -303,7 +307,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() @@ -1034,7 +1039,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 68377f295..a07663554 100644 --- a/lua/luasnip/nodes/choiceNode.lua +++ b/lua/luasnip/nodes/choiceNode.lua @@ -278,7 +278,7 @@ function ChoiceNode:set_choice(choice, current_node) 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 diff --git a/lua/luasnip/nodes/dynamicNode.lua b/lua/luasnip/nodes/dynamicNode.lua index 9834bf4c2..d618270bb 100644 --- a/lua/luasnip/nodes/dynamicNode.lua +++ b/lua/luasnip/nodes/dynamicNode.lua @@ -166,7 +166,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 @@ -268,8 +273,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 @@ -321,7 +331,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() @@ -356,7 +370,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 00252a293..94027bd01 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 @@ -400,12 +423,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/util.lua b/lua/luasnip/nodes/util.lua index dd30020bc..9d93263d3 100644 --- a/lua/luasnip/nodes/util.lua +++ b/lua/luasnip/nodes/util.lua @@ -857,9 +857,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 return { @@ -887,5 +889,5 @@ return { find_node_dependents = find_node_dependents, collect_dependents = collect_dependents, node_subtree_do = node_subtree_do, - str_args = str_args + str_args = str_args, } 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 022c4d45e..8b92b5770 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_end = vim.deepcopy(e), id = id} + enqueued_cursor_state = + { pos = vim.deepcopy(b), pos_end = vim.deepcopy(e), 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, id = id} + enqueued_cursor_state = { pos = pos, id = id } enqueue_action(function() -- if current and target mode is INSERT, there's no reason to leave it. @@ -177,7 +179,7 @@ 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 } -- only re-enter select for now. if vim.fn.mode() == "s" then @@ -185,7 +187,7 @@ function M.last_state() -- 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_end = {getposv[2]-1, getposv[3]+1} + state.pos_end = { getposv[2] - 1, getposv[3] + 1 } end return state 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 b4a58ebcc..6b90131a5 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 local function shallow_copy(t) @@ -449,5 +449,5 @@ return { ternary = ternary, pos_cmp = pos_cmp, pos_offset = pos_offset, - shallow_copy = shallow_copy + shallow_copy = shallow_copy, } diff --git a/tests/integration/dynamic_spec.lua b/tests/integration/dynamic_spec.lua index 45d4125a6..9ec50938e 100644 --- a/tests/integration/dynamic_spec.lua +++ b/tests/integration/dynamic_spec.lua @@ -383,25 +383,27 @@ 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() - exec_lua[[ + it( + "selected text is selected again after updating (when possible).", + function() + exec_lua([[ ls.snip_expand(s("trig", { d(1, function(args) if not args[1] then @@ -411,15 +413,16 @@ screen:expect({ end end, {opt(k("ins"))}, {snippetstring_args = true}) })) - ]] - 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 acb84bcec..34443e311 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") 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)