Format with stylua

This commit is contained in:
L3MON4D3 2023-10-04 13:03:25 +00:00 committed by github-actions[bot]
parent 8b2e2961b8
commit 3788ef48c3
11 changed files with 556 additions and 251 deletions

View file

@ -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.
@ -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
@ -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
@ -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()
@ -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)
@ -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.
@ -395,7 +412,12 @@ local function safe_choice_action(snip, ...)
else
-- 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
@ -464,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
@ -578,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
@ -605,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
@ -744,7 +776,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
@ -774,7 +806,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
@ -856,7 +891,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

View file

@ -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)
@ -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
@ -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)
@ -308,7 +311,8 @@ function InsertNode:subtree_set_pos_rgrav(pos, direction, rgrav)
direction,
rgrav,
-- don't assume that the child-snippets are all adjacent.
false)
false
)
end
function InsertNode:subtree_set_rgrav(rgrav)

View file

@ -545,7 +545,7 @@ local function focus_node(self, lrgrav, rrgrav)
-- dynamicNode, for example, the generated snippets parent is not the
-- dynamicNode, but its parent).
-- also: don't need to check for nil, because the
local node_above = nodes_path[i+1]
local node_above = nodes_path[i + 1]
if node_above then
node_above:set_sibling_rgravs(
node,
@ -603,14 +603,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 {

View file

@ -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
)
@ -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
@ -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]
@ -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
@ -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.
@ -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
@ -1302,19 +1319,15 @@ end
-- (direction is the direction into which is searched, from child_indx outward)
-- assumption: direction-endpoint of node is on child_endpoint. (caller
-- responsible)
function Snippet:set_sibling_rgravs(
node,
child_endpoint,
direction,
rgrav )
function Snippet:set_sibling_rgravs(node, child_endpoint, direction, rgrav)
node_util.nodelist_adjust_rgravs(
self.nodes,
node.absolute_position[#node.absolute_position] + direction,
child_endpoint,
direction,
rgrav,
true)
true
)
end
-- called only if the "-direction"-endpoint has to be changed, but the
@ -1335,7 +1348,8 @@ function Snippet:subtree_set_pos_rgrav(pos, direction, rgrav)
pos,
direction,
rgrav,
true)
true
)
end
-- changes rgrav of all nodes and all endpoints in this snippetNode to `rgrav`.
function Snippet:subtree_set_rgrav(rgrav)
@ -1383,13 +1397,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
@ -1453,7 +1467,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
@ -1464,12 +1479,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

View file

@ -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

View file

@ -104,7 +104,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
@ -115,7 +115,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()
@ -180,7 +180,10 @@ end
local function linkable_node(node)
-- node.type has to be one of insertNode, exitNode.
return vim.tbl_contains({types.insertNode, types.exitNode}, rawget(node, "type"))
return vim.tbl_contains(
{ types.insertNode, types.exitNode },
rawget(node, "type")
)
end
-- mainly used internally, by binarysearch_pos.
@ -190,7 +193,10 @@ end
-- feel appropriate (higher runtime), most cases should be served well by this
-- heuristic.
local function non_linkable_node(node)
return vim.tbl_contains({types.textNode, types.functionNode}, rawget(node, "type"))
return vim.tbl_contains(
{ types.textNode, types.functionNode },
rawget(node, "type")
)
end
-- return whether a node is certainly (not) interactive.
-- Coincindentially, the same nodes as (non-)linkable ones, but since there is a
@ -228,7 +234,7 @@ local binarysearch_preference = {
return cmp_mid_to > 0, cmp_mid_from < 0
end,
linkable = prefer_nodes(linkable_node, non_linkable_node),
interactive = prefer_nodes(interactive_node, non_interactive_node)
interactive = prefer_nodes(interactive_node, non_interactive_node),
}
-- `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
@ -256,12 +262,12 @@ local binarysearch_preference = {
-- This way, we are more likely to return a node that can handle a new
-- snippet/is interactive.
-- * `"prefer_outside"` makes sense when the nodes are not contiguous, and we'd
-- like to find a position between two nodes.
-- like to find a position between two nodes.
-- This mode makes sense for finding the snippet a new snippet should be
-- inserted in, since we'd like to prefer inserting before/after a snippet, if
-- the position is ambiguous.
--
-- In general:
--
-- In general:
-- These options are 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, and
@ -271,7 +277,12 @@ local binarysearch_preference = {
-- the snippet/node a new snippet should be expanded inside, it seems better to
-- shift an existing snippet to the right/left than expand the new snippet
-- inside it (when the expand-point is on the boundary).
local function binarysearch_pos(nodes, pos, respect_rgravs, boundary_resolve_mode)
local function binarysearch_pos(
nodes,
pos,
respect_rgravs,
boundary_resolve_mode
)
local left = 1
local right = #nodes
@ -281,7 +292,7 @@ local function binarysearch_pos(nodes, pos, respect_rgravs, boundary_resolve_mod
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)
@ -310,7 +321,8 @@ local function binarysearch_pos(nodes, pos, respect_rgravs, boundary_resolve_mod
local cmp_mid_to = util.pos_cmp(pos, mid_to)
local cmp_mid_from = util.pos_cmp(pos, mid_from)
local cont_behind_mid, cont_before_mid = boundary_resolve_mode(cmp_mid_to, cmp_mid_from, nodes[mid])
local cont_behind_mid, cont_before_mid =
boundary_resolve_mode(cmp_mid_to, cmp_mid_from, nodes[mid])
if cont_behind_mid then
-- make sure right-left becomes smaller.
@ -342,7 +354,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
@ -455,7 +467,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
@ -498,7 +514,8 @@ local function refocus(from, to)
-- here.
-- snippet does not have input_leave_children, so only input_leave
-- needs to be called.
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
@ -507,7 +524,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
@ -533,11 +551,17 @@ local function refocus(from, to)
-- This means that, if we want to enter a non-exitNode, we have to
-- explicitly activate the snippet for all jumps to behave correctly.
-- (if we enter a i(0)/i(-1), this is not necessary, of course).
if final_leave_node.type == types.exitNode and first_enter_node.type ~= types.exitNode then
if
final_leave_node.type == types.exitNode
and first_enter_node.type ~= types.exitNode
then
common_node:input_enter(true)
end
-- symmetrically, entering an i(0)/i(-1) requires leaving the snippet.
if final_leave_node.type ~= types.exitNode and first_enter_node.type == types.exitNode then
if
final_leave_node.type ~= types.exitNode
and first_enter_node.type == types.exitNode
then
common_node:input_leave(true)
end
@ -579,10 +603,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)
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
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()
@ -595,19 +626,25 @@ end
-- * the node of this snippet pos is on.
local function snippettree_find_undamaged_node(pos, opts)
local prev_parent, child_indx, found_parent
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: don't respect rgravs.
-- Prefer inserting the snippet outside an existing one.
found_parent, child_indx = binarysearch_pos(prev_parent_children, pos, opts.tree_respect_rgravs, opts.tree_preference)
found_parent, child_indx = binarysearch_pos(
prev_parent_children,
pos,
opts.tree_respect_rgravs,
opts.tree_preference
)
if found_parent == false then
-- if the procedure returns false, there was an error getting the
-- position of a node (in this case, that node is a snippet).
-- The position of the offending snippet is returned in child_indx,
-- and we can remove it here.
prev_parent_children[child_indx]:remove_from_jumplist()
elseif (found_parent ~= nil and not found_parent:extmarks_valid()) then
elseif found_parent ~= nil and not found_parent:extmarks_valid() then
-- 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 along the insertion-path correctly).
@ -654,7 +691,14 @@ local function root_path(node)
end
-- adjust rgravs of siblings of the node with indx child_from_indx in nodes.
local function nodelist_adjust_rgravs(nodes, child_from_indx, child_endpoint, direction, rgrav, nodes_adjacent)
local function nodelist_adjust_rgravs(
nodes,
child_from_indx,
child_endpoint,
direction,
rgrav,
nodes_adjacent
)
-- only handle siblings, not the node with child_from_indx itself.
local i = child_from_indx
local node = nodes[i]
@ -667,7 +711,13 @@ local function nodelist_adjust_rgravs(nodes, child_from_indx, child_endpoint, di
node:subtree_set_rgrav(rgrav)
else
-- either assume that they are adjacent, or check.
if nodes_adjacent or util.pos_equal(node.mark:get_endpoint(-direction), child_endpoint) then
if
nodes_adjacent
or util.pos_equal(
node.mark:get_endpoint(-direction),
child_endpoint
)
then
-- only the -direction-endpoint matches child_endpoint, adjust its
-- position and break the loop (don't need to look at any other
-- siblings).

View file

@ -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 = {}

View file

@ -627,7 +627,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 {

View file

@ -67,7 +67,8 @@ function M.session_setup_luasnip(opts)
]])
end
helpers.exec_lua([[
helpers.exec_lua(
[[
local hl_choiceNode, setup_extend = ...
-- MYVIMRC might not be set when nvim is loaded like this.
@ -89,7 +90,8 @@ function M.session_setup_luasnip(opts)
]],
-- passing nil here means the argument-list is terminated, I think.
-- Just pass false instead of nil/false.
hl_choiceNode or false, setup_extend
hl_choiceNode or false,
setup_extend
)
if not no_snip_globals then

View file

@ -5,9 +5,15 @@ local exec_lua, feed, exec = helpers.exec_lua, helpers.feed, helpers.exec
local ls_helpers = require("helpers")
local Screen = require("test.functional.ui.screen")
local function expand() exec_lua("ls.expand()") end
local function jump(dir) exec_lua("ls.jump(...)", dir) end
local function change(dir) exec_lua("ls.change_choice(...)", dir) end
local function expand()
exec_lua("ls.expand()")
end
local function jump(dir)
exec_lua("ls.jump(...)", dir)
end
local function change(dir)
exec_lua("ls.change_choice(...)", dir)
end
describe("session", function()
local screen
@ -15,7 +21,7 @@ describe("session", function()
before_each(function()
helpers.clear()
ls_helpers.setup_jsregexp()
ls_helpers.session_setup_luasnip({hl_choiceNode = true})
ls_helpers.session_setup_luasnip({ hl_choiceNode = true })
-- add a rather complicated snippet.
-- It may be a bit hard to grasp, but will cover lots and lots of
@ -133,14 +139,18 @@ describe("session", function()
[1] = { bold = true, foreground = Screen.colors.Brown },
[2] = { bold = true },
[3] = { background = Screen.colors.LightGray },
[4] = {background = Screen.colors.Red1, foreground = Screen.colors.White}
[4] = {
background = Screen.colors.Red1,
foreground = Screen.colors.White,
},
})
end)
it("Deleted snippet is handled properly in expansion.", function()
feed("o<Cr><Cr><Up>fn")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
|
|
/** |
@ -170,9 +180,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
jump(1) jump(1) jump(1)
screen:expect{grid=[[
{2:-- INSERT --} |]],
})
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
|
|
/** |
@ -202,15 +216,18 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- delete whole buffer.
feed("<Esc>ggVGcfn")
-- immediately expand at the old position of the snippet.
exec_lua("ls.expand()")
-- first jump goes to i(-1), second might go back into deleted snippet,
-- if we did something wrong.
jump(-1) jump(-1)
screen:expect{grid=[[
jump(-1)
jump(-1)
screen:expect({
grid = [[
^/** |
* A short Description |
*/ |
@ -240,11 +257,18 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- seven jumps to go to i(0), 8th, again, should not do anything.
jump(1) jump(1) jump(1) jump(1)
jump(1) jump(1) jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -274,14 +298,16 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
jump(1)
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
end)
it("Deleted snippet is handled properly when jumping.", function()
feed("o<Cr><Cr><Up>fn")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
|
|
/** |
@ -311,9 +337,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
jump(1) jump(1) jump(1)
screen:expect{grid=[[
{2:-- INSERT --} |]],
})
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
|
|
/** |
@ -343,7 +373,8 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- delete whole buffer.
feed("<Esc>ggVGd")
-- should not cause an error.
@ -352,7 +383,8 @@ describe("session", function()
it("Deleting nested snippet only removes it.", function()
feed("o<Cr><Cr><Up>fn")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
|
|
/** |
@ -382,10 +414,12 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("<Esc>jlafn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
|
|
/** |
@ -415,14 +449,19 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
jump(1) jump(1)
{2:-- INSERT --} |]],
})
jump(1)
jump(1)
feed("<Esc>llllvbbbx")
-- first jump goes into function-arguments, second will trigger update,
-- which will in turn recognize the broken snippet.
-- The third jump will then go into the outer snippet.
jump(1) jump(1) jump(-1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(-1)
screen:expect({
grid = [[
|
|
/** |
@ -452,11 +491,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
-- this should jump into the $0 of the outer snippet, highlighting the
-- entire nested snippet.
jump(1)
screen:expect{grid=[[
screen:expect({
grid = [[
|
|
/** |
@ -486,27 +527,34 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
end)
for _, link_roots_val in ipairs({"true", "false"}) do
it(("Snippets are inserted according to link_roots and keep_roots=%s"):format(link_roots_val), function()
exec_lua(([[
for _, link_roots_val in ipairs({ "true", "false" }) do
it(
("Snippets are inserted according to link_roots and keep_roots=%s"):format(
link_roots_val
),
function()
exec_lua(([[
ls.setup({
keep_roots = %s,
link_roots = %s
})
]]):format(link_roots_val, link_roots_val))
feed("ifn")
expand()
-- "o" does not extend the extmark of the active snippet.
feed("<Esc>Go<Cr>fn")
expand()
jump(-1) jump(-1)
-- if linked, should end up back in the original snippet, if not,
-- stay in second.
if link_roots_val == "true" then
screen:expect{grid=[[
feed("ifn")
expand()
-- "o" does not extend the extmark of the active snippet.
feed("<Esc>Go<Cr>fn")
expand()
jump(-1)
jump(-1)
-- if linked, should end up back in the original snippet, if not,
-- stay in second.
if link_roots_val == "true" then
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -536,9 +584,11 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
else
screen:expect{grid=[[
{2:-- INSERT --} |]],
})
else
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -568,11 +618,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
end
end
end)
)
end
for _, keep_roots_val in ipairs({"true", "false"}) do
for _, keep_roots_val in ipairs({ "true", "false" }) do
it("Root-snippets are stored iff keep_roots=true", function()
exec_lua(([[
ls.setup({
@ -594,7 +646,8 @@ describe("session", function()
-- if linked, should end up back in the original snippet, if not,
-- stay in second.
if keep_roots_val == "true" then
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* ^A{3: short Description} |
*/ |
@ -624,17 +677,14 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
else
assert(
err:match(
"No Snippet at that position"
)
)
assert(err:match("No Snippet at that position"))
end
end)
end
for _, link_children_val in ipairs({"true", "false"}) do
for _, link_children_val in ipairs({ "true", "false" }) do
it("Child-snippets are linked iff link_children=true", function()
exec_lua(([[
ls.setup({
@ -650,7 +700,8 @@ describe("session", function()
-- expand another child.
feed("<Esc>jjAfn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -680,14 +731,17 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- if linked, should end up back in the original snippet, if not,
-- stay in second.
if link_children_val == "true" then
-- make sure we can jump into the previous child...
jump(-1) jump(-1)
screen:expect{grid=[[
jump(-1)
jump(-1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -717,10 +771,19 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- ...and from the first child back into the parent...
jump(-1) jump(-1) jump(-1) jump(-1) jump(-1) jump(-1) jump(-1) jump(-1)
screen:expect{grid=[[
jump(-1)
jump(-1)
jump(-1)
jump(-1)
jump(-1)
jump(-1)
jump(-1)
jump(-1)
screen:expect({
grid = [[
/** |
* ^A{3: short Description} |
*/ |
@ -750,11 +813,26 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
-- ...and back to the end of the second snippet...
-- (first only almost to the end, to make sure we makde the correct number of jumps).
jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1) jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -784,9 +862,11 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
jump(1)
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -816,19 +896,21 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- test inability to jump beyond a few times, I've had bugs
-- where after a multiple jumps, a new node became active.
jump(1)
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
jump(1)
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
jump(1)
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
-- For good measure, make sure the node is actually still active.
jump(-1)
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -858,9 +940,9 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
else
end
end)
end
@ -868,12 +950,13 @@ describe("session", function()
feed("ifn")
expand()
-- delete the entier text of a textNode, which will make
-- extmarks_valid() false.
-- extmarks_valid() false.
feed("<Esc>eevllx")
-- insert snippet inside the invalid parent.
feed("jAfn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -903,11 +986,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- make sure the parent is invalid.
jump(-1)
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -937,11 +1022,12 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- should not move back into the parent.
jump(-1)
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
end)
it("region_check_events works correctly", function()
exec_lua([[
@ -962,7 +1048,8 @@ describe("session", function()
-- expand snippet.
feed("ifn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -992,11 +1079,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- leave its region.
feed("<Esc>Go<Esc>")
-- check we have left the snippet (choiceNode indicator no longer active).
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1026,17 +1115,20 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
|]]}
|]],
})
-- re-activate $0, expand child.
jump(-1) jump(1)
jump(-1)
jump(1)
feed("fn")
expand()
-- jump behind child, activate region_leave, make sure the child and
-- root-snippet are _not_ exited.
feed("<Esc>jjA<Esc>o<Esc>")
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1066,10 +1158,12 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
|]]}
|]],
})
-- .. and now both are left upon leaving the region of the root-snippet.
feed("<Esc>jji<Esc>")
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1099,7 +1193,8 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
|]]}
|]],
})
end)
it("delete_check_events works correctly", function()
exec_lua([[
@ -1120,7 +1215,8 @@ describe("session", function()
-- expand.
feed("ifn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1150,11 +1246,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- delete textNode, to trigger unlink_current_if_deleted via esc.
feed("<Esc>eevllx<Esc>")
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1184,9 +1282,10 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
|]]}
|]],
})
jump(1)
screen:expect{unchanged=true}
screen:expect({ unchanged = true })
end)
it("Insertion into non-interactive node works correctly", function()
feed("ifn")
@ -1195,7 +1294,8 @@ describe("session", function()
-- expand snippet in textNode, ie. s.t. it can't be properly linked up.
feed("<Esc>kifn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
/** |
@ -1225,10 +1325,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- jump into startNode, and back into current node.
jump(-1) jump(-1)
screen:expect{grid=[[
jump(-1)
jump(-1)
screen:expect({
grid = [[
/** |
* A short Description |
/** |
@ -1258,14 +1361,21 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- check jumping out in other direction.
feed("<Esc>jjifn")
expand()
-- jump to one before jumping out of child-snippet.
jump(1) jump(1) jump(1) jump(1) jump(1) jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A short Description |
/** |
@ -1295,11 +1405,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- leave child.
jump(1)
-- check back in current node.
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
/** |
@ -1329,7 +1441,8 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
end)
it("All operations work as expected in a longer session.", function()
exec_lua([[
@ -1352,7 +1465,8 @@ describe("session", function()
expand()
feed("<Esc>kkwwwifn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A /** |
* A short Description |
@ -1382,11 +1496,13 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("<Esc>ggOfn")
expand()
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1416,13 +1532,19 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- ensure correct linkage.
jump(1) jump(1) jump(1)
jump(1) jump(1) jump(1)
jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1452,12 +1574,14 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- enter third choiceNode of second expanded snippet.
feed("<Esc>kkkk$h")
exec_lua([[require("luasnip").activate_node()]])
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1487,11 +1611,15 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- check connectivity.
jump(1) jump(1) jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1521,18 +1649,21 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- stay at last node.
jump(1)
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
-- expand in textNode.
feed("<Esc>kkbifn")
expand()
-- check connectivity.
jump(-1) jump(-1)
screen:expect{grid=[[
jump(-1)
jump(-1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1562,12 +1693,20 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
-- end up back in last node, not in textNode-expanded snippet.
jump(1) jump(1) jump(1) jump(1)
jump(1) jump(1) jump(1) jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A short Description |
*/ |
@ -1597,14 +1736,16 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("<Esc>gg")
exec_lua([[require("luasnip").activate_node()]])
feed("<Esc>Vjjjjjx")
exec_lua("ls.unlink_current_if_deleted()")
screen:expect{grid=[[
screen:expect({
grid = [[
^/** |
* A /** |
* A short Description |
@ -1634,10 +1775,12 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
6 fewer lines |]]}
6 fewer lines |]],
})
-- first snippet is active again.
jump(1)
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* A /** |
* A short Description |
@ -1667,15 +1810,26 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- make sure the deleted snippet got disconnected properly.
assert.are.same(exec_lua([[return ls.session.current_nodes[1].parent.snippet.prev.prev and "Node before" or "No node before"]]), "No node before")
assert.are.same(
exec_lua(
[[return ls.session.current_nodes[1].parent.snippet.prev.prev and "Node before" or "No node before"]]
),
"No node before"
)
-- jump a bit into snippet, so exit_out_of_region changes the current snippet.
jump(1) jump(1) jump(1) jump(1)
jump(1) jump(1)
screen:expect{grid=[[
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
jump(1)
screen:expect({
grid = [[
/** |
* A /** |
* A short Description |
@ -1705,12 +1859,14 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("<Esc>Go")
exec_lua("ls.exit_out_of_region(ls.session.current_nodes[1])")
jump(-1)
screen:expect{grid=[[
screen:expect({
grid = [[
/** |
* ^A{3: /**} |
{3: * A short Description} |
@ -1740,6 +1896,7 @@ describe("session", function()
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
end)
end)

View file

@ -250,24 +250,30 @@ describe("snippets_basic", function()
})
-- last snippet is not forgotten (yet).
exec_lua("ls.jump(1)")
screen:expect{grid=[[
screen:expect({
grid = [[
a[^a{3:[]ab}]ab |
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
feed("<Esc>o")
exec_lua("ls.snip_expand(" .. snip .. ")")
screen:expect{grid=[[
screen:expect({
grid = [[
a[a[]ab]ab |
a[^]ab |
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec_lua("ls.jump(-1) ls.jump(-1)")
-- first snippet can't be accessed anymore.
screen:expect{grid=[[
screen:expect({
grid = [[
a[a[]ab]ab |
^a[]ab |
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
end)
it("history=true allows jumping back into exited snippet.", function()
@ -1212,32 +1218,38 @@ describe("snippets_basic", function()
feed("iaa")
exec_lua([[ ls.expand() ]])
exec_lua([[ ls.jump(1) ]])
screen:expect{grid=[[
screen:expect({
grid = [[
a:(^) |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("aa")
exec_lua([[ ls.expand() ]])
exec_lua([[ ls.jump(1) ]])
screen:expect{grid=[[
screen:expect({
grid = [[
a:(a:(^)) |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("aa")
exec_lua([[ ls.expand() ]])
exec_lua([[ ls.jump(1) ]])
screen:expect{grid=[[
screen:expect({
grid = [[
a:(a:(a:(^))) |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- jump should not move cursor!
-- for some reason need multiple jumps to trigger the mistake.
exec_lua([[ ls.jump(1)]])
exec_lua([[ ls.jump(1)]])
screen:expect{unchanged = true}
screen:expect({ unchanged = true })
end)
it("exit_out_of_region activates last node of snippet-root.", function()
@ -1254,40 +1266,46 @@ describe("snippets_basic", function()
feed("<Esc>lllliaa")
exec_lua("ls.expand()")
exec_lua("ls.jump(-1) ls.jump(-1)")
screen:expect{grid=[[
screen:expect({
grid = [[
^( 0-( 0-text )text ) |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("<Esc>o")
exec_lua("ls.exit_out_of_region(ls.session.current_nodes[1])")
-- verify that we are in the $0 of the nested snippet.
exec_lua("ls.jump(-1)")
screen:expect{grid=[[
screen:expect({
grid = [[
( 0-^( 0-text )text ) |
|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec_lua("ls.jump(1)")
screen:expect{grid=[[
screen:expect({
grid = [[
( 0-( ^0{3:-text} )text ) |
|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
end)
it("focus correctly adjusts gravities of parent-snippets.", function()
exec_lua[[
exec_lua([[
ls.setup{
link_children = true
}
]]
]])
exec_lua([[ls.lsp_expand("a$1$1a")]])
exec_lua([[ls.lsp_expand("b$1")]])
feed("ccc")
exec_lua([[ls.active_update_dependents()]])
feed("dddd")
-- Here's how this fails if `focus` does not behave correctly (ie. only
-- adjusts extmarks in the snippet the current node is inside):
-- adjusts extmarks in the snippet the current node is inside):
-- child has a changed $1, triggers update of own snippets, and
-- transitively of the parent-$1.
-- Since the parent has a functionNode that copies the $1's text, it
@ -1303,18 +1321,22 @@ describe("snippets_basic", function()
-- If, in doing this, the parent-$1-extmark end-gravity is not restored
-- to the right, the child-snippet will extend beyond the extmark of
-- its parent-node, the parent-$1.
exec_lua[[ls.jump(-1) ls.jump(-1)]]
exec_lua([[ls.jump(-1) ls.jump(-1)]])
-- highlights outer $1.
exec_lua[[ls.jump(1)]]
screen:expect{grid=[[
exec_lua([[ls.jump(1)]])
screen:expect({
grid = [[
a^b{3:cccdddd}bcccdddda |
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
-- and then inner $1.
exec_lua[[ls.jump(1)]]
screen:expect{grid=[[
exec_lua([[ls.jump(1)]])
screen:expect({
grid = [[
ab^c{3:ccdddd}bcccdddda |
{0:~ }|
{2:-- SELECT --} |]]}
{2:-- SELECT --} |]],
})
end)
end)