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 Oct 3, 2023
1 parent 2f9e58e commit 5593d42
Show file tree
Hide file tree
Showing 11 changed files with 521 additions and 231 deletions.
69 changes: 52 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.parent.snippet, "Error while setting adjacent snippet as current node: %s", err)
unlink_set_adjacent_as_current(
next_current.parent.snippet,
"Error while setting adjacent snippet as current node: %s",
err
)
end
end
end
Expand Down Expand Up @@ -139,7 +145,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 All @@ -160,7 +171,8 @@ end

local function jumpable(dir)
-- node is jumpable if there is a destination.
return jump_destination(dir) ~= session.current_nodes[vim.api.nvim_get_current_buf()]
return jump_destination(dir)
~= session.current_nodes[vim.api.nvim_get_current_buf()]
end

local function expandable()
Expand All @@ -186,7 +198,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 @@ -260,7 +276,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 @@ -397,7 +414,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 @@ -466,19 +488,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 @@ -580,7 +604,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 @@ -607,7 +635,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 @@ -746,7 +778,7 @@ local function activate_node(opts)
local _, _, _, node = node_util.snippettree_find_undamaged_node(pos, {
tree_respect_rgravs = false,
tree_preference = node_util.binarysearch_preference.inside,
snippet_mode = "interactive"
snippet_mode = "interactive",
})

if not node then
Expand Down Expand Up @@ -776,7 +808,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
)
if select then
-- 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
Expand Down Expand Up @@ -858,7 +893,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
15 changes: 12 additions & 3 deletions lua/luasnip/nodes/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,23 @@ end

function Node:linkable()
-- linkable if insert or exitNode.
return vim.tbl_contains({types.insertNode, types.exitNode}, rawget(self, "type"))
return vim.tbl_contains(
{ types.insertNode, types.exitNode },
rawget(self, "type")
)
end
function Node:interactive()
-- interactive if immediately inside choiceNode.
return vim.tbl_contains({types.insertNode, types.exitNode}, rawget(self, "type")) or rawget(self, "choice") ~= nil
return vim.tbl_contains(
{ types.insertNode, types.exitNode },
rawget(self, "type")
) or rawget(self, "choice") ~= nil
end
function Node:leaf()
return vim.tbl_contains({types.textNode, types.functionNode, types.insertNode, types.exitNode}, rawget(self, "type"))
return vim.tbl_contains(
{ types.textNode, types.functionNode, types.insertNode, types.exitNode },
rawget(self, "type")
)
end

return {
Expand Down
57 changes: 40 additions & 17 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,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 @@ -487,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 @@ -523,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 @@ -620,11 +629,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_mode = "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_mode = "linkable",
})

if current_node then
if parent_node then
Expand Down Expand Up @@ -718,7 +728,7 @@ function Snippet:trigger_expand(current_node, pos_id, env)
start_node.mark = self.nodes[1].mark
start_node.pos = -1
-- needed for querying node-path from snippet to this node.
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 @@ -734,7 +744,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 @@ -1405,13 +1422,13 @@ function Snippet:node_at(pos, mode)
-- all nodes well before it are quickly skipped => should benefit
-- all cases where the runtime of this is noticeable, and which are not
-- unrealistic (lots of zero-width nodes).
if util.pos_cmp(pos, {node_to[1], node_to[2]+1}) > 0 then
if util.pos_cmp(pos, { node_to[1], node_to[2] + 1 }) > 0 then
return false
end

-- generate gravity-adjusted endpoints.
local grav_adjusted_from = {node_from[1], node_from[2]}
local grav_adjusted_to = {node_to[1], node_to[2]}
local grav_adjusted_from = { node_from[1], node_from[2] }
local grav_adjusted_to = { node_to[1], node_to[2] }
if node_mark:get_rgrav(-1) then
grav_adjusted_from[2] = grav_adjusted_from[2] + 1
end
Expand Down Expand Up @@ -1475,7 +1492,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 @@ -1486,12 +1504,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 5593d42

Please sign in to comment.