[mini.snippets] Context aware global snippets #2234
Replies: 1 comment 2 replies
-
|
That's some very nice ideas! Thanks for sharing! This is also related to #1225 and is something I'd recommend doing. One unfortunate drawback of using snippets here is inability to pre-align text in boxes (like for "TITLE" in the I'd also suggest adjusting couple of things:
So all in all here is something I'd do: local author = vim.fn.executable('git') == 1 and vim.fn.system('git config user.name'):gsub('%s+$', '')
or vim.uv.os_get_passwd().username
local special_comments = function(context)
local cs_left, cs_right = vim.bo[context.buf_id].commentstring:match('^(.*)%%s(.*)$')
if cs_left == nil then return {} end
-- Optional: adjust comment parts to always have inner padding
cs_left = cs_left:find('%s$') and cs_left or (cs_left .. ' ')
cs_right = cs_left:find('^%s') and cs_right or (' ' .. cs_right)
local wrap = function(s)
-- Optional: make it work nicer with blank lines
if s:match('^%s*$') then return (cs_left:gsub('%s*$', '') .. cs_right:gsub('^%s*', '')) end
return (string.format('%s%s%s', cs_left, s, cs_right):gsub('%s$', ''))
end
--stylua: ignore
return {
{
{ prefix = "fixme", body = wrap("FIXME ${0:describe the bug or needed fix}"), desc = "Create a fixme comment" },
{ prefix = "hack", body = wrap("HACK ${0:temporary workaround or solution}"), desc = "Create a hack comment" },
{ prefix = "info", body = wrap("INFO ${0:relevant context or metadata}"), desc = "Create a info comment" },
{ prefix = "link", body = wrap("LINK ${0:href to issue or documentation}"), desc = "Create a link comment" },
{ prefix = "note", body = wrap("NOTE ${0:important detail}"), desc = "Create a note comment" },
{ prefix = "todo", body = wrap("TODO ${0:describe the task or goal}"), desc = "Create a todo comment" },
{ prefix = "warn", body = wrap("WARN ${0:potential risk or edge case}"), desc = "Create a warn comment" },
{
prefix = "box",
body = {
wrap("┌──────────────────────────────────────┐"),
wrap("│ ${0:TITLE} │"),
wrap("└──────────────────────────────────────┘"),
},
desc = "Create a boxed comment",
},
{
prefix = "section",
body = wrap("── ${0:Section name} ─────────────────────────────"),
desc = "Create a section divider",
},
{
prefix = "header",
body = {
wrap("================================================"),
wrap(" " .. vim.fn.expand('%:t')),
wrap(""),
wrap("Description: ${0:What this file does}"),
wrap("Author: " .. author),
wrap("================================================"),
},
desc = "Create a file header",
},
},
}
end
return { special_comments } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I would like to share how to create context-aware global snippets using
mini.snippets.Demo
mini_snippets_context_demo.mp4
Example
mini.snippetssetup:Example
snippets/global.luafile:Would love to hear if anyone else has fun ideas for context-aware snippets
Beta Was this translation helpful? Give feedback.
All reactions