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 29, 2024
1 parent 19a3b79 commit 339fb3f
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 104 deletions.
2 changes: 1 addition & 1 deletion lua/luasnip/nodes/choiceNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function ChoiceNode:set_choice(choice, current_node)
if self.restore_cursor then
local target_node = self:find_node(function(test_node)
return test_node.change_choice_id == change_choice_id
end, {find_in_child_snippets = true})
end, { find_in_child_snippets = true })

if target_node then
-- the node that the cursor was in when changeChoice was called
Expand Down
22 changes: 18 additions & 4 deletions lua/luasnip/nodes/dynamicNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ function DynamicNode:update()
tmp = SnippetNode(nil, {})
else
-- also enter node here.
tmp = self.fn(effective_args, self.parent, nil, unpack(self.user_args))
tmp = self.fn(
effective_args,
self.parent,
nil,
unpack(self.user_args)
)
end
end

Expand Down Expand Up @@ -268,8 +273,13 @@ function DynamicNode:update_static()
tmp = SnippetNode(nil, {})
else
-- also enter node here.
ok, tmp =
pcall(self.fn, effective_args, self.parent, nil, unpack(self.user_args))
ok, tmp = pcall(
self.fn,
effective_args,
self.parent,
nil,
unpack(self.user_args)
)
end
end
if not ok then
Expand Down Expand Up @@ -321,7 +331,11 @@ function DynamicNode:update_static()

tmp:update_static()
-- updates own dependents.
self:update_dependents_static({ own = true, parents = true, children = true })
self:update_dependents_static({
own = true,
parents = true,
children = true,
})
end

function DynamicNode:exit()
Expand Down
47 changes: 38 additions & 9 deletions lua/luasnip/nodes/insertNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,15 @@ function InsertNode:get_snippetstring()
local self_from, self_to = self.mark:pos_begin_end_raw()
-- only do one get_text, and establish relative offsets partition this
-- text.
local ok, text = pcall(vim.api.nvim_buf_get_text, 0, self_from[1], self_from[2], self_to[1], self_to[2], {})
local ok, text = pcall(
vim.api.nvim_buf_get_text,
0,
self_from[1],
self_from[2],
self_to[1],
self_to[2],
{}
)

local snippetstring = snippet_string.new()

Expand All @@ -353,17 +361,32 @@ function InsertNode:get_snippetstring()
return snippetstring
end

local current = {0,0}
local current = { 0, 0 }
for _, snip in ipairs(self:child_snippets()) do
local snip_from, snip_to = snip.mark:pos_begin_end_raw()
local snip_from_base_rel = util.pos_offset(self_from, snip_from)
local snip_to_base_rel = util.pos_offset(self_from, snip_to)

snippetstring:append_text(str_util.multiline_substr(text, current, snip_from_base_rel))
snippetstring:append_snip(snip, str_util.multiline_substr(text, snip_from_base_rel, snip_to_base_rel))
local snip_to_base_rel = util.pos_offset(self_from, snip_to)

snippetstring:append_text(
str_util.multiline_substr(text, current, snip_from_base_rel)
)
snippetstring:append_snip(
snip,
str_util.multiline_substr(
text,
snip_from_base_rel,
snip_to_base_rel
)
)
current = snip_to_base_rel
end
snippetstring:append_text(str_util.multiline_substr(text, current, util.pos_offset(self_from, self_to)))
snippetstring:append_text(
str_util.multiline_substr(
text,
current,
util.pos_offset(self_from, self_to)
)
)

return snippetstring
end
Expand Down Expand Up @@ -400,12 +423,18 @@ function InsertNode:put_initial(pos)
-- index.
true,
-- try to enter snippets I guess.
node_util.binarysearch_preference.inside)
node_util.binarysearch_preference.inside
)

for snip in self.static_text:iter_snippets() do
-- don't have to pass a current_node, we don't need it since we can
-- certainly link the snippet into this insertNode.
snip:insert_into_jumplist(nil, self, self.parent.snippet.child_snippets, child_snippet_idx)
snip:insert_into_jumplist(
nil,
self,
self.parent.snippet.child_snippets,
child_snippet_idx
)
child_snippet_idx = child_snippet_idx + 1
end
end
Expand Down
9 changes: 5 additions & 4 deletions lua/luasnip/nodes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -857,9 +857,10 @@ local function collect_dependents(node, which, static)
end

local function str_args(args)
return args and vim.tbl_map(function(arg)
return snippet_string.isinstance(arg) and arg:str() or arg
end, args)
return args
and vim.tbl_map(function(arg)
return snippet_string.isinstance(arg) and arg:str() or arg
end, args)
end

return {
Expand Down Expand Up @@ -887,5 +888,5 @@ return {
find_node_dependents = find_node_dependents,
collect_dependents = collect_dependents,
node_subtree_do = node_subtree_do,
str_args = str_args
str_args = str_args,
}
99 changes: 52 additions & 47 deletions lua/luasnip/nodes/util/snippet_string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local util = require("luasnip.util.util")
local SnippetString = {}
local SnippetString_mt = {
__index = SnippetString,
__tostring = SnippetString.str
__tostring = SnippetString.str,
}

local M = {}
Expand All @@ -14,7 +14,7 @@ local M = {}
---@param initial_str string[]?, optional initial multiline string.
---@return SnippetString
function M.new(initial_str)
local o = {initial_str}
local o = { initial_str }
return setmetatable(o, SnippetString_mt)
end

Expand All @@ -23,15 +23,18 @@ function M.isinstance(o)
end

function SnippetString:append_snip(snip, str)
table.insert(self, {snip = snip, str = str})
table.insert(self, { snip = snip, str = str })
end
function SnippetString:append_text(str)
table.insert(self, str)
end
function SnippetString:str()
local str = {""}
local str = { "" }
for _, snipstr_or_str in ipairs(self) do
str_util.multiline_append(str, snipstr_or_str.str and snipstr_or_str.str or snipstr_or_str)
str_util.multiline_append(
str,
snipstr_or_str.str and snipstr_or_str.str or snipstr_or_str
)
end
return str
end
Expand Down Expand Up @@ -62,11 +65,11 @@ function SnippetString:iter_snippets()
local i = 1
return function()
-- find the next snippet.
while self[i] and (not self[i].snip) do
i = i+1
while self[i] and not self[i].snip do
i = i + 1
end
local res = self[i] and self[i].snip
i = i+1
i = i + 1
return res
end
end
Expand All @@ -84,45 +87,47 @@ end

function SnippetString:reown(new_parent)
-- on 0.7 vim.deepcopy does not behave correctly => have to manually copy.
return setmetatable(vim.tbl_map(function(snipstr_or_str)
if snipstr_or_str.snip then
local snip = snipstr_or_str.snip

-- remove associations with objects beyond this snippet.
-- This is so we can easily deepcopy it without copying too much data.
-- We could also do this copy in
local prevprev = snip.prev.prev
local i0next = snip.insert_nodes[0].next
local parentnode = snip.parent_node

snip.prev.prev = nil
snip.insert_nodes[0].next = nil
snip.parent_node = nil

local snipcop = snip:copy()

snip.prev.prev = prevprev
snip.insert_nodes[0].next = i0next
snip.parent_node = parentnode


-- bring into inactive mode, so that we will jump into it correctly when it
-- is expanded again.
snipcop:subtree_do({
pre = function(node)
node.mark:invalidate()
end,
post = util.nop
})
snipcop:exit()
-- set correct parent_node.
snipcop.parent_node = new_parent

return {snip = snipcop, str = vim.deepcopy(snipstr_or_str.str)}
else
return vim.deepcopy(snipstr_or_str)
end
end, self), SnippetString_mt)
return setmetatable(
vim.tbl_map(function(snipstr_or_str)
if snipstr_or_str.snip then
local snip = snipstr_or_str.snip

-- remove associations with objects beyond this snippet.
-- This is so we can easily deepcopy it without copying too much data.
-- We could also do this copy in
local prevprev = snip.prev.prev
local i0next = snip.insert_nodes[0].next
local parentnode = snip.parent_node

snip.prev.prev = nil
snip.insert_nodes[0].next = nil
snip.parent_node = nil

local snipcop = snip:copy()

snip.prev.prev = prevprev
snip.insert_nodes[0].next = i0next
snip.parent_node = parentnode

-- bring into inactive mode, so that we will jump into it correctly when it
-- is expanded again.
snipcop:subtree_do({
pre = function(node)
node.mark:invalidate()
end,
post = util.nop,
})
snipcop:exit()
-- set correct parent_node.
snipcop.parent_node = new_parent

return { snip = snipcop, str = vim.deepcopy(snipstr_or_str.str) }
else
return vim.deepcopy(snipstr_or_str)
end
end, self),
SnippetString_mt
)
end

return M
4 changes: 2 additions & 2 deletions lua/luasnip/util/str.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ function M.multiline_substr(str, from, to)

-- include all rows
for i = from[1], to[1] do
table.insert(res, str[i+1])
table.insert(res, str[i + 1])
end

-- trim text before from and after to.
-- First trim from behind, that way this works correctly if from and to are
-- on the same line. If res[1] was trimmed first, we'd have to adjust the
-- trim-point of `to`.
res[#res] = res[#res]:sub(1, to[2])
res[1] = res[1]:sub(from[2]+1)
res[1] = res[1]:sub(from[2] + 1)

return res
end
Expand Down
4 changes: 2 additions & 2 deletions lua/luasnip/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ end
-- Assumption: `pos` occurs after `base_pos`.
local function pos_offset(base_pos, pos)
local row_offset = pos[1] - base_pos[1]
return {row_offset, row_offset == 0 and pos[2] - base_pos[2] or pos[2]}
return { row_offset, row_offset == 0 and pos[2] - base_pos[2] or pos[2] }
end

return {
Expand Down Expand Up @@ -447,5 +447,5 @@ return {
indx_of = indx_of,
ternary = ternary,
pos_cmp = pos_cmp,
pos_offset = pos_offset
pos_offset = pos_offset,
}
Loading

0 comments on commit 339fb3f

Please sign in to comment.