Skip to content

Commit edef203

Browse files
committed
fix(lua): fix luals warnings
1 parent cf0dd38 commit edef203

File tree

3 files changed

+18
-37
lines changed

3 files changed

+18
-37
lines changed

lua/nvim-treesitter-textobjects/move.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ local function move(opts, query_strings, query_group)
8686
local function filter_function(start_, range)
8787
local row, col = unpack(api.nvim_win_get_cursor(winid)) --[[@as integer, integer]]
8888
row = row - 1 -- nvim_win_get_cursor is (1,0)-indexed
89-
---@type integer, integer, integer, integer
89+
---@type integer, integer, integer, integer, integer, integer
9090
local start_row, start_col, _, end_row, end_col, _ = unpack(range)
9191

9292
if not start_ then
@@ -95,7 +95,7 @@ local function move(opts, query_strings, query_group)
9595
start_col = end_col
9696
else
9797
start_row = end_row
98-
start_col = end_col - 1
98+
start_col = end_col - 1 ---@type integer
9999
end
100100
end
101101
if forward then

lua/nvim-treesitter-textobjects/select.lua

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ local global_config = require('nvim-treesitter-textobjects.config')
33
local shared = require('nvim-treesitter-textobjects.shared')
44

55
---@param range Range4
6-
---@param selection_mode string
6+
---@param selection_mode TSTextObjects.SelectionMode
77
local function update_selection(range, selection_mode)
88
---@type integer, integer, integer, integer
99
local start_row, start_col, end_row, end_col = unpack(range)
10-
11-
local v_table = { charwise = 'v', linewise = 'V', blockwise = '<C-v>' }
12-
selection_mode = selection_mode or 'charwise'
13-
14-
-- Normalise selection_mode
15-
if vim.tbl_contains(vim.tbl_keys(v_table), selection_mode) then
16-
selection_mode = v_table[selection_mode]
17-
end
10+
selection_mode = selection_mode or 'v'
1811

1912
-- enter visual mode if normal or operator-pending (no) mode
2013
-- Why? According to https://learnvimscriptthehardway.stevelosh.com/chapters/15.html
@@ -115,7 +108,7 @@ local function include_surrounding_whitespace(bufnr, range, selection_mode)
115108
local next = next_position(bufnr, unpack(position))
116109
while next and is_whitespace(bufnr, unpack(next)) do
117110
extended = true
118-
position = next
111+
position = next ---@type {[1]: integer, [2]: integer}
119112
next = next_position(bufnr, unpack(position))
120113
end
121114
if extended then
@@ -214,14 +207,14 @@ function M.detect_selection_mode(query_string)
214207
end
215208

216209
local ret_value = selection_mode
217-
local mode = api.nvim_get_mode().mode --[[@as string]]
210+
local mode = api.nvim_get_mode().mode
218211

219212
local is_normal_or_charwise_v = mode == 'n' or mode == 'v'
220213
if not is_normal_or_charwise_v then
221214
-- According to "mode()" mapping, if we are in operator pending mode or visual mode,
222215
-- then last char is {v,V,<C-v>}, exept for "no", which is "o", in which case we honor
223216
-- last set `selection_mode`
224-
mode = mode:sub(#mode)
217+
mode = mode:sub(#mode) --[[@as string]]
225218
ret_value = mode == 'o' and selection_mode or mode
226219
end
227220

lua/nvim-treesitter-textobjects/shared.lua

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ local lang_to_parser = { ecma = 'javascript', jsx = 'javascript' }
1010

1111
local M = {}
1212

13-
---@param object any
13+
---@param object table
1414
---@param path string[]
15-
---@param value any
15+
---@param value table
1616
local 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
2931
end
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
6351
end
6452

@@ -69,14 +57,14 @@ end
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[]
7361
local 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
131119
local 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

Comments
 (0)