[Q] How to lazy init time a snippet?
salkin-mada opened this issue · comments
Hi snippet folks!
I would like to pass a number/string to a function called in a snippet. But within the snippet. So to say. Hope this makes sense.
Code is maybe clearer than me trying to explain what I want.
local format = string.format
F = {}
function F.randf_array(num)
local list = ""
local randnum = math.random(3,8)
if num == nil then num = randnum end
for i=1,num,1 do
list = list .. tostring(math.random())
if i < num then list = list .. ", " end
end
return format("[%s]", list)
end
snippets = {
_global = {
some = [[Some(${1|F.randf_array()}, ${2:inf})]]; -- fine, creating random size of array (3-8 random floats)
fruit = [[Fruit(${1|F.randf_array(5)}, ${2:inf})]]; -- fine, creating array of size 5 with random floats
-- here comes the pseudo code
other = [[Other(${1|F.randf_array(${-1=vim.fn.input("how many random floats would you like? ")})}, ${2:inf})]]; -- not ok, failing
};
}
I wonder how to do the thing I sketch out in the snippet other
?
And not necessarily with vim.fn.input
I would rather just write ${1(require'snippets'.u.lazy_magic):20}
.. :)
Something that makes the snippet wait until the value for the randf_array(num)
function has been entered.
Preferably with an UX (user experience) similar to if it had been the first $
entry in the snippet (eg. $1)
any help much appreciated.
Okay so I tried with repeat until
and it does work.
However I am being prompted several times to enter my value (number/integer) before returning to the snippet and then being able to expand again. AND then the process starts over. But this time around I am only prompted two times.
The second time I enter a number (this second time of expanding/advancing the snippet) the function actually behaves as expected.
I end up with a list/array of random floats with the size of the number inputted via vim.fn.input
.
Any idea why this is?
For testing/reproducing use the floaty ux inserter.
local format = string.format
F = {}
function F.randf_array_query()
local list = ""
local n
repeat
n = vim.fn.input("how many random floats? ")
if tonumber(n) == nil then
print ('enter a number')
end
until type(tonumber(n)) == "number"
-- until tonumber(n) ~= nil
for i=1,tonumber(n),1 do
list = list .. tostring(math.random())
if i < tonumber(n) then list = list .. ", " end
end
return format("[%s]", list)
end
snippets = {
_global = {
flist = "${1|F.randf_array_query()}";
};
}
Interesting enough, If using this as the snippet flist = "${|F.randf_array_query()}";
The input is only prompting three times and then returning the array of floats.
same "three times"-behavior with a while do loop
version.
function F.randf_array_query()
local list = ""
local n
while tonumber(n)==nil do
n = vim.fn.input("how many random floats? ")
end
for i=1,tonumber(n),1 do
list = list .. tostring(math.random())
if i < tonumber(n) then list = list .. ", " end
end
return format("[%s]", list)
end
ahhh.. (S.v)
.. slowly learning lua
here :<)
yepp
function F.randfs(n)
local list = ""
n = tonumber(n) or 0
if n == 0 then
return ""
end
for i=1,tonumber(n),1 do
list = list .. tostring(math.random())
if i < tonumber(n) then list = list .. ", " end
end
return format("[%s]", list)
end
snippet: flist = "${1|F.randfs(S.v)}";