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 Sep 22, 2023
1 parent 1e44993 commit 1697f69
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 110 deletions.
65 changes: 49 additions & 16 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ local function unlink_set_adjacent_as_current_no_log(snippet)
if next_current then
-- if snippet was active before, we need to now set its parent to be no
-- longer inner_active.
if snippet.parent_node == next_current and next_current.inner_active then
if
snippet.parent_node == next_current and next_current.inner_active
then
snippet.parent_node:input_leave_children()
else
-- set no_move.
Expand Down Expand Up @@ -131,7 +133,12 @@ local function safe_jump_current(dir, no_move, dry_run)
else
local snip = node.parent.snippet

unlink_set_adjacent_as_current(snip, "Removing snippet `%s` due to error %s", snip.trigger, res)
unlink_set_adjacent_as_current(
snip,
"Removing snippet `%s` due to error %s",
snip.trigger,
res
)
return session.current_nodes[vim.api.nvim_get_current_buf()]
end
end
Expand Down Expand Up @@ -178,7 +185,11 @@ local function in_snippet()
-- if there was an error getting the position, the snippets text was
-- most likely removed, resulting in messed up extmarks -> error.
-- remove the snippet.
unlink_set_adjacent_as_current(snippet, "Error while getting extmark-position: %s", snip_begin_pos)
unlink_set_adjacent_as_current(
snippet,
"Error while getting extmark-position: %s",
snip_begin_pos
)
return
end
local pos = vim.api.nvim_win_get_cursor(0)
Expand Down Expand Up @@ -252,7 +263,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 @@ -389,7 +401,12 @@ local function safe_choice_action(snip, ...)

-- not very elegant, but this way we don't have a near
-- re-implementation of unlink_current.
unlink_set_adjacent_as_current(snip, "Removing snippet `%s` due to error %s", snip.trigger, res)
unlink_set_adjacent_as_current(
snip,
"Removing snippet `%s` due to error %s",
snip.trigger,
res
)
return session.current_nodes[vim.api.nvim_get_current_buf()]
end
end
Expand Down Expand Up @@ -458,19 +475,21 @@ local function active_update_dependents()

local ok, err = pcall(active.update_dependents, active)
if not ok then
log.warn(
)
unlink_set_adjacent_as_current(active.parent.snippet,
log.warn()
unlink_set_adjacent_as_current(
active.parent.snippet,
"Error while updating dependents for snippet %s due to error %s",
active.parent.snippet.trigger,
err)
err
)
return
end

-- 'restore' orientation of extmarks, may have been changed by some set_text or similar.
ok, err = pcall(active.focus, active)
if not ok then
unlink_set_adjacent_as_current(active.parent.snippet,
unlink_set_adjacent_as_current(
active.parent.snippet,
"Error while entering node in snippet %s: %s",
active.parent.snippet.trigger,
err
Expand Down Expand Up @@ -572,7 +591,11 @@ local function unlink_current_if_deleted()
-- * textnodes that should contain text still do so, and
-- * that extmarks still fulfill all expectations (should be successive, no gaps, etc.)
if not snippet:extmarks_valid() then
unlink_set_adjacent_as_current(snippet, "Detected deletion of snippet `%s`, removing it", snippet.trigger)
unlink_set_adjacent_as_current(
snippet,
"Detected deletion of snippet `%s`, removing it",
snippet.trigger
)
end
end

Expand All @@ -599,7 +622,11 @@ local function exit_out_of_region(node)
pcall(snippet.mark.pos_begin_end, snippet.mark)

if not ok then
unlink_set_adjacent_as_current(snippet, "Error while getting extmark-position: %s", snip_begin_pos)
unlink_set_adjacent_as_current(
snippet,
"Error while getting extmark-position: %s",
snip_begin_pos
)
return
end

Expand Down Expand Up @@ -727,17 +754,23 @@ local function activate_node(pos)
local _, _, _, node = node_util.snippettree_find_undamaged_node(pos, {
tree_respect_rgravs = true,
tree_preference = node_util.binarysearch_preference.inside,
snippet_preference = node_util.binarysearch_preference.interactive
snippet_preference = node_util.binarysearch_preference.interactive,
})

-- only activate interactive nodes, or nodes that are immediately nested
-- inside a choiceNode.
if not node_util.interactive_node(node) and rawget(node, "choice") == nil then
if
not node_util.interactive_node(node)
and rawget(node, "choice") == nil
then
print("Refusing to activate a non-interactive node.")
return
end

node_util.refocus(session.current_nodes[vim.api.nvim_get_current_buf()], node)
node_util.refocus(
session.current_nodes[vim.api.nvim_get_current_buf()],
node
)
-- input_enter node again, to get highlight and the like.
-- One side-effect of this is that an event will be execute twice, but I
-- feel like that is a trade-off worth doing, since it otherwise refocus
Expand Down Expand Up @@ -817,7 +850,7 @@ ls = util.lazy_table({
setup = require("luasnip.config").setup,
extend_decorator = extend_decorator,
log = require("luasnip.util.log"),
activate_node = activate_node
activate_node = activate_node,
}, ls_lazy)

return ls
9 changes: 6 additions & 3 deletions lua/luasnip/nodes/insertNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ function ExitNode:jump_from(dir, no_move, dry_run)
self:init_dry_run_inner_active(dry_run)

local next_node = util.ternary(dir == 1, self.next, self.prev)
local next_inner_node = util.ternary(dir == 1, self.inner_first, self.inner_last)
local next_inner_node =
util.ternary(dir == 1, self.inner_first, self.inner_last)

if next_inner_node then
self:input_enter_children(dry_run)
Expand All @@ -199,7 +200,8 @@ function ExitNode:jump_from(dir, no_move, dry_run)
-- not have children active if jump_from is called.

-- true: don't move
local target_node = next_node:jump_into(dir, true, next_node_dry_run)
local target_node =
next_node:jump_into(dir, true, next_node_dry_run)
-- if there is no node that can serve as jump-target, just remain
-- here.
-- Regular insertNodes don't have to handle this, since there is
Expand All @@ -220,7 +222,8 @@ function InsertNode:jump_from(dir, no_move, dry_run)
self:init_dry_run_inner_active(dry_run)

local next_node = util.ternary(dir == 1, self.next, self.prev)
local next_inner_node = util.ternary(dir == 1, self.inner_first, self.inner_last)
local next_inner_node =
util.ternary(dir == 1, self.inner_first, self.inner_last)

if next_inner_node then
self:input_enter_children(dry_run)
Expand Down
59 changes: 44 additions & 15 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,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 @@ -483,7 +483,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 @@ -519,8 +521,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 @@ -612,11 +621,12 @@ 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, {})

-- find tree-node the snippet should be inserted at (could be before another node).
local _, sibling_snippets, own_indx, parent_node = node_util.snippettree_find_undamaged_node(pos, {
tree_respect_rgravs = false,
tree_preference = node_util.binarysearch_preference.outside,
snippet_preference = node_util.binarysearch_preference.linkable
})
local _, sibling_snippets, own_indx, parent_node =
node_util.snippettree_find_undamaged_node(pos, {
tree_respect_rgravs = false,
tree_preference = node_util.binarysearch_preference.outside,
snippet_preference = node_util.binarysearch_preference.linkable,
})
if current_node then
node_util.refocus(current_node, parent_node)
if parent_node then
Expand Down Expand Up @@ -712,7 +722,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 @@ -1344,10 +1361,16 @@ end
-- pos-column has to be a byte-index, not a display-column.
function Snippet:smallest_node_at(pos, mode)
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, mode)
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 @@ -1380,7 +1403,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 @@ -1391,12 +1415,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
8 changes: 7 additions & 1 deletion lua/luasnip/nodes/textNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ end

function TextNode:extmarks_valid()
local from, to = self.mark:pos_begin_end_raw()
if util.pos_cmp(from, to) == 0 and not (#self.static_text == 0 or (#self.static_text == 1 and #self.static_text[1] == 0)) then
if
util.pos_cmp(from, to) == 0
and not (
#self.static_text == 0
or (#self.static_text == 1 and #self.static_text[1] == 0)
)
then
-- assume the snippet is invalid if a textNode occupies zero space,
-- but has text which would occupy some.
-- This should allow some modifications, but as soon as a textNode is
Expand Down
Loading

0 comments on commit 1697f69

Please sign in to comment.