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 2, 2023
1 parent de48e16 commit 22d4a1a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
6 changes: 5 additions & 1 deletion lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,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 not found_parent then
-- prev_parent is nil if this snippet is expanded at the top-level.
return prev_parent, prev_parent_children, child_indx
Expand All @@ -473,7 +475,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 @@ -501,8 +505,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 @@ -519,7 +530,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}, rawget(parent_node, "type"))
local can_link_parent_node = vim.tbl_contains(
{ types.insertNode, types.snippetNode },
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 @@ -576,7 +590,8 @@ end
function Snippet:trigger_expand(current_node, pos_id, env)
local pos = vim.api.nvim_buf_get_extmark_by_id(0, session.ns_id, pos_id, {})

local parent_snippet, sibling_snippets, own_indx = find_snippettree_position(pos)
local parent_snippet, sibling_snippets, own_indx =
find_snippettree_position(pos)
-- may be nil, ofc.
local parent_node = parent_snippet and parent_snippet:node_at(pos)

Expand Down Expand Up @@ -668,7 +683,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
)
end

-- returns copy of snip if it matches, nil if not.
Expand Down Expand Up @@ -1287,10 +1309,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
10 changes: 5 additions & 5 deletions lua/luasnip/nodes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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 @@ -110,7 +110,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 @@ -185,7 +185,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 @@ -203,7 +203,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 @@ -231,7 +231,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 mid_from, mid_to = mid_mark:pos_begin_end_raw()

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 22d4a1a

Please sign in to comment.