refactor(types): fix miscellaneous type warnings

This commit is contained in:
Maria José Solano 2024-02-18 18:46:19 -08:00 committed by Lewis Russell
parent 0fcbda5987
commit 185752614d
6 changed files with 32 additions and 19 deletions

View File

@ -2509,7 +2509,7 @@ vim.ui.input({opts}, {on_confirm}) *vim.ui.input()*
< <
Parameters: ~ Parameters: ~
• {opts} (`table`) Additional options. See |input()| • {opts} (`table?`) Additional options. See |input()|
• prompt (string|nil) Text of the prompt • prompt (string|nil) Text of the prompt
• default (string|nil) Default reply to the input • default (string|nil) Default reply to the input
• completion (string|nil) Specifies type of completion • completion (string|nil) Specifies type of completion
@ -2566,7 +2566,7 @@ vim.ui.select({items}, {opts}, {on_choice}) *vim.ui.select()*
< <
Parameters: ~ Parameters: ~
• {items} (`table`) Arbitrary items • {items} (`any[]`) Arbitrary items
• {opts} (`table`) Additional options • {opts} (`table`) Additional options
• prompt (string|nil) Text of the prompt. Defaults to • prompt (string|nil) Text of the prompt. Defaults to
`Select one of:` `Select one of:`
@ -2577,8 +2577,8 @@ vim.ui.select({items}, {opts}, {on_choice}) *vim.ui.select()*
item shape. Plugins reimplementing `vim.ui.select` may item shape. Plugins reimplementing `vim.ui.select` may
wish to use this to infer the structure or semantics of wish to use this to infer the structure or semantics of
`items`, or the context in which select() was called. `items`, or the context in which select() was called.
• {on_choice} (`function`) ((item|nil, idx|nil) -> ()) Called once the • {on_choice} (`fun(item: any?, idx: integer?)`) Called once the user
user made a choice. `idx` is the 1-based index of `item` made a choice. `idx` is the 1-based index of `item`
within `items`. `nil` if the user aborted the dialog. within `items`. `nil` if the user aborted the dialog.

View File

@ -732,7 +732,7 @@ get_node({opts}) *vim.treesitter.get_node()*
< <
Parameters: ~ Parameters: ~
• {opts} (`table?`) Optional keyword arguments: • {opts} (`vim.treesitter.GetNodeOpts?`) Optional keyword arguments:
• bufnr integer|nil Buffer number (nil or 0 for current • bufnr integer|nil Buffer number (nil or 0 for current
buffer) buffer)
• pos table|nil 0-indexed (row, col) tuple. Defaults to cursor • pos table|nil 0-indexed (row, col) tuple. Defaults to cursor

View File

@ -294,6 +294,7 @@ function M.get_captures_at_pos(bufnr, row, col)
for capture, node, metadata in iter do for capture, node, metadata in iter do
if M.is_in_node_range(node, row, col) then if M.is_in_node_range(node, row, col) then
---@diagnostic disable-next-line: invisible
local c = q._query.captures[capture] -- name of the capture in the query local c = q._query.captures[capture] -- name of the capture in the query
if c ~= nil then if c ~= nil then
table.insert(matches, { capture = c, metadata = metadata, lang = tree:lang() }) table.insert(matches, { capture = c, metadata = metadata, lang = tree:lang() })
@ -325,6 +326,12 @@ function M.get_captures_at_cursor(winnr)
return captures return captures
end end
--- @class vim.treesitter.GetNodeOpts
--- @field bufnr integer?
--- @field pos { [1]: integer, [2]: integer }?
--- @field lang string?
--- @field ignore_injections boolean?
--- Returns the smallest named node at the given position --- Returns the smallest named node at the given position
--- ---
--- NOTE: Calling this on an unparsed tree can yield an invalid node. --- NOTE: Calling this on an unparsed tree can yield an invalid node.
@ -335,7 +342,7 @@ end
--- vim.treesitter.get_parser(bufnr):parse(range) --- vim.treesitter.get_parser(bufnr):parse(range)
--- ``` --- ```
--- ---
---@param opts table|nil Optional keyword arguments: ---@param opts vim.treesitter.GetNodeOpts? Optional keyword arguments:
--- - bufnr integer|nil Buffer number (nil or 0 for current buffer) --- - bufnr integer|nil Buffer number (nil or 0 for current buffer)
--- - pos table|nil 0-indexed (row, col) tuple. Defaults to cursor position in the --- - pos table|nil 0-indexed (row, col) tuple. Defaults to cursor position in the
--- current window. Required if {bufnr} is not the current buffer --- current window. Required if {bufnr} is not the current buffer
@ -352,7 +359,7 @@ function M.get_node(opts)
bufnr = api.nvim_get_current_buf() bufnr = api.nvim_get_current_buf()
end end
local row, col local row, col --- @type integer, integer
if opts.pos then if opts.pos then
assert(#opts.pos == 2, 'Position must be a (row, col) tuple') assert(#opts.pos == 2, 'Position must be a (row, col) tuple')
row, col = opts.pos[1], opts.pos[2] row, col = opts.pos[1], opts.pos[2]

View File

@ -7,7 +7,7 @@ local ns = api.nvim_create_namespace('treesitter/highlighter')
---@alias vim.treesitter.highlighter.Iter fun(end_line: integer|nil): integer, TSNode, TSMetadata ---@alias vim.treesitter.highlighter.Iter fun(end_line: integer|nil): integer, TSNode, TSMetadata
---@class vim.treesitter.highlighter.Query ---@class vim.treesitter.highlighter.Query
---@field private _query vim.treesitter.query.Query? ---@field private _query vim.treesitter.Query?
---@field private lang string ---@field private lang string
---@field private hl_cache table<integer,integer> ---@field private hl_cache table<integer,integer>
local TSHighlighterQuery = {} local TSHighlighterQuery = {}

View File

@ -20,7 +20,7 @@ local M = {}
--- end) --- end)
--- ``` --- ```
--- ---
---@param items table Arbitrary items ---@param items any[] Arbitrary items
---@param opts table Additional options ---@param opts table Additional options
--- - prompt (string|nil) --- - prompt (string|nil)
--- Text of the prompt. Defaults to `Select one of:` --- Text of the prompt. Defaults to `Select one of:`
@ -32,7 +32,7 @@ local M = {}
--- Plugins reimplementing `vim.ui.select` may wish to --- Plugins reimplementing `vim.ui.select` may wish to
--- use this to infer the structure or semantics of --- use this to infer the structure or semantics of
--- `items`, or the context in which select() was called. --- `items`, or the context in which select() was called.
---@param on_choice function ((item|nil, idx|nil) -> ()) ---@param on_choice fun(item: any|nil, idx: integer|nil)
--- Called once the user made a choice. --- Called once the user made a choice.
--- `idx` is the 1-based index of `item` within `items`. --- `idx` is the 1-based index of `item` within `items`.
--- `nil` if the user aborted the dialog. --- `nil` if the user aborted the dialog.
@ -44,7 +44,7 @@ function M.select(items, opts, on_choice)
opts = opts or {} opts = opts or {}
local choices = { opts.prompt or 'Select one of:' } local choices = { opts.prompt or 'Select one of:' }
local format_item = opts.format_item or tostring local format_item = opts.format_item or tostring
for i, item in pairs(items) do for i, item in ipairs(items) do
table.insert(choices, string.format('%d: %s', i, format_item(item))) table.insert(choices, string.format('%d: %s', i, format_item(item)))
end end
local choice = vim.fn.inputlist(choices) local choice = vim.fn.inputlist(choices)
@ -66,7 +66,7 @@ end
--- end) --- end)
--- ``` --- ```
--- ---
---@param opts table Additional options. See |input()| ---@param opts table? Additional options. See |input()|
--- - prompt (string|nil) --- - prompt (string|nil)
--- Text of the prompt --- Text of the prompt
--- - default (string|nil) --- - default (string|nil)
@ -87,6 +87,7 @@ end
--- `nil` if the user aborted the dialog. --- `nil` if the user aborted the dialog.
function M.input(opts, on_confirm) function M.input(opts, on_confirm)
vim.validate({ vim.validate({
opts = { opts, 'table', true },
on_confirm = { on_confirm, 'function', false }, on_confirm = { on_confirm, 'function', false },
}) })

View File

@ -69,6 +69,8 @@ Version.__index = Version
--- Compares prerelease strings: per semver, number parts must be must be treated as numbers: --- Compares prerelease strings: per semver, number parts must be must be treated as numbers:
--- "pre1.10" is greater than "pre1.2". https://semver.org/#spec-item-11 --- "pre1.10" is greater than "pre1.2". https://semver.org/#spec-item-11
---@param prerel1 string?
---@param prerel2 string?
local function cmp_prerel(prerel1, prerel2) local function cmp_prerel(prerel1, prerel2)
if not prerel1 or not prerel2 then if not prerel1 or not prerel2 then
return prerel1 and -1 or (prerel2 and 1 or 0) return prerel1 and -1 or (prerel2 and 1 or 0)
@ -78,8 +80,8 @@ local function cmp_prerel(prerel1, prerel2)
local iter1 = prerel1:gmatch('([^0-9]*)(%d*)') local iter1 = prerel1:gmatch('([^0-9]*)(%d*)')
local iter2 = prerel2:gmatch('([^0-9]*)(%d*)') local iter2 = prerel2:gmatch('([^0-9]*)(%d*)')
while true do while true do
local word1, n1 = iter1() local word1, n1 = iter1() --- @type string?, string|number|nil
local word2, n2 = iter2() local word2, n2 = iter2() --- @type string?, string|number|nil
if word1 == nil and word2 == nil then -- Done iterating. if word1 == nil and word2 == nil then -- Done iterating.
return 0 return 0
end end
@ -168,6 +170,7 @@ function M._version(version, strict) -- Adapted from https://github.com/folke/la
end end
if not strict then -- TODO: add more "scrubbing". if not strict then -- TODO: add more "scrubbing".
--- @cast version string
version = version:match('%d[^ ]*') version = version:match('%d[^ ]*')
end end
@ -298,8 +301,9 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
local semver = M.parse(version) local semver = M.parse(version)
if semver then if semver then
local from = semver local from = semver --- @type Version?
local to = vim.deepcopy(semver, true) local to = vim.deepcopy(semver, true) --- @type Version?
---@diagnostic disable: need-check-nil
if mods == '' or mods == '=' then if mods == '' or mods == '=' then
to.patch = to.patch + 1 to.patch = to.patch + 1
elseif mods == '<' then elseif mods == '<' then
@ -309,9 +313,9 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
to.patch = to.patch + 1 to.patch = to.patch + 1
elseif mods == '>' then elseif mods == '>' then
from.patch = from.patch + 1 from.patch = from.patch + 1
to = nil ---@diagnostic disable-line: cast-local-type to = nil
elseif mods == '>=' then elseif mods == '>=' then
to = nil ---@diagnostic disable-line: cast-local-type to = nil
elseif mods == '~' then elseif mods == '~' then
if #parts >= 2 then if #parts >= 2 then
to[2] = to[2] + 1 to[2] = to[2] + 1
@ -332,6 +336,7 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
end end
end end
end end
---@diagnostic enable: need-check-nil
return setmetatable({ from = from, to = to }, { __index = VersionRange }) return setmetatable({ from = from, to = to }, { __index = VersionRange })
end end
end end
@ -445,7 +450,7 @@ setmetatable(M, {
--- Returns the current Nvim version. --- Returns the current Nvim version.
---@return Version ---@return Version
__call = function() __call = function()
local version = vim.fn.api_info().version local version = vim.fn.api_info().version ---@type Version
-- Workaround: vim.fn.api_info().version reports "prerelease" as a boolean. -- Workaround: vim.fn.api_info().version reports "prerelease" as a boolean.
version.prerelease = version.prerelease and 'dev' or nil version.prerelease = version.prerelease and 'dev' or nil
return setmetatable(version, Version) return setmetatable(version, Version)