Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions lua/luasnip/extras/fmt.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
local text_node = require("luasnip.nodes.textNode").T
local wrap_nodes = require("luasnip.util.util").wrap_nodes
local util = require("luasnip.util.util")
local extend_decorator = require("luasnip.util.extend_decorator")
local Str = require("luasnip.util.str")
local rp = require("luasnip.extras").rep

-- https://gist.github.com/tylerneylon/81333721109155b2d244
local function copy3(obj, seen)
-- Handle non-tables and previously-seen tables.
if type(obj) ~= "table" then
return obj
end
if seen and seen[obj] then
return seen[obj]
end

-- New table; mark it as seen an copy recursively.
local s = seen or {}
local res = {}
s[obj] = res
for k, v in next, obj do
res[copy3(k, s)] = copy3(v, s)
end
return setmetatable(res, getmetatable(obj))
end

-- Interpolate elements from `args` into format string with placeholders.
--
-- The placeholder syntax for selecting from `args` is similar to fmtlib and
Expand Down Expand Up @@ -102,7 +82,7 @@ local function interpolate(fmt, args, opts)
if used_keys[key] then
local jump_index = args[key]:get_jump_index() -- For nodes that don't have a jump index, copy it instead
if not opts.repeat_duplicates or jump_index == nil then
table.insert(elements, copy3(args[key]))
table.insert(elements, util.copy3(args[key]))
else
table.insert(elements, rp(jump_index))
end
Expand Down Expand Up @@ -198,7 +178,7 @@ local function format_nodes(str, nodes, opts)
opts = vim.tbl_extend("force", defaults, opts or {})

-- allow to pass a single node
nodes = wrap_nodes(nodes)
nodes = util.wrap_nodes(nodes)

-- optimization: avoid splitting multiple times
local lines = nil
Expand Down
22 changes: 1 addition & 21 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -859,28 +859,8 @@ function Snippet:matches(line_to_cursor, opts)
return expand_params
end

-- https://gist.github.com/tylerneylon/81333721109155b2d244
local function copy3(obj, seen)
-- Handle non-tables and previously-seen tables.
if type(obj) ~= "table" then
return obj
end
if seen and seen[obj] then
return seen[obj]
end

-- New table; mark it as seen an copy recursively.
local s = seen or {}
local res = {}
s[obj] = res
for k, v in next, obj do
res[copy3(k, s)] = copy3(v, s)
end
return setmetatable(res, getmetatable(obj))
end

function Snippet:copy()
return copy3(self)
return util.copy3(self)
end

function Snippet:del_marks()
Expand Down
31 changes: 29 additions & 2 deletions lua/luasnip/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ end
--- by.
---@param parent_indent_displaycolumns number, displaycolumn this text is
--- already at.
---@return string[], `text` (only for simple nesting).
---@return string[] _ `text` (only for simple nesting).
local function expand_tabs(text, tabwidth, parent_indent_displaycolumns)
for i, line in ipairs(text) do
local new_line = ""
Expand Down Expand Up @@ -246,7 +246,7 @@ local function to_line_table(table_or_string)
-- split entries at \n.
local line_table = {}
for _, str in ipairs(tbl) do
local split = vim.split(str, "\n", true)
local split = vim.split(str, "\n", { plain = true })
for i = 1, #split do
line_table[#line_table + 1] = split[i]
end
Expand Down Expand Up @@ -444,6 +444,32 @@ local function shallow_copy(t)
return t
end

--- Deepcopy given table, with support for recursive tables & metatable.
--- Taken from: https://gist.github.com/tylerneylon/81333721109155b2d244
---
---@generic T: table
---@param obj T
---@param seen? table
---@return T
local function copy3(obj, seen)
-- Handle non-tables and previously-seen tables.
if type(obj) ~= "table" then
return obj
end
if seen and seen[obj] then
return seen[obj]
end

-- New table; mark it as seen an copy recursively.
local s = seen or {}
local res = {}
s[obj] = res
for k, v in next, obj do
res[copy3(k, s)] = copy3(v, s)
end
return setmetatable(res, getmetatable(obj))
end

return {
get_cursor_0ind = get_cursor_0ind,
set_cursor_0ind = set_cursor_0ind,
Expand Down Expand Up @@ -489,4 +515,5 @@ return {
pos_offset = pos_offset,
pos_from_offset = pos_from_offset,
shallow_copy = shallow_copy,
copy3 = copy3,
}