Skip to content

Instantly share code, notes, and snippets.

@gbishop
Created November 22, 2022 12:52
Show Gist options
  • Save gbishop/6c8190fa7c3983df5a4e3d14ae3d4e19 to your computer and use it in GitHub Desktop.
Save gbishop/6c8190fa7c3983df5a4e3d14ae3d4e19 to your computer and use it in GitHub Desktop.
Live jsdoc for luasnip - hacked from their demo
-- experiment with live jsdoc
local ls = require("luasnip")
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
local i = ls.insert_node
local d = ls.dynamic_node
local r = ls.restore_node
local c = ls.choice_node
-- from https://github.com/L3MON4D3/LuaSnip/blob/master/Examples/snippets.lua#L81-L166
-- hacked up for javascript and jsdoc
local function jsdoc(args)
local pstring = vim.trim(args[1][1])
local nodes = {
t({ "/**", " * " }),
r(1, "desc", i(1, "A short Description")),
t({ "", "" }),
}
-- At least one param.
if #pstring > 0 then
vim.list_extend(nodes, { t({ " * ", "" }) })
end
local insert = 2
for arg in string.gmatch(pstring, '%w+') do
local inode = r(insert, "arg" .. arg, i(1, "any"))
vim.list_extend(nodes, { t({ " * @param {" }), inode, t({ "} " .. arg .. " ", "" }) })
insert = insert + 1
end
vim.list_extend(nodes,
{
t({ " * @returns {" }),
r(insert, "returns", i(1, "void")),
t({ "} ", ""})
})
vim.list_extend(nodes, { t({ " */" }) })
local snip = sn(nil, nodes)
return snip
end
return {
-- function with jsdoc
s("jf", {
d(4, jsdoc, 3), -- generate the jsdoc
t({ "", "" }),
c(1, { -- choose function or method
t({ "function "}),
t({ "" })
}),
i(2, "name"), -- give it a name
t("("),
i(3), -- parameters
t(")"),
t({ " {", "\t" }),
i(0), -- body
t({ "", "}" }),
}),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment