Skip to content

Commit

Permalink
BREAKING: Make last arg to snippet a table and remove varargs.
Browse files Browse the repository at this point in the history
Last arg table: More flexible, nicer for options that don't depend on
one another (nicer than a bunch of `nil`s before the next important
bit).

No varargs: With a table last, it doesn't really make much sense to keep
the (in some cases unrelated) user-arguments (plus they will eventually
be removed from function/dynamicNode as well, they only exist because I
didn't know how easy it is to do partial functions in lua).
  • Loading branch information
L3MON4D3 committed Aug 25, 2021
1 parent 8b146e7 commit f2d90d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
10 changes: 4 additions & 6 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ The second argument to `s` is a table containing all nodes that belong to the
snippet. If the table only has a single node, it can be passed directly
without wrapping it in a table.

The third argument is the condition-function. The snippet will be expanded only
if it returns true (default is a function that just returns true) (the function
is called before the text is modified in any way).

The fourth and following args are passed to the condition-function(allows
reusing condition-functions).
The third argument is a table with the following valid keys:
- `cond`: the condition-function. The snippet will be expanded only
if it returns true (default is a function that just returns true)
(the function is called before the text is modified in any way).

Snippets contain some interesting tables, eg. `snippet.env` contains variables
used in the LSP-protocol like `TM_CURRENT_LINE` or `TM_FILENAME` or
Expand Down
13 changes: 4 additions & 9 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ local function wrap_nodes(nodes)
end
end

local function S(context, nodes, condition, ...)
if not condition then
condition = function()
return true
end
end
local function S(context, nodes, opts)
opts = opts or {}

if type(context) == "string" then
context = { trig = context }
Expand Down Expand Up @@ -123,8 +119,7 @@ local function S(context, nodes, condition, ...)
nodes = nodes,
insert_nodes = {},
current_insert = 0,
condition = condition,
user_args = { ... },
condition = opts.condition or function() return true end,
mark = nil,
dependents = {},
active = false,
Expand Down Expand Up @@ -362,7 +357,7 @@ function Snippet:matches(line_to_cursor)
return nil
end

if not self.condition(unpack(self.user_args)) then
if not self.condition() then
return nil
end

Expand Down

0 comments on commit f2d90d4

Please sign in to comment.