|
1 | 1 | local M = {} |
2 | 2 |
|
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[]} |
4 | 14 | local function_properties = setmetatable({}, { __mode = "k" }) |
5 | 15 |
|
| 16 | +--- The default extend function implementation. |
| 17 | +--- |
| 18 | +---@param arg any[] |
| 19 | +---@param extend any[] |
| 20 | +---@return any[] |
6 | 21 | local function default_extend(arg, extend) |
7 | 22 | return vim.tbl_extend("keep", arg or {}, extend or {}) |
8 | 23 | end |
9 | 24 |
|
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. |
24 | 44 | function M.apply(fn, ...) |
25 | 45 | local extend_properties = function_properties[fn] |
26 | 46 | assert( |
@@ -54,20 +74,16 @@ function M.apply(fn, ...) |
54 | 74 | return decorated_fn |
55 | 75 | end |
56 | 76 |
|
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`. |
71 | 87 | function M.register(fn, ...) |
72 | 88 | local fn_eps = { ... } |
73 | 89 |
|
|
0 commit comments