-
Notifications
You must be signed in to change notification settings - Fork 4
Snippets
Snippets API is available in v0.10+
Integration with a snippet engine, e.g. LuaSnip can amplify your checkmate.nvim workflow.
Instead of having to manually create write combinations of text and metadata that you may use often, you can define snippets to quickly insert 'templated' todo items, saving keystrokes and maintaining consistency.
Without snippets (multiple steps):
- Create todo:
- □
- Type description
- Add priority:
@priority(high)
- Add assignee:
@assigned(john)
With snippets (one step):
- Type:
.task
- Get:
- □ Task: |cursor| @priority(medium) @assigned(me)
You can create additional snippets for features, bugs, notes/topics, etc. The possibilities are endless!
Follow the LuaSnip documentation for loading/adding snippets. Remember, these can also be stored in separate files and loaded en bloc (see Lua loader).
Here's a minimal example:
local ls = require("luasnip")
ls.add_snippets("markdown", {
-- Adds a simple text node with regular Markdown checkbox (checkmate.nvim
-- will automatically convert this upon leaving insert mode)
ls.s({ trig = ".todo", desc = "Basic Todo" }, { ls.t("- [ ] New TODO") }),
})
checkmate.nvim exposes a snippet API to reduce some boilerplate of creating todo snippets. See checkmate.snippets
.
Returns a LuaSnip snippet that expands to a custom todo string
See checkmate.TodoSnippetOpts
for full opts
, but includes:
-
trigger
(string) - the string used as the LuaSniptrig
-
text?
(string) - default string to add (before any metadata). Will fallback to usedesc
if nil. -
metadata?
(table) - where eachkey
is a metadata tag/name and thevalue
is one of the following (seecheckmate.MetadataSnippetType
) which determines how the metadata's default value is obtained:-
boolean
Will place a text node with the metadata'sget_value()
returning the string -
string
Will place a text node with this string as the node text -
number
Will place an insert node at this position. See InsertNode. -
function(captures: any): string
Will place a text node based on the return of this function. Captures are returned when the trigger is interpreted as a Lua pattern with capture groups. SeetrigEngine
in LuaSnip documentation.
-
-
ls_context?
(table) - thecontext
table passed to LuaSnip snippet. Will overridetrigger
anddesc
above, if defined. -
ls_opts?
(table) - theopts
table passed to LuaSnip snippet.
local cms = require("checkmate.snippets")
local ls = require("luasnip")
ls.add_snippets("markdown", {
-- use checkmate.snippet's todo helper
cms.todo({
trigger = ".bug",
text = "New BUG",
metadata = {
bug = "",
priority = true,
},
ls_context = {
snippetType = "autosnippet",
},
}),
})
With this setup, you can now type ".bug" can get the following behavior:
- LuaSnip expansion to
- □ New BUG @bug() @priority(medium)
- The
@bug
tag's value is set as empty and the@priority
tag's value is computed from itsget_value
function.
local cms = require("checkmate.snippets")
local ls = require("luasnip")
ls.add_snippets("markdown", {
cms.todo({
trigger = "%.i(%d+)", -- ".i" followed by a number (captured)
desc = "New ISSUE",
metadata = {
issue = function(captures)
local issue_num = captures[1] or ""
return "#" .. issue_num
end,
},
ls_context = {
snippetType = "snippet",
regTrig = true,
},
}),
}
With this setup, you can now type ".i45" can get the following behavior:
- LuaSnip expansion to
- □ New ISSUE @issue(#45)
- The
@issue
tag obtains its value from the capture group from theregTrig
trigger
Returns a LuaSnip snippet that expands to a metadata @tag(value)
string
See checkmate.MetadataSnippetOpts
for full opts
, but includes:
-
tag
(string) - Metadata tag name -
value?
(string) - Metadata value to insert. If nil, will use the metadata'sget_value
function -
auto_select?
- Selects the metadata's value on insert. Otherwise, moves cursor to the end. Default: false.
local cms = require("checkmate.snippets")
local ls = require("luasnip")
ls.add_snippets("markdown", {
cms.metadata({
trigger = "@p",
tag = "priority",
desc = "@priority",
auto_select = true,
ls_context = { snippetType = "autosnippet" }
})
})
Will insert a @priority(<default>)
tag where <default>
is the value obtained from the get_value
function, or ""
.
local cms = require("checkmate.snippets")
cms.todo({
trigger = "%.i(%d+)",
desc = "New ISSUE",
metadata = {
issue = function(captures)
local issue_num = captures[1] or ""
return "#" .. issue_num
end,
url = function(captures)
local repo = vim.fn
.system("git remote get-url origin")
:gsub("\n", "")
:gsub("%.git$", "")
:gsub("^.*:", "https://github.com/")
return string.format("%s/issues/%s", repo, captures[1])
end,
},
ls_context = {
snippetType = "snippet",
regTrig = true,
},
})
Expanding ".i500"
will result in:
- □ New ISSUE @url(https://github.com/bngarren/checkmate.nvim/issues/500) @issue(#500)