@@ -10,9 +10,9 @@ local lang_to_parser = { ecma = 'javascript', jsx = 'javascript' }
1010
1111local M = {}
1212
13- --- @param object any
13+ --- @param object table
1414--- @param path string[]
15- --- @param value any
15+ --- @param value table
1616local function insert_to_path (object , path , value )
1717 --- @type table<string , any | table<string , any>>
1818 local curr_obj = object
@@ -22,9 +22,11 @@ local function insert_to_path(object, path, value)
2222 curr_obj [path [index ]] = {}
2323 end
2424
25+ --- @type table<string , any | table<string , any>>
2526 curr_obj = curr_obj [path [index ]]
2627 end
2728
29+ --- @type table<string , any | table<string , any>>
2830 curr_obj [path [# path ]] = value
2931end
3032
@@ -39,26 +41,12 @@ local function memoize(fn, hash_fn)
3941 return function (...)
4042 local key = hash_fn (... )
4143 if cache [key ] == nil then
42- local v = { fn (... ) } --- @type any
43-
44- for k , value in pairs (v ) do
45- if value == nil then
46- value [k ] = vim .NIL
47- end
48- end
49-
50- cache [key ] = v
44+ local v = fn (... ) --- @type any
45+ cache [key ] = v ~= nil and v or vim .NIL
5146 end
5247
5348 local v = cache [key ]
54-
55- for k , value in pairs (v ) do
56- if value == vim .NIL then
57- value [k ] = nil
58- end
59- end
60-
61- return unpack (v )
49+ return v ~= vim .NIL and v or nil
6250 end
6351end
6452
6957--- @param query_group string the query file to use
7058--- @param root TSNode the root node
7159--- @param root_lang string the root node lang , if known
72- --- @return table
60+ --- @return table[]
7361local get_query_matches = memoize (function (bufnr , query_group , root , root_lang )
7462 local query = ts .query .get (root_lang , query_group )
7563 if not query then
7664 return {}
7765 end
7866
79- local matches = {}
67+ local matches = {} --- @type table[]
8068 local start_row , _ , end_row , _ = root :range ()
8169 -- The end row is exclusive so we need to add 1 to it.
8270 for pattern , match , metadata in query :iter_matches (root , bufnr , start_row , end_row + 1 ) do
@@ -127,18 +115,18 @@ end)
127115
128116--- @param tbl table<string , any | table<string , any>> the table to access
129117--- @param path string the ' .' separated path
130- --- @return unknown | nil result the value at path or nil
118+ --- @return any | nil result the value at path or nil
131119local function get_at_path (tbl , path )
132120 if path == ' ' then
133121 return tbl
134122 end
135123
136124 local segments = vim .split (path , ' %.' )
137- --- @type table<string , any | table<string , any>>
138125 local result = tbl
139126
140127 for _ , segment in ipairs (segments ) do
141128 if type (result ) == ' table' then
129+ --- @type any
142130 result = result [segment ]
143131 end
144132 end
0 commit comments