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 15, 2023
1 parent a11fb33 commit 12426d5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 33 deletions.
9 changes: 7 additions & 2 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
59 changes: 46 additions & 13 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,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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
46 changes: 31 additions & 15 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 @@ -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 = {
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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,
}
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 12426d5

Please sign in to comment.