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 28, 2023
1 parent b3010ad commit fe6240e
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 179 deletions.
71 changes: 54 additions & 17 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 All @@ -106,7 +108,11 @@ local function unlink_set_adjacent_as_current_no_log(snippet)
-- this won't try to set the previously broken snippet as
-- current, since that link is removed in
-- `remove_from_jumplist`.
unlink_set_adjacent_as_current(next_current, "Error while setting adjacent snippet as current node: %s", err)
unlink_set_adjacent_as_current(
next_current,
"Error while setting adjacent snippet as current node: %s",
err
)
end
end
end
Expand Down Expand Up @@ -138,7 +144,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 @@ -185,7 +196,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 @@ -259,7 +274,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.keep_roots 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 @@ -396,7 +412,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 @@ -465,19 +486,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 @@ -579,7 +602,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 @@ -606,7 +633,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 @@ -736,7 +767,7 @@ local function activate_node(opts)
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,
})

if not node then
Expand All @@ -746,7 +777,10 @@ local function activate_node(opts)

-- 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
if strict then
error("Refusing to activate a non-interactive node.")
return
Expand All @@ -766,7 +800,10 @@ local function activate_node(opts)
end
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 @@ -846,7 +883,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
61 changes: 45 additions & 16 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 @@ -610,11 +619,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 @@ -695,7 +705,7 @@ function Snippet:trigger_expand(current_node, pos_id, env)
-- Marks should stay at the beginning of the snippet, only the first mark is needed.
start_node.mark = self.nodes[1].mark
start_node.pos = -1
start_node.absolute_position = {-1}
start_node.absolute_position = { -1 }
start_node.parent = self

-- hook up i0 and start_node, and then the snippet itself.
Expand All @@ -711,7 +721,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 @@ -1348,10 +1365,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 @@ -1384,7 +1407,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 @@ -1395,12 +1419,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 fe6240e

Please sign in to comment.