Skip to content

Commit ae344e6

Browse files
committed
Add types & update docs for util extend_decorator
This is required for proper type deduction after extend_decorator.apply is used.
1 parent 3732756 commit ae344e6

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

lua/luasnip/util/extend_decorator.lua

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
local M = {}
22

3-
-- map fn -> {arg_indx = int, extend = fn}[]
3+
---@alias LuaSnip.Opts.Util.ExtendDecoratorFn fun(arg: any[], extend_value: any[]): any[]
4+
5+
---@class LuaSnip.Opts.Util.ExtendDecoratorRegister
6+
---@field arg_indx integer The position of the parameter to override
7+
---@field extend? LuaSnip.Opts.Util.ExtendDecoratorFn A function used to extend
8+
--- the args passed to the decorated function.
9+
--- Defaults to a function which extends the arg-table with the extend-table.
10+
--- This extend-behaviour is adaptable to accomodate `s`, where the first
11+
--- argument may be string or table.
12+
13+
---@type {[fun(...): any]: LuaSnip.Opts.Util.ExtendDecoratorRegister[]}
414
local function_properties = setmetatable({}, { __mode = "k" })
515

16+
--- The default extend function implementation.
17+
---
18+
---@param arg any[]
19+
---@param extend any[]
20+
---@return any[]
621
local function default_extend(arg, extend)
722
return vim.tbl_extend("keep", arg or {}, extend or {})
823
end
924

10-
---Create a new decorated version of `fn`.
11-
---@param fn The function to create a decorator for.
12-
---@vararg The values to extend with. These should match the descriptions passed
13-
---in `register`:
14-
---```lua
15-
---local function somefn(arg1, arg2, opts1, opts2)
16-
---...
17-
---end
18-
---register(somefn, {arg_indx=4}, {arg_indx=3})
19-
---apply(somefn,
20-
--- {key = "opts2 is extended with this"},
21-
--- {key = "and opts1 with this"})
22-
---```
23-
---@return function: The decorated function.
25+
--- Create a new decorated version of `fn`.
26+
---
27+
---@generic T: fun(...: any): any
28+
---@param fn T The function to create a decorator for.
29+
---@vararg any The values to extend with.
30+
--- These should match the descriptions passed in `register`.
31+
---
32+
--- Example:
33+
--- ```lua
34+
--- local function somefn(arg1, arg2, opts1, opts2)
35+
--- ...
36+
--- end
37+
--- register(somefn, {arg_indx=4}, {arg_indx=3})
38+
--- apply(somefn,
39+
--- {key = "opts2 is extended with this"},
40+
--- {key = "and opts1 with this"}
41+
--- )
42+
--- ```
43+
---@return T _ The decorated function.
2444
function M.apply(fn, ...)
2545
local extend_properties = function_properties[fn]
2646
assert(
@@ -54,20 +74,16 @@ function M.apply(fn, ...)
5474
return decorated_fn
5575
end
5676

57-
---Prepare a function for usage with extend_decorator.
58-
---To create a decorated function which extends `opts`-style tables passed to it, we need to know
59-
--- 1. which parameter-position the opts are in and
60-
--- 2. how to extend them.
61-
---@param fn function: the function that should be registered.
62-
---@vararg tables. Each describes how to extend one parameter to `fn`.
63-
---The tables accept the following keys:
64-
--- - arg_indx, number (required): the position of the parameter to override.
65-
--- - extend, fn(arg, extend_value) -> effective_arg (optional): this function
66-
--- is used to extend the args passed to the decorated function.
67-
--- It defaults to a function which just extends the the arg-table with the
68-
--- extend-table.
69-
--- This extend-behaviour is adaptable to accomodate `s`, where the first
70-
--- argument may be string or table.
77+
--- Prepare a function for usage with extend_decorator.
78+
---
79+
--- To create a decorated function which extends `opts`-style tables passed to
80+
--- it, we need to know:
81+
--- 1. which parameter-position the opts are in and
82+
--- 2. how to extend them.
83+
---
84+
---@param fn function The function that should be registered.
85+
---@vararg LuaSnip.Opts.Util.ExtendDecoratorRegister Each describes how to
86+
--- extend one parameter to `fn`.
7187
function M.register(fn, ...)
7288
local fn_eps = { ... }
7389

0 commit comments

Comments
 (0)