-
I started to migrate from Ultisnips to LuaSnip - thanks a lot for this awesome, incredibly well thought out and amazingly powerful snippet engine that even supports multiple source formats (snipmate, vscode, Lua ❤️ ...). Converting the snipmate snippets was extremely easy I only struggle with one last bit that puzzles me and is most probably due to a lack of understanding on my side. Short: I wanted to create a snippet for my c/c++ header include guards so I created a local function get_guard_name ()
local name = vim.fn.expand ('%:.'):lower ()
for _, item in ipairs ({'src', 'sources', 'build'}) do
name = name:gsub (item .. '/', '')
end
return name:gsub ('[/\\.-]', '_'):upper() .. '_INCLUDED'
end
local function guard_node ()
return sn (nil, fmt ([[
#ifndef {}
#define {}
{}
#endif // NOT {}
]], {i (1, get_guard_name ()), rep (1), i (2), rep (1)}))
end
return {s ({trig = 'incguard', desc = 'Insert include guard'}, {d (1, guard_node)})} This works nicely but I need to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I obviously overlooked the following statement in the docs about node references:
And since the dynamic node is not the root it cannot access its index. The following works as expected: local function guard_name_node () return sn (nil, {i (1, get_guard_name ())}) end
return {
s ({trig = 'incguard', desc = 'Insert include guard'}, fmt ([[
#ifndef {}
#define {}
{}
#endif // NOT {}
]], {d (1, guard_name_node), rep (1), i (0), rep (1)}))
} |
Beta Was this translation helpful? Give feedback.
I obviously overlooked the following statement in the docs about node references:
And since the dynamic node is not the root it cannot access its index. The following works as expected: