Skip to content

Commit

Permalink
Factor out arg-generation for lambda+dynLambdaNodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 committed Sep 5, 2021
1 parent 0bb95a2 commit 30cc695
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions lua/luasnip/extras/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,43 @@ local function _concat(lines)
return table.concat(lines, "\n")
end

local function make_lambda_args(node_args)
local snip = node_args[#node_args]
-- remove snippet before concatenation.
node_args[#node_args] = nil
local args = vim.tbl_map(_concat, node_args)

setmetatable(args, {
__index = function(table, key)
Insp(key)
print(debug.traceback())
local val
-- key may be capture or env-variable.
local num = key:match("CAPTURE(%d+)")
if num then
val = snip.captures[tonumber(num)]
else
-- env may be string or table.
if type(snip.env[key] == "table") then
val = _concat(snip.env[key])
else
val = snip.env[key]
end
end
rawset(table, key, val)
return val
end
})
return args
end

local function expr_to_fn(expr)
local _lambda = require("luasnip.extras._lambda")

local fn_code = _lambda.instantiate(expr)
local function fn(args)
local inputs = vim.tbl_map(_concat, args)
-- last is snippet, makes no sense to contain in there.
inputs[#inputs] = nil
setmetatable(inputs, {
__index = function(table, key)
local val
-- key may be capture or env-variable.
local num = key:match("CAPTURE(%d+)")
if num then
val = args[#args].captures[tonumber(num)]
else
val = args[#args].env[key]
end
rawset(table, key, val)
return val
end,
})
-- to be sure, lambda may end with a `match` returning nil.
local out = fn_code(inputs) or ""
local out = fn_code(make_lambda_args(args)) or ""
return vim.split(out, "\n")
end
return fn
Expand Down Expand Up @@ -127,11 +140,9 @@ return {
end,
dynamic_lambda = function(pos, lambd, args_indcs)
local insert_preset_text_func = lambda.instantiate(lambd)
return D(pos, function(args_text)
-- \n-concat lines from each node.
local inputs = vim.tbl_map(_concat, args_text)
return D(pos, function(args)
-- to be sure, lambda may end with a `match` returning nil.
local out = insert_preset_text_func(unpack(inputs)) or ""
local out = insert_preset_text_func(make_lambda_args(args)) or ""
return SN(pos, {
I(1, vim.split(out, "\n")),
})
Expand Down

0 comments on commit 30cc695

Please sign in to comment.