Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
90ef60a
refactor: put all api-function into their own class.
L3MON4D3 Jun 17, 2025
af1420c
Add more documentation.
L3MON4D3 Jun 17, 2025
91dddd0
doc: add code for generating docstrings from annotations.
L3MON4D3 Jun 17, 2025
c314824
gitignore build artifacts from doc.
L3MON4D3 Jun 17, 2025
b7abead
finish doc for init.lua, document ChoiceNode.C, update luals-mdgen.
L3MON4D3 Jun 18, 2025
8b327ce
remove doc.json, is build artifact.
L3MON4D3 Jun 18, 2025
79e7ab1
ci: add DOC.md-generation to push workflow.
L3MON4D3 Jun 18, 2025
aec181e
whoops!
L3MON4D3 Jun 18, 2025
f34a4ac
remove old comment.
L3MON4D3 Jun 19, 2025
8605dc1
nix: update luals-mdgen.
L3MON4D3 Jun 19, 2025
58b431d
make sure there are empty lines before any lists.
L3MON4D3 Jun 19, 2025
0d86256
update dictionary.
L3MON4D3 Jun 19, 2025
6948930
ci: use aspell directly for running spellcheck.
L3MON4D3 Jun 19, 2025
5130823
move DOC-template.md into ./data/.
L3MON4D3 Jun 19, 2025
ea7ba6d
doc: specify special-case of textNode as a choice.
L3MON4D3 Jun 20, 2025
7113cfe
ci: fix spell workflow.
L3MON4D3 Jun 20, 2025
c334ff1
update luals-mdgen.
L3MON4D3 Jun 20, 2025
678b5bf
spellcheck: set locale-dependent encoding, fix spellcheck_interactive.
L3MON4D3 Jun 20, 2025
0b37dca
Auto generate docs
L3MON4D3 Jun 20, 2025
592a634
Format with stylua
L3MON4D3 Jun 20, 2025
9659b9d
doc: add panvimdoc and vimdoc make-step, use in ci.
L3MON4D3 Jun 22, 2025
a3e75ee
re-generate doc.
L3MON4D3 Jun 22, 2025
9611fc1
doc: don't be too verbose about return-values.
L3MON4D3 Jun 22, 2025
964cff2
doc: add newlines in long doc-blocks.
L3MON4D3 Jun 22, 2025
338d9a9
Auto generate docs
L3MON4D3 Jun 22, 2025
6e9fc6d
doc: s/--/---/
L3MON4D3 Jun 22, 2025
357800b
doc: make sure class-doc is above @class.
L3MON4D3 Jun 22, 2025
e15ad06
annotations: replace table<x,y> with {[x]: y}.
L3MON4D3 Jun 22, 2025
5857a31
annotations: make type a bit more strict.
L3MON4D3 Jun 22, 2025
407ebb9
regenerate doc.
L3MON4D3 Jun 22, 2025
ac1ee19
update helptags after updating vimdoc.
L3MON4D3 Jun 22, 2025
06e6084
use panvimdoc-fork.
L3MON4D3 Jun 22, 2025
cdeed19
annotations: add return-type for get_snip_env.
L3MON4D3 Jun 22, 2025
11a250d
generate api-doc as subsubsubheadings.
L3MON4D3 Jun 22, 2025
8484341
panvimdoc: drop unnecessary flag.
L3MON4D3 Jun 22, 2025
b9e6223
doc: generate DOC.md once with panvimdoc-links, once without.
L3MON4D3 Jun 22, 2025
bec3c1b
doc: add empty line before list.
L3MON4D3 Jun 22, 2025
0c90008
doc: generate documentation before running luals-mdgen.
L3MON4D3 Jun 23, 2025
c49fdb2
doc: update luals-mdgen.
L3MON4D3 Jun 23, 2025
4bf7206
LuaSnip.Choice -> LuaSnip.ChoiceNode.ItemNode.
L3MON4D3 Jun 23, 2025
da6eee4
Few annotations tweaks
bew Jun 23, 2025
e0b81c1
Include changes for the other comments on #1353
bew Jun 23, 2025
4bfc49c
Apply suggestions
bew Jun 23, 2025
62c62e9
Add in-comment indent in doc annotations for top-doc & line continuation
bew Jun 23, 2025
ae20fa1
re-generate doc.
L3MON4D3 Jun 23, 2025
f5cdf05
add missing annotations.
L3MON4D3 Jun 23, 2025
707f89e
update luals-mdgen.
L3MON4D3 Jun 23, 2025
b0fdce2
add one last missing parameter.
L3MON4D3 Jun 23, 2025
3ccbb4f
shuffle around choiceNode doc.
L3MON4D3 Jun 24, 2025
a70f977
update luals-mdgen.
L3MON4D3 Jun 24, 2025
a47e5aa
make sure links are formatted correctly.
L3MON4D3 Jun 24, 2025
cd0f3c4
add missing newline before list.
L3MON4D3 Jun 24, 2025
5be06e2
update panvimdoc.
L3MON4D3 Jun 24, 2025
68faade
upate luals-mdgen.
L3MON4D3 Jun 24, 2025
fe21c82
update luals-mdgen.
L3MON4D3 Jun 24, 2025
fb4b3fa
update panvimdoc.
L3MON4D3 Jun 25, 2025
d8cee89
doc: briefly expand how to create a `LuaSnip.Addable`.
L3MON4D3 Jun 25, 2025
fbbd855
doc: add missing explanation for clean_invalidated-opts.
L3MON4D3 Jun 25, 2025
c9f081a
update panvimdoc.
L3MON4D3 Jun 25, 2025
75baad0
doc: render vimdoc-markdown with textwidth 80.
L3MON4D3 Jun 25, 2025
bd39313
doc: remove conflicting `LuaSnip.Snippet` definition.
L3MON4D3 Jun 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more documentation.
  • Loading branch information
L3MON4D3 committed Jun 20, 2025
commit af1420cf7eb542a19b6fe37c78d12f3970336a07
12 changes: 11 additions & 1 deletion lua/luasnip/_types.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
---@alias LuaSnip.Cursor {[1]: number, [2]: number}

---@class LuaSnip.MatchRegion 0-based region
---@class LuaSnip.MatchRegion 0-based region within a single line
---@field row integer 0-based row
---@field col_range { [1]: integer, [2]: integer } 0-based column range, from-in, to-exclusive

---@class LuaSnip.BufferRegion 0-based region in a buffer.
---@field from [integer, integer] row,col-tuple, usually considered within the
---range.
---@field to [integer, integer] row,col, usually considered one column beyond
---the range.

---@alias LuaSnip.Addable table
---Anything that can be passed to ls.add_snippets().

-- very approximate classes, for now.
---@alias LuaSnip.Snippet table
---@alias LuaSnip.Node table
109 changes: 108 additions & 1 deletion lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ local log = require("luasnip.util.log").new("main")
---@class LuaSnip.API
local API = {}

---Get the currently active snippet.
---@return LuaSnip.Snippet? active_snip The active snippet if one exists,
---otherwise `nil`.
function API.get_active_snip()
local node = session.current_nodes[vim.api.nvim_get_current_buf()]
if not node then
Expand Down Expand Up @@ -53,6 +56,17 @@ end
-- opts: optional args, can contain `type`, either "snippets" or "autosnippets".
--
-- return table, may be empty.

---@class LuaSnip.Opts.GetSnippets
---@field type? "snippets"|"autosnippets" Whether to get snippets or
---autosnippets. Defaults to "snippets".

---Retrieve snippets from luasnip.
---@param ft string? Filetype, if not given returns snippets for all filetypes.
---@param opts LuaSnip.Opts.GetSnippets? Optional arguments.
---@return LuaSnip.Snippet[]|table<string, LuaSnip.Snippet[]> snippets Flat
---array when `ft` is non-nil, otherwise a table mapping filetypes to
---snippets.
function API.get_snippets(ft, opts)
opts = opts or {}

Expand All @@ -69,6 +83,25 @@ local function default_snip_info(snip)
}
end

---Retrieve information about snippets available in the current file/at the
---current position (in case treesitter-based filetypes are enabled).
---@generic T
---@param snip_info (fun(LuaSnip.Snippet): T)? Optionally pass a function that,
---given a snippet, returns the data that is returned by this function in the
---snippets' stead. By default, this function is
---```lua
---function(snip)
--- return {
--- name = snip.name,
--- trigger = snip.trigger,
--- description = snip.description,
--- wordTrig = snip.wordTrig and true or false,
--- regTrig = snip.regTrig and true or false,
--- }
--end
---```
---@return table<string, T[]> available_info Table mapping filetypes to list of
---data returned by snip_info.
function API.available(snip_info)
snip_info = snip_info or default_snip_info

Expand Down Expand Up @@ -128,6 +161,9 @@ function unlink_set_adjacent_as_current(snippet, reason, ...)
unlink_set_adjacent_as_current_no_log(snippet)
end

---Removes the current snippet from the jumplist (useful if LuaSnip fails to
---automatically detect e.g. deletion of a snippet) and sets the current node
---behind the snippet, or, if not possible, before it.
function API.unlink_current()
local current = session.current_nodes[vim.api.nvim_get_current_buf()]
if not current then
Expand Down Expand Up @@ -159,6 +195,9 @@ local function safe_jump_current(dir, no_move, dry_run)
return session.current_nodes[vim.api.nvim_get_current_buf()]
end
end
---Jump forwards or backwards
---@param dir 1|-1 Jump forward for 1, backward for -1.
---@return boolean jumped `true` if a jump was performed, `false` otherwise.
function API.jump(dir)
local current = session.current_nodes[vim.api.nvim_get_current_buf()]
if current then
Expand All @@ -179,27 +218,48 @@ function API.jump(dir)
return false
end
end
---Find the node the next jump will end up at. This will not work always,
---because we will not update the node before jumping, so if the jump would eg.
---insert a new node between this node and its pre-update jump target, this
---would not be registered.
---Thus, it currently only works for simple cases.
---@param dir 1|-1 `1`: find the next node, `-1`: find the previous node.
---@return LuaSnip.Node target The destination.
function API.jump_destination(dir)
-- dry run of jump (+no_move ofc.), only retrieves destination-node.
return safe_jump_current(dir, true, { active = {} })
end

---Determine whether jumping forwards or backwards will actually jump, or if
---there is no node in that direction.
---@param dir 1|-1 `1` forward, `-1` backward.
---@return boolean is_jumpable
function API.jumpable(dir)
-- node is jumpable if there is a destination.
return API.jump_destination(dir)
~= session.current_nodes[vim.api.nvim_get_current_buf()]
end

---Determine whether there is an expandable snippet at the current cursor
---position. Does not consider autosnippets since those would already be
---expanded at this point.
---@return boolean is_expandable
function API.expandable()
next_expand, next_expand_params =
match_snippet(util.get_current_line_to_cursor(), "snippets")
return next_expand ~= nil
end

---Determines if it's possible to expand a snippet at the current
---cursor-position, or whether it's possible to jump forward from the current
---node.
---@return boolean is_expand_or_jumpable
function API.expand_or_jumpable()
return API.expandable() or API.jumpable(1)
end

---Determine whether the cursor is within a snippet.
---@return boolean is_inside
function API.in_snippet()
-- check if the cursor on a row inside a snippet.
local node = session.current_nodes[vim.api.nvim_get_current_buf()]
Expand All @@ -218,18 +278,26 @@ function API.in_snippet()
"Error while getting extmark-position: %s",
snip_begin_pos
)
return
return false
end
local pos = vim.api.nvim_win_get_cursor(0)
if pos[1] - 1 >= snip_begin_pos[1] and pos[1] - 1 <= snip_end_pos[1] then
return true -- cursor not on row inside snippet
end
return false
end

---Determine if a snippet can be expanded at the current cursor position, or
---whether the cursor is inside a snippet and the current node can be jumped
---forward from.
---@return boolean is_expand_or_locally_jumpable
function API.expand_or_locally_jumpable()
return API.expandable() or (API.in_snippet() and API.jumpable(1))
end

---Determine if whether the cursor is inside a snippet and the current node can
---be jumped forward from.
---@return boolean is_locally_jumpable
function API.locally_jumpable(dir)
return API.in_snippet() and API.jumpable(dir)
end
Expand All @@ -239,6 +307,45 @@ local function _jump_into_default(snippet)
end

-- opts.clear_region: table, keys `from` and `to`, both (0,0)-indexed.

---@class LuaSnip.Opts.SnipExpandExpandParams
---@field trigger string? What to set as the expanded snippets' trigger
---(Defaults to `snip.trigger`).
---@field captures string[]? Set as the expanded snippets' captures (Defaults to
---`{}`).
---@field env_override table<string, string>? Set or override environment
---variables of the expanded snippet (Defaults to `{}`).

---@class LuaSnip.Opts.SnipExpand
---@field clear_region? LuaSnip.BufferRegion A region of text to clear after
---populating env-variables, but before jumping into `snip`. If `nil`, no
---clearing is performed.
---Being able to remove text at this point is useful as clearing before calling
---this function would populate `TM_CURRENT_LINE` and `TM_CURRENT_WORD` with
---wrong values (they would miss the snippet trigger).
---The actual values used for clearing are `region.from` and `region.to`, both
---(0,0)-indexed byte-positions in the buffer.
---@field expand_params LuaSnip.Opts.SnipExpandExpandParams? Override various
---fields of the expanded snippet. Don't override anything by default.
---This is useful for manually expanding snippets where the trigger passed
---via `trig` is not the text triggering the snippet, or those which expect
---`captures` (basically, snippets with a non-plaintext `trigEngine`).
---
---One Example:
---```lua
---snip_expand(snip, {
--- trigger = "override_trigger",
--- captures = {"first capture", "second capture"},
--- env_override = { this_key = "some value", other_key = {"multiple", "lines"}, TM_FILENAME = "some_other_filename.lua" }
---})
---```
---@field pos [integer, integer]? Position at which the snippet should be
---inserted. Pass as `(row,col)`, both 0-based, the `col` given in bytes.

---Expand a snippet in the current buffer.
---@param snippet LuaSnip.Snippet The snippet.
---@param opts LuaSnip.Opts.SnipExpand? Optional additional arguments.
---@return [TODO:return]
function API.snip_expand(snippet, opts)
local snip = snippet:copy()

Expand Down