From 12426d51b229aef854a402c011ced8045377472f Mon Sep 17 00:00:00 2001 From: L3MON4D3 Date: Sat, 15 Jul 2023 20:18:54 +0000 Subject: [PATCH] Format with stylua --- lua/luasnip/init.lua | 9 ++++-- lua/luasnip/nodes/snippet.lua | 59 +++++++++++++++++++++++++++-------- lua/luasnip/nodes/util.lua | 46 ++++++++++++++++++--------- lua/luasnip/session/init.lua | 4 +-- lua/luasnip/util/util.lua | 2 +- 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/lua/luasnip/init.lua b/lua/luasnip/init.lua index 65b1ae62f..8efe176fd 100644 --- a/lua/luasnip/init.lua +++ b/lua/luasnip/init.lua @@ -245,7 +245,8 @@ local function snip_expand(snippet, opts) session.current_nodes[vim.api.nvim_get_current_buf()] = opts.jump_into_func(snip) - local buf_snippet_roots = session.snippet_roots[vim.api.nvim_get_current_buf()] + local buf_snippet_roots = + session.snippet_roots[vim.api.nvim_get_current_buf()] if not session.config.history and #buf_snippet_roots > 1 then -- if history is not set, and there is more than one snippet-root, -- remove the other one. @@ -607,7 +608,11 @@ local function exit_out_of_region(node) pcall(snippet.mark.pos_begin_end, snippet.mark) if not ok then - remove_snip_set_adjacent_as_current(snippet, "Error while getting extmark-position: %s", snip_begin_pos) + remove_snip_set_adjacent_as_current( + snippet, + "Error while getting extmark-position: %s", + snip_begin_pos + ) end -- stylua: ignore diff --git a/lua/luasnip/nodes/snippet.lua b/lua/luasnip/nodes/snippet.lua index 3df3d3ac3..e6005f064 100644 --- a/lua/luasnip/nodes/snippet.lua +++ b/lua/luasnip/nodes/snippet.lua @@ -319,7 +319,7 @@ local function _S(snip, nodes, opts) -- list of snippets expanded within the region of this snippet. -- sorted by their buffer-position, for quick searching. - child_snippets = {} + child_snippets = {}, }), opts ) @@ -446,14 +446,19 @@ extend_decorator.register(ISN, { arg_indx = 4 }) -- * the index this snippet would be at if inserted into that list local function find_snippettree_position(pos) local prev_parent = nil - local prev_parent_children = session.snippet_roots[vim.api.nvim_get_current_buf()] + local prev_parent_children = + session.snippet_roots[vim.api.nvim_get_current_buf()] while true do -- `false`: if pos is on the boundary of a snippet, consider it as -- outside the snippet (in other words, prefer shifting the snippet to -- continuing the search inside it.) - local found_parent, child_indx = node_util.binarysearch_pos(prev_parent_children, pos, false) - if found_parent == false or (found_parent ~= nil and not found_parent:extmarks_valid()) then + local found_parent, child_indx = + node_util.binarysearch_pos(prev_parent_children, pos, false) + if + found_parent == false + or (found_parent ~= nil and not found_parent:extmarks_valid()) + then -- error while running procedure, or found snippet damaged (the -- idea to sidestep the damaged snippet, even if no error occurred -- _right now_, is to ensure that we can input_enter all the nodes @@ -482,7 +487,9 @@ function Snippet:remove_from_jumplist() self:exit() - local sibling_list = self.parent_node ~= nil and self.parent_node.parent.snippet.child_snippets or session.snippet_roots[vim.api.nvim_get_current_buf()] + local sibling_list = self.parent_node ~= nil + and self.parent_node.parent.snippet.child_snippets + or session.snippet_roots[vim.api.nvim_get_current_buf()] local self_indx for i, snip in ipairs(sibling_list) do if snip == self then @@ -518,8 +525,15 @@ function Snippet:remove_from_jumplist() end end -local function insert_into_jumplist(snippet, start_node, current_node, parent_node, sibling_snippets, own_indx) - local prev_snippet = sibling_snippets[own_indx-1] +local function insert_into_jumplist( + snippet, + start_node, + current_node, + parent_node, + sibling_snippets, + own_indx +) + local prev_snippet = sibling_snippets[own_indx - 1] -- have not yet inserted self!! local next_snippet = sibling_snippets[own_indx] @@ -706,7 +720,14 @@ function Snippet:trigger_expand(current_node, pos_id, env) -- parent_node is nil if the snippet is toplevel. self.parent_node = parent_node - insert_into_jumplist(self, start_node, current_node, parent_node, sibling_snippets, own_indx) + insert_into_jumplist( + self, + start_node, + current_node, + parent_node, + sibling_snippets, + own_indx + ) return parent_node end @@ -1336,10 +1357,16 @@ end -- pos-column has to be a byte-index, not a display-column. function Snippet:smallest_node_at(pos) local self_from, self_to = self.mark:pos_begin_end_raw() - assert(util.pos_cmp(self_from, pos) <= 0 and util.pos_cmp(pos, self_to) <= 0, "pos is not inside the snippet.") + assert( + util.pos_cmp(self_from, pos) <= 0 and util.pos_cmp(pos, self_to) <= 0, + "pos is not inside the snippet." + ) local smallest_node = self:node_at(pos) - assert(smallest_node ~= nil, "could not find a smallest node (very unexpected)") + assert( + smallest_node ~= nil, + "could not find a smallest node (very unexpected)" + ) return smallest_node end @@ -1372,7 +1399,8 @@ end function Snippet:extmarks_valid() -- assumption: extmarks are contiguous, and all can be queried via pos_begin_end_raw. - local ok, current_from, self_to = pcall(self.mark.pos_begin_end_raw, self.mark) + local ok, current_from, self_to = + pcall(self.mark.pos_begin_end_raw, self.mark) if not ok then return false end @@ -1383,12 +1411,17 @@ function Snippet:extmarks_valid() end for _, node in ipairs(self.nodes) do - local ok_, node_from, node_to = pcall(node.mark.pos_begin_end_raw, node.mark) + local ok_, node_from, node_to = + pcall(node.mark.pos_begin_end_raw, node.mark) -- this snippet is invalid if: -- - we can't get the position of some node -- - the positions aren't contiguous or don't completely fill the parent, or -- - any child of this node violates these rules. - if not ok_ or util.pos_cmp(current_from, node_from) ~= 0 or not node:extmarks_valid() then + if + not ok_ + or util.pos_cmp(current_from, node_from) ~= 0 + or not node:extmarks_valid() + then return false end current_from = node_to diff --git a/lua/luasnip/nodes/util.lua b/lua/luasnip/nodes/util.lua index eec74ed29..f9b49e0a7 100644 --- a/lua/luasnip/nodes/util.lua +++ b/lua/luasnip/nodes/util.lua @@ -101,7 +101,7 @@ local function leave_nodes_between(parent, child, no_move) -- entirely (because we stop at nodes[2], and handle nodes[1] -- separately) nodes[i]:input_leave(no_move) - nodes[i-1]:input_leave_children() + nodes[i - 1]:input_leave_children() end nodes[1]:input_leave(no_move) end @@ -112,7 +112,7 @@ local function enter_nodes_between(parent, child, no_move) return end - for i = 1, #nodes-1 do + for i = 1, #nodes - 1 do -- only enter children for nodes before the last (lowest) one. nodes[i]:input_enter(no_move) nodes[i]:input_enter_children() @@ -177,7 +177,10 @@ end local function linkable_node(node) -- node.type has to be one of insertNode, snippetNode, exitNode. - return vim.tbl_contains({types.insertNode, types.snippetNode, types.exitNode}, rawget(node, "type")) + return vim.tbl_contains( + { types.insertNode, types.snippetNode, types.exitNode }, + rawget(node, "type") + ) end local cmp_functions = { @@ -192,7 +195,7 @@ local cmp_functions = { end, boundary_outside_greater = function(pos, range_to) return util.pos_cmp(pos, range_to) >= 0 - end + end, } -- `nodes` is a list of nodes ordered by their occurrence in the buffer. -- `pos` is a row-column-tuble, byte-columns, and we return the node the LEFT @@ -210,7 +213,7 @@ local cmp_functions = { -- * if it is false, pos has to be fully inside a node to be considered inside -- it. If pos is on the left endpoint, it is considered to be left of the -- node, and likewise for the right endpoint. --- +-- -- This differentiation is useful for making this function more general: -- When searching in the contiguous nodes of a snippet, we'd like this routine -- to return any of them (obviously the one pos is inside/or on the border of), @@ -238,7 +241,7 @@ local function binarysearch_pos(nodes, pos, respect_rgravs) return nil, 1 end while true do - local mid = left + math.floor((right-left)/2) + local mid = left + math.floor((right - left) / 2) local mid_mark = nodes[mid].mark local ok, mid_from, mid_to = pcall(mid_mark.pos_begin_end_raw, mid_mark) @@ -293,7 +296,7 @@ local function first_common_node(a, b) local i = 0 local last_common = a.parent.snippet -- invariant: last_common is parent of both a and b. - while (a_pos[i+1] ~= nil) and a_pos[i + 1] == b_pos[i + 1] do + while (a_pos[i + 1] ~= nil) and a_pos[i + 1] == b_pos[i + 1] do last_common = last_common:resolve_position(a_pos[i + 1]) i = i + 1 end @@ -406,7 +409,11 @@ local function refocus(from, to) end -- pass nil if from/to is nil. -- if either is nil, first_common_node is nil, and the corresponding list empty. - local first_common_snippet, from_snip_path, to_snip_path = first_common_snippet_ancestor_path(from and from.parent.snippet, to and to.parent.snippet) + local first_common_snippet, from_snip_path, to_snip_path = + first_common_snippet_ancestor_path( + from and from.parent.snippet, + to and to.parent.snippet + ) -- we want leave/enter_path to be s.t. leaving/entering all nodes between -- each entry and its snippet (and the snippet itself) will leave/enter all @@ -445,7 +452,8 @@ local function refocus(from, to) if #from_snip_path > 0 then -- we know that the first node is from. local ok1 = pcall(leave_nodes_between, from.parent.snippet, from, true) - local ok2 = pcall(from.parent.snippet.input_leave, from.parent.snippet, true) + local ok2 = + pcall(from.parent.snippet.input_leave, from.parent.snippet, true) if not ok1 or not ok2 then from.parent.snippet:remove_from_jumplist() end @@ -454,7 +462,8 @@ local function refocus(from, to) local node = from_snip_path[i] local ok1 = pcall(node.input_leave_children, node) local ok2 = pcall(leave_nodes_between, node.parent.snippet, node, true) - local ok3 = pcall(node.parent.snippet.input_leave, node.parent.snippet, true) + local ok3 = + pcall(node.parent.snippet.input_leave, node.parent.snippet, true) if not ok1 or not ok2 or not ok3 then from.parent.snippet:remove_from_jumplist() end @@ -493,10 +502,17 @@ local function generic_extmarks_valid(node, child) -- valid if -- - extmark-extents match. -- - current choice is valid - local ok1, self_from, self_to = pcall(node.mark.pos_begin_end_raw, node.mark) - local ok2, child_from, child_to = pcall(child.mark.pos_begin_end_raw, child.mark) - - if not ok1 or not ok2 or util.pos_cmp(self_from, child_from) ~= 0 or util.pos_cmp(self_to, child_to) ~= 0 then + local ok1, self_from, self_to = + pcall(node.mark.pos_begin_end_raw, node.mark) + local ok2, child_from, child_to = + pcall(child.mark.pos_begin_end_raw, child.mark) + + if + not ok1 + or not ok2 + or util.pos_cmp(self_from, child_from) ~= 0 + or util.pos_cmp(self_to, child_to) ~= 0 + then return false end return child:extmarks_valid() @@ -518,5 +534,5 @@ return { linkable_node = linkable_node, binarysearch_pos = binarysearch_pos, refocus = refocus, - generic_extmarks_valid = generic_extmarks_valid + generic_extmarks_valid = generic_extmarks_valid, } diff --git a/lua/luasnip/session/init.lua b/lua/luasnip/session/init.lua index 77447c3ce..52b3b2fbf 100644 --- a/lua/luasnip/session/init.lua +++ b/lua/luasnip/session/init.lua @@ -17,11 +17,11 @@ M.current_nodes = {} -- snippet_roots[n] => list of snippet-roots in buffer n. M.snippet_roots = setmetatable({}, { -- create missing lists automatically. - __index = function(t,k) + __index = function(t, k) local new_t = {} rawset(t, k, new_t) return new_t - end + end, }) M.ns_id = vim.api.nvim_create_namespace("Luasnip") M.active_choice_nodes = {} diff --git a/lua/luasnip/util/util.lua b/lua/luasnip/util/util.lua index d308323d6..7a247210d 100644 --- a/lua/luasnip/util/util.lua +++ b/lua/luasnip/util/util.lua @@ -612,7 +612,7 @@ end -- compare two positions, <0 => pos1 pos1=pos2, >0 => pos1 > pos2. local function pos_cmp(pos1, pos2) -- if row is different it determines result, otherwise the column does. - return 2*cmp(pos1[1], pos2[1]) + cmp(pos1[2], pos2[2]) + return 2 * cmp(pos1[1], pos2[1]) + cmp(pos1[2], pos2[2]) end return {