Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Apply easy suggestions (part2)
  • Loading branch information
bew committed Nov 8, 2025
commit 02cabf1378f3d49146e4add6ddfaa8d0324ea2fb
2 changes: 1 addition & 1 deletion lua/luasnip/extras/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ return {
--- s("extras4", { i(1), t { "", "" }, extras.rep(1) })
--- ```
---
---@param node_ref LuaSnip.NodeRef a single [Node Reference](#node-reference).
---@param node_ref LuaSnip.NodeRef a single [Node Reference](../../../DOC.md#node-reference).
---@return LuaSnip.FunctionNode
rep = function(node_ref)
return F(function(args)
Expand Down
2 changes: 1 addition & 1 deletion lua/luasnip/nodes/choiceNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function ChoiceNode:subsnip_init()
choice.parent = self.parent
-- only insertNode needs this. (?)
if
vim.list_contains(
util.list_contains(
{ types.textNode, types.insertNode, types.functionNode },
choice.type
)
Expand Down
13 changes: 4 additions & 9 deletions lua/luasnip/nodes/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local describe = require("luasnip.util.log").describe
---@field key? any Key to identify the node with.
---@field node_ext_opts LuaSnip.NodeExtOpts
---@field merge_node_ext_opts boolean
---@field node_callbacks {["enter"|"leave"]: fun(node:LuaSnip.Node)}
---@field node_callbacks {[LuaSnip.EventType]: fun(node:LuaSnip.Node)}

---@class LuaSnip.Node: LuaSnip.NormalizedNodeOpts
---@field pos? integer Jump-index of the node
Expand Down Expand Up @@ -254,21 +254,16 @@ end

---@param event LuaSnip.EventType
function Node:event(event)
-- FIXME(@bew): wrong type for node_callbacks ?
-- We index witha EventType(integer), but the field definition uses string
-- keys "enter"/"leave" πŸ€”
local node_callback = self.node_callbacks[event]
if node_callback then
node_callback(self)
end

-- try to get the callback from the parent.
if self.pos then
-- node needs position to get callback (nodes may not have position if
-- defined in a choiceNode, ie. c(1, {
-- i(nil, {"works!"})
-- }))
-- works just fine.
-- The node needs position to get callback
-- (nodes may not have position if defined in a choiceNode)
-- ie. `c(1, { i(nil, {"works!"}) }))` works just fine.
local parent_callback = self.parent.callbacks[self.pos][event]
if parent_callback then
parent_callback(self)
Expand Down
22 changes: 11 additions & 11 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ local callbacks_mt = {
-- declare SN here, is needed in metatable.
local SN

local stored_mt = {
__index = function(table, key)
-- default-node is just empty text.
local val = SN(nil, { iNode.I(1) })
val.is_default = true
rawset(table, key, val)
return val
end,
}

---@class LuaSnip.BareInternalSnippet: LuaSnip.Node
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite get at what point a snippet is a BareInternalSnippet.. In my mind there is Addable for things that can be put into add_snippets, Expandable for stuff that has :matches and condition and the like, and then there is ExpandedSnippet for when the snippet is expanded.

As far as I remember snippet, dependents_dict, child_snippets, static_text, and indentstr are only used by snippets once they are expanded, maybe ExpandedSnippet is a better place for them? (And maybe it'd be a good idea to initialize them in trigger_expand?)

Copy link
Contributor Author

@bew bew Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to make something for the return type of _S, which is used in different places for defining different types.
And it's made to have the common fields for all other classes that are derived from it.

It's used as the common base for Snippet & SnippetNode:

  • ---@class LuaSnip.SnippetNode: LuaSnip.BareInternalSnippet, LuaSnip.NormalizedSnippetNodeOpts
  • ---@class LuaSnip.Snippet: LuaSnip.BareInternalSnippet, LuaSnip.NormalizedSnippetContext, LuaSnip.NormalizedSnippetOpts, LuaSnip.Addable

Those two don't share all the same fields nor the same parent classes, so we need a kind of base class for all their common fields πŸ€”


As far as I remember snippet, dependents_dict, child_snippets, static_text, and indentstr are only used by snippets once they are expanded, maybe ExpandedSnippet is a better place for them? (And maybe it'd be a good idea to initialize them in trigger_expand?)

Interesting, I'll have to explore that!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to make something for the return type of _S, which is used in different places for defining different types.
And it's made to have the common fields for all other classes that are derived from it.

It's used as the common base for Snippet & SnippetNode

Ahh I see.. this seems fine πŸ‘
So far I'd thought of snippet as a specialization of snippetNode, but it's possible that, rigourously, there are some conflicts there πŸ˜…

--- To be used as a base for all snippet-like nodes (Snippet, SnippetProxy, ..)
---
Expand Down Expand Up @@ -238,6 +228,16 @@ local function init_snippetNode_opts(opts)
return in_node
end

local stored_mt = {
__index = function(table, key)
-- default-node is just empty text.
local val = SN(nil, { iNode.I(1) })
val.is_default = true
rawset(table, key, val)
return val
end,
}

---@param opts LuaSnip.Opts.Snippet
---@return LuaSnip.NormalizedSnippetOpts
local function init_snippet_opts(opts)
Expand Down Expand Up @@ -628,7 +628,7 @@ end
--- This overrides the filetype the snippet is added (via `add_snippet`) as.

---@class LuaSnip.Opts.Snippet: LuaSnip.Opts.SnippetNode
---@field stored? {[string]: LuaSnip.Node}
---@field stored? {[string]: LuaSnip.Node} Snippet-level state for restore node.

---@param context string|LuaSnip.SnipContext The snippet context.
--- Passing a string is equivalent to passing `{ trig = <the string> }`.
Expand Down
3 changes: 3 additions & 0 deletions lua/luasnip/util/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ local EventType = {
}

local M = setmetatable({}, { __index = EventType })
-- NOTE: The metatable is set so that callers of this `events` module can do
-- `events.change_choice` to get a named value from the enum, while leaving the
-- enum definition standalone to avoid adding unnecessary enum fields.

---@param node_type LuaSnip.NodeType
---@param event_id LuaSnip.EventType
Expand Down
3 changes: 3 additions & 0 deletions lua/luasnip/util/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ local NodeType = {
}

local M = setmetatable({}, { __index = NodeType })
-- NOTE: The metatable is set so that callers of this `types` module can do
-- `types.insertNode` to get a named value from the enum, while leaving the
-- enum definition standalone to avoid adding unnecessary enum fields.

local refs = {
{ value = NodeType.textNode, name = "textNode", pascal_name = "TextNode" },
Expand Down
16 changes: 16 additions & 0 deletions lua/luasnip/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,21 @@ local function copy3(obj, seen)
return setmetatable(res, getmetatable(obj))
end

--- Checks if a list-like table (integer keys without gaps) contains `needle`.
---@param t table Table to check (must be list-like, not validated)
---@param needle any Value to compare
---@return boolean _ `true` if `t` contains `needle`
local function list_contains(t, needle)
vim.validate('t', t, 'table')

for _, v in ipairs(t) do
if v == needle then
return true
end
end
return false
end

return {
get_cursor_0ind = get_cursor_0ind,
set_cursor_0ind = set_cursor_0ind,
Expand Down Expand Up @@ -525,4 +540,5 @@ return {
pos_from_offset = pos_from_offset,
shallow_copy = shallow_copy,
copy3 = copy3,
list_contains = list_contains,
}
Loading