Skip to content

Commit

Permalink
Format with stylua
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 authored and github-actions[bot] committed Jul 11, 2023
1 parent 645b3cb commit 25491aa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
6 changes: 5 additions & 1 deletion lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,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
Expand Down
50 changes: 39 additions & 11 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -446,13 +446,15 @@ 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)
local found_parent, child_indx =
node_util.binarysearch_pos(prev_parent_children, pos, false)
if found_parent == false then
-- error while running procedure!
local bad_snippet = child_indx
Expand All @@ -479,7 +481,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
local sibling_list = self.parent_node ~= nil
and self.parent_node.parent.snippet.child_snippets
or session.snippet_roots
local self_indx
for i, snip in ipairs(sibling_list) do
if snip == self then
Expand Down Expand Up @@ -515,8 +519,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]

Expand All @@ -533,7 +544,10 @@ local function insert_into_jumplist(snippet, start_node, current_node, parent_no
end

if parent_node then
local can_link_parent_node = vim.tbl_contains({types.insertNode, types.snippetNode, types.exitNode}, rawget(parent_node, "type"))
local can_link_parent_node = vim.tbl_contains(
{ types.insertNode, types.snippetNode, types.exitNode },
rawget(parent_node, "type")
)
-- snippetNode (which has to be empty to be viable here) and
-- insertNode can both deal with inserting a snippet inside them
-- (ie. hooking it up st. it can be visited after jumping back to
Expand Down Expand Up @@ -594,7 +608,8 @@ function Snippet:trigger_expand(current_node, pos_id, env)
-- should not be an infinite loop (done in one iteration, in most cases, actually)
while true do
-- find snippettree-position.
parent_snippet, sibling_snippets, own_indx = find_snippettree_position(pos)
parent_snippet, sibling_snippets, own_indx =
find_snippettree_position(pos)
if parent_snippet then
local ok
-- if found, find node to insert at.
Expand Down Expand Up @@ -699,7 +714,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
Expand Down Expand Up @@ -1320,10 +1342,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
Expand Down
18 changes: 11 additions & 7 deletions lua/luasnip/nodes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -187,7 +187,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
Expand All @@ -205,7 +205,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),
Expand Down Expand Up @@ -233,7 +233,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)

Expand Down Expand Up @@ -288,7 +288,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
Expand Down Expand Up @@ -400,7 +400,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
Expand Down
4 changes: 2 additions & 2 deletions lua/luasnip/session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/luasnip/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ end
-- compare two positions, <0 => pos1<pos2, 0 => 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 {
Expand Down

0 comments on commit 25491aa

Please sign in to comment.