-
Notifications
You must be signed in to change notification settings - Fork 6
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 usedescif nil. -
metadata?(table) - where eachkeyis a metadata tag/name and thevalueis one of the following (seecheckmate.MetadataSnippetType) which determines how the metadata's default value is obtained:-
booleanWill place a text node with the metadata'sget_value()returning the string -
stringWill place a text node with this string as the node text -
numberWill place an insert node at this position. See InsertNode. -
function(captures: any): stringWill 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. SeetrigEnginein LuaSnip documentation.
-
-
ls_context?(table) - thecontexttable passed to LuaSnip snippet. Will overridetriggeranddescabove, if defined. -
ls_opts?(table) - theoptstable 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
@bugtag's value is set as empty and the@prioritytag's value is computed from itsget_valuefunction.
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
@issuetag obtains its value from the capture group from theregTrigtrigger
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_valuefunction -
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)