Skip to content

Commit c6d088d

Browse files
committed
chore: add some more type annotations
1 parent bfa2628 commit c6d088d

File tree

14 files changed

+196
-137
lines changed

14 files changed

+196
-137
lines changed

lua/wincent/commandt/private/buffer_visible.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local buffer_visible = nil
5-
6-
-- `vim.fn.bufwinnr()` doesn't see windows in other tabs, meaning we open them
7-
-- again instead of switching to the other tab; but `vim.fn.bufname()` sees
8-
-- hidden buffers, and if we try to open one of those, we get an unwanted split.
9-
-- So, this function does some additional work to check whether `buffer` is
10-
-- _really_ visible.
11-
buffer_visible = function(buffer)
4+
--- `vim.fn.bufwinnr()` doesn't see windows in other tabs, meaning we open them
5+
--- again instead of switching to the other tab; but `vim.fn.bufname()` sees
6+
--- hidden buffers, and if we try to open one of those, we get an unwanted
7+
--- split. So, this function does some additional work to check whether `buffer`
8+
--- is _really_ visible.
9+
---
10+
--- @param buffer string|number
11+
--- @return boolean
12+
local function buffer_visible(buffer)
1213
-- TODO: port this to use lower-level nvim APIs, if there are any that could
1314
-- be used here...
1415
if vim.fn.bufwinnr('^' .. buffer .. '$') ~= -1 then

lua/wincent/commandt/private/concat.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local concat = nil
5-
6-
-- Utility function that concatenates one or more list-like tables into a new
7-
-- list.
8-
concat = function(...)
4+
--- Utility function that concatenates one or more list-like tables into a new
5+
--- list.
6+
---
7+
--- @param ... any[]
8+
--- @return any[]
9+
local function concat(...)
910
local final = {}
1011
for _, t in ipairs({ ... }) do
1112
for _, v in ipairs(t) do

lua/wincent/commandt/private/includes.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4+
--- Returns `true` if `value` exists in list-like table, `t`.
5+
---
6+
--- @param t any[]
7+
--- @param value any
8+
--- @return boolean
49
local function includes(t, value)
510
for _, candidate in ipairs(t) do
611
if candidate == value then

lua/wincent/commandt/private/is_integer.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local is_integer = function(numberish)
4+
--- Returns `true` if `numberish` is an integer.
5+
---
6+
--- @param numberish any
7+
--- @return boolean
8+
local function is_integer(numberish)
59
return type(numberish) == 'number' and math.floor(numberish) == numberish
610
end
711

lua/wincent/commandt/private/is_list.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local is_list = function(value)
4+
--- Returns `true` if `value` is a list-like table.
5+
---
6+
--- @param value any
7+
--- @return boolean
8+
local function is_list(value)
59
if type(value) ~= 'table' then
610
return false
711
elseif #value > 0 then

lua/wincent/commandt/private/is_table.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
local is_list = require('wincent.commandt.private.is_list')
55

6-
local is_table = function(value)
6+
--- Returns `true` if `value` is a dictionary-like table (not a list).
7+
---
8+
--- @param value any
9+
--- @return boolean
10+
local function is_table(value)
711
return type(value) == 'table' and (#value == 0 or not is_list(value))
812
end
913

lua/wincent/commandt/private/keys.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local keys = nil
5-
6-
-- Utility function that returns a list of keys in the given table.
7-
keys = function(t)
4+
--- Utility function that returns a list of keys in the given table, `t`.
5+
---
6+
--- @param t table
7+
--- @return any[]
8+
local function keys(t)
89
local final = {}
910
for k, _ in pairs(t) do
1011
table.insert(final, k)

lua/wincent/commandt/private/len.lua

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
11
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
-- Temporary wrapper to give us compatibility between Neovim v0.10.4 and
5-
-- more recent nightlies; see: https://github.com/wincent/command-t/issues/434
6-
--
7-
-- `:help str_utfindex` in v0.10.4:
8-
--
9-
-- vim.str_utfindex({str}, {index}) *vim.str_utfindex()*
10-
-- Convert byte index to UTF-32 and UTF-16 indices. If {index} is not
11-
-- supplied, the length of the string is used. All indices are zero-based.
12-
--
13-
-- Embedded NUL bytes are treated as terminating the string. Invalid UTF-8
14-
-- bytes, and embedded surrogates are counted as one code point each. An
15-
-- {index} in the middle of a UTF-8 sequence is rounded upwards to the end of
16-
-- that sequence.
17-
--
18-
-- Parameters: ~
19-
-- • {str} (`string`)
20-
-- • {index} (`integer?`)
21-
--
22-
-- Return (multiple): ~
23-
-- (`integer`) UTF-32 index
24-
-- (`integer`) UTF-16 index
25-
--
26-
--
27-
-- `:help str_utfindex` in nightly:
28-
--
29-
-- *vim.str_utfindex()*
30-
-- vim.str_utfindex({s}, {encoding}, {index}, {strict_indexing})
31-
-- Convert byte index to UTF-32, UTF-16 or UTF-8 indices. If {index} is not
32-
-- supplied, the length of the string is used. All indices are zero-based.
33-
--
34-
-- If {strict_indexing} is false then an out of range index will return
35-
-- string length instead of throwing an error. Invalid UTF-8 bytes, and
36-
-- embedded surrogates are counted as one code point each. An {index} in the
37-
-- middle of a UTF-8 sequence is rounded upwards to the end of that sequence.
38-
--
39-
-- Parameters: ~
40-
-- • {s} (`string`)
41-
-- • {encoding} (`"utf-8"|"utf-16"|"utf-32"`)
42-
-- • {index} (`integer?`)
43-
-- • {strict_indexing} (`boolean?`) default: true
44-
--
45-
-- Return: ~
46-
-- (`integer`)
47-
4+
--- Temporary wrapper to give us compatibility between Neovim v0.10.4 and
5+
--- more recent nightlies; see: https://github.com/wincent/command-t/issues/434
6+
---
7+
--- `:help str_utfindex` in v0.10.4:
8+
---
9+
--- vim.str_utfindex({str}, {index}) *vim.str_utfindex()*
10+
--- Convert byte index to UTF-32 and UTF-16 indices. If {index} is not
11+
--- supplied, the length of the string is used. All indices are zero-based.
12+
---
13+
--- Embedded NUL bytes are treated as terminating the string. Invalid UTF-8
14+
--- bytes, and embedded surrogates are counted as one code point each. An
15+
--- {index} in the middle of a UTF-8 sequence is rounded upwards to the end of
16+
--- that sequence.
17+
---
18+
--- Parameters: ~
19+
--- • {str} (`string`)
20+
--- • {index} (`integer?`)
21+
---
22+
--- Return (multiple): ~
23+
--- (`integer`) UTF-32 index
24+
--- (`integer`) UTF-16 index
25+
---
26+
---
27+
--- `:help str_utfindex` in nightly:
28+
---
29+
--- *vim.str_utfindex()*
30+
--- vim.str_utfindex({s}, {encoding}, {index}, {strict_indexing})
31+
--- Convert byte index to UTF-32, UTF-16 or UTF-8 indices. If {index} is not
32+
--- supplied, the length of the string is used. All indices are zero-based.
33+
---
34+
--- If {strict_indexing} is false then an out of range index will return
35+
--- string length instead of throwing an error. Invalid UTF-8 bytes, and
36+
--- embedded surrogates are counted as one code point each. An {index} in the
37+
--- middle of a UTF-8 sequence is rounded upwards to the end of that sequence.
38+
---
39+
--- Parameters: ~
40+
--- • {s} (`string`)
41+
--- • {encoding} (`"utf-8"|"utf-16"|"utf-32"`)
42+
--- • {index} (`integer?`)
43+
--- • {strict_indexing} (`boolean?`) default: true
44+
---
45+
--- Return: ~
46+
--- (`integer`)
47+
---
48+
--- @param str string
49+
--- @param encoding 'utf-8' | 'utf-16' | 'utf-32'
50+
--- @param start_char integer?
51+
--- @param strict_indexing boolean?
52+
--- @return integer
4853
local function str_utfindex(str, encoding, start_char, strict_indexing)
4954
local success, result = pcall(function()
5055
-- Nightly.
@@ -58,7 +63,10 @@ local function str_utfindex(str, encoding, start_char, strict_indexing)
5863
return vim.str_utfindex(str, start_char)
5964
end
6065

61-
-- Unicode-aware `len` implementation.
66+
--- Unicode-aware `len` implementation.
67+
---
68+
--- @param str string
69+
--- @return integer
6270
local function len(str)
6371
return str_utfindex(str, 'utf-32')
6472
end

lua/wincent/commandt/private/merge.lua

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local merge = nil
5-
64
local is_list = require('wincent.commandt.private.is_list')
75
local is_table = require('wincent.commandt.private.is_table')
86

9-
-- Utility function for working with functions that take optional arguments.
10-
--
11-
-- Creates a merged table containing items from the supplied tables, working
12-
-- from left to right, recursively.
13-
--
14-
-- ie. `merge(t1, t2, t3)` will insert elements from `t1`, then `t2`, then
15-
-- `t3` into a new table, then return the new table.
16-
--
17-
-- Note that:
18-
--
19-
-- - Values of different types will overwrite rather than merge recursively.
20-
-- - List-like tables will overwrite rather than merge (because it is convenient
21-
-- for user settings).
22-
-- - Table-like tables _will_ merge recursively.
23-
--
24-
-- We're not depending on `vim.tbl_deep_extend()` so that we can use this
25-
-- anywhere (eg. benchmarks, tests etc).
26-
merge = function(...)
7+
--- Utility function for working with functions that take optional arguments.
8+
---
9+
--- Creates a merged table containing items from the supplied tables, working
10+
--- from left to right, recursively.
11+
---
12+
--- ie. `merge(t1, t2, t3)` will insert elements from `t1`, then `t2`, then
13+
--- `t3` into a new table, then return the new table.
14+
---
15+
--- Note that:
16+
---
17+
--- - Values of different types will overwrite rather than merge recursively.
18+
--- - List-like tables will overwrite rather than merge (because it is convenient
19+
--- for user settings).
20+
--- - Table-like tables _will_ merge recursively.
21+
---
22+
--- We're not depending on `vim.tbl_deep_extend()` so that we can use this
23+
--- anywhere (eg. benchmarks, tests etc).
24+
---
25+
--- @param ... table? # One or more tables to merge (nil values are ignored)
26+
--- @return table
27+
local function merge(...)
2728
local final = {}
2829
for _, t in ipairs({ ... }) do
2930
if t ~= nil then

lua/wincent/commandt/private/str_prefix.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
local sub = require('wincent.commandt.private.sub')
55

6-
-- Attempts to return a prefix of `str` of size `length`, as measured in screen
7-
-- cells. If `str` if overlength, and the final character that must be trimmed
8-
-- to bring it down to the desired length is a double-cell one, the actual
9-
-- returned link may be off (specifically, underlength) by 1 screen cell.
6+
--- Attempts to return a prefix of `str` of size `length`, as measured in screen
7+
--- cells. If `str` if overlength, and the final character that must be trimmed
8+
--- to bring it down to the desired length is a double-cell one, the actual
9+
--- returned link may be off (specifically, underlength) by 1 screen cell.
10+
---
11+
--- @param str string
12+
--- @param length integer
13+
--- @return string
1014
local function str_prefix(str, length)
1115
if length < 1 then
1216
return ''

0 commit comments

Comments
 (0)