add declaration snippet (c)
This commit is contained in:
@ -1,13 +1,12 @@
|
|||||||
luasnip = require('luasnip')
|
ls = require('luasnip')
|
||||||
|
s = ls.snippet
|
||||||
s = luasnip.snippet
|
sn = ls.snippet_node
|
||||||
sn = luasnip.snippet_node
|
t = ls.text_node
|
||||||
t = luasnip.text_node
|
i = ls.insert_node
|
||||||
i = luasnip.insert_node
|
f = ls.function_node
|
||||||
f = luasnip.function_node
|
c = ls.choice_node
|
||||||
c = luasnip.choice_node
|
d = ls.dynamic_node
|
||||||
d = luasnip.dynamic_node
|
r = ls.restore_node
|
||||||
r = luasnip.restore_node
|
|
||||||
l = require("luasnip.extras").lambda
|
l = require("luasnip.extras").lambda
|
||||||
rep = require("luasnip.extras").rep
|
rep = require("luasnip.extras").rep
|
||||||
p = require("luasnip.extras").partial
|
p = require("luasnip.extras").partial
|
||||||
@ -20,3 +19,5 @@ types = require("luasnip.util.types")
|
|||||||
conds = require("luasnip.extras.conditions")
|
conds = require("luasnip.extras.conditions")
|
||||||
conds_expand = require("luasnip.extras.conditions.expand")
|
conds_expand = require("luasnip.extras.conditions.expand")
|
||||||
ts_postfix = require("luasnip.extras.treesitter_postfix").treesitter_postfix
|
ts_postfix = require("luasnip.extras.treesitter_postfix").treesitter_postfix
|
||||||
|
postfix = require("luasnip.extras.postfix").postfix
|
||||||
|
ms = ls.multi_snippet
|
||||||
|
@ -1,23 +1,43 @@
|
|||||||
require('core.snippets.shorthands')
|
require('core.snippets.shorthands')
|
||||||
|
|
||||||
return {
|
--- create a decleration of a function from it's definition using treesitter
|
||||||
-- method snippet
|
---@param func string
|
||||||
s("main", {
|
---@return string|nil
|
||||||
c(1, {
|
local function c_func(func)
|
||||||
t(""),
|
local tree = vim.treesitter.get_parser():parse()[1]:root()
|
||||||
t("static "),
|
|
||||||
}),
|
|
||||||
t("int main("),
|
|
||||||
c(2, {
|
|
||||||
t("int argc, char *argv[]"),
|
|
||||||
i(1, "void"),
|
|
||||||
}),
|
|
||||||
t(")"),
|
|
||||||
t({ " {", "\t" }),
|
|
||||||
i(0),
|
|
||||||
t({ "", "}" }),
|
|
||||||
}),
|
|
||||||
|
|
||||||
|
local q = vim.treesitter.query.parse("c", [[
|
||||||
|
(function_definition
|
||||||
|
declarator: (function_declarator
|
||||||
|
(identifier) @function_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
]])
|
||||||
|
local matches = q:iter_matches(tree, 0)
|
||||||
|
|
||||||
|
for _, match in matches do
|
||||||
|
for _, node in pairs(match) do
|
||||||
|
if vim.treesitter.get_node_text(node, 0) == func then
|
||||||
|
while node:type() ~= "function_definition" do
|
||||||
|
node = node:parent()
|
||||||
|
end
|
||||||
|
local def = ""
|
||||||
|
for i = 0, node:child_count() - 2 do
|
||||||
|
def = def..vim.treesitter.get_node_text(node:child(i), 0)
|
||||||
|
if i < node:child_count() - 2 then
|
||||||
|
def = def.." "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Print the function name using node text
|
||||||
|
return def:gsub("\n", "")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
-- function snippet
|
-- function snippet
|
||||||
s({ trig = [[fn\|main]], trigEngine = "vim" }, {
|
s({ trig = [[fn\|main]], trigEngine = "vim" }, {
|
||||||
d(1, function(_, snip)
|
d(1, function(_, snip)
|
||||||
@ -57,5 +77,19 @@ return {
|
|||||||
i(0),
|
i(0),
|
||||||
t({ "", "}" })
|
t({ "", "}" })
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
-- create decleration based on existing c function
|
||||||
|
postfix(".d", {
|
||||||
|
f(function(_, parent)
|
||||||
|
local r = c_func(parent.snippet.env.POSTFIX_MATCH)
|
||||||
|
if not r then
|
||||||
|
return parent.snippet.env.POSTFIX_MATCH
|
||||||
|
end
|
||||||
|
local bidx, aidx = r:find(parent.snippet.env.POSTFIX_MATCH)
|
||||||
|
|
||||||
|
return r:sub(1, bidx - 1)..
|
||||||
|
parent.snippet.env.POSTFIX_MATCH..
|
||||||
|
r:sub(aidx + 1)..";"
|
||||||
|
end, {})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user