Skip to content

Commit

Permalink
handle errors in binarysearch_pos.
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 committed Jul 11, 2023
1 parent 37a393a commit 56d9997
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
31 changes: 27 additions & 4 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,13 @@ local function find_snippettree_position(pos)
-- 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 not found_parent then
if found_parent == false then
-- error while running procedure!
local bad_snippet = child_indx
bad_snippet:unlink_current()
-- continue again with same parent, but one less snippet in its
-- children => shouldn't cause endless loop.
elseif found_parent == nil then
-- prev_parent is nil if this snippet is expanded at the top-level.
return prev_parent, prev_parent_children, child_indx
else
Expand Down Expand Up @@ -584,9 +590,26 @@ 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)
-- may be nil, ofc.
local parent_node = parent_snippet and parent_snippet:node_at(pos)
local parent_snippet, sibling_snippets, own_indx, parent_node
-- 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)
if parent_snippet then
local ok
-- if found, find node to insert at.
ok, parent_node = pcall(parent_snippet.node_at, parent_snippet, pos)
if ok then
break
else
-- error while finding node in snippet => remove snippet from jumplist and try again.
parent_snippet:remove_from_jumplist()
end
else
parent_node = nil
break
end
end

local pre_expand_res = self:event(events.pre_expand, { expand_pos = pos })
or {}
Expand Down
10 changes: 9 additions & 1 deletion lua/luasnip/nodes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,15 @@ local function binarysearch_pos(nodes, pos, respect_rgravs)
while true do
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()
local ok, mid_from, mid_to = pcall(mid_mark.pos_begin_end_raw, mid_mark)

if not ok then
-- error while running this procedure!
-- return false (because I don't know how to do this with `error`
-- and the offending node).
-- (returning data instead of a message in `error` seems weird..)
return false, mid
end

if respect_rgravs then
-- if rgrav is set on either endpoint, the node considers its
Expand Down

0 comments on commit 56d9997

Please sign in to comment.