aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/_editor.lua29
-rw-r--r--runtime/lua/vim/_init_packages.lua1
-rw-r--r--runtime/lua/vim/_watch.lua1
-rw-r--r--runtime/lua/vim/diagnostic.lua1
-rw-r--r--runtime/lua/vim/health.lua102
-rw-r--r--runtime/lua/vim/iter.lua9
-rw-r--r--runtime/lua/vim/loader.lua2
-rw-r--r--runtime/lua/vim/lsp.lua4
-rw-r--r--runtime/lua/vim/lsp/buf.lua2
-rw-r--r--runtime/lua/vim/shared.lua5
-rw-r--r--runtime/lua/vim/ui.lua2
11 files changed, 108 insertions, 50 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua
index 0b338fa86f..cde61697b6 100644
--- a/runtime/lua/vim/_editor.lua
+++ b/runtime/lua/vim/_editor.lua
@@ -156,10 +156,10 @@ function vim._os_proc_info(pid)
elseif r.code ~= 0 then
error('command failed: ' .. vim.fn.string(cmd))
end
- local ppid = assert(vim.system({ 'ps', '-p', pid, '-o', 'ppid=' }):wait().stdout)
+ local ppid_string = assert(vim.system({ 'ps', '-p', pid, '-o', 'ppid=' }):wait().stdout)
-- Remove trailing whitespace.
name = vim.trim(name):gsub('^.*/', '')
- ppid = tonumber(ppid) or -1
+ local ppid = tonumber(ppid_string) or -1
return {
name = name,
pid = pid,
@@ -533,20 +533,21 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
local region = {}
for l = pos1[1], pos2[1] do
- local c1, c2
+ local c1 --- @type number
+ local c2 --- @type number
if regtype:byte() == 22 then -- block selection: take width from regtype
c1 = pos1[2]
- c2 = c1 + regtype:sub(2)
+ c2 = c1 + tonumber(regtype:sub(2))
-- and adjust for non-ASCII characters
local bufline = vim.api.nvim_buf_get_lines(bufnr, l, l + 1, true)[1]
local utflen = vim.str_utfindex(bufline, #bufline)
if c1 <= utflen then
- c1 = vim.str_byteindex(bufline, c1)
+ c1 = assert(tonumber(vim.str_byteindex(bufline, c1)))
else
c1 = #bufline + 1
end
if c2 <= utflen then
- c2 = vim.str_byteindex(bufline, c2)
+ c2 = assert(tonumber(vim.str_byteindex(bufline, c2)))
else
c2 = #bufline + 1
end
@@ -576,7 +577,7 @@ end
---@return table timer luv timer object
function vim.defer_fn(fn, timeout)
vim.validate({ fn = { fn, 'c', true } })
- local timer = vim.uv.new_timer()
+ local timer = assert(vim.uv.new_timer())
timer:start(
timeout,
0,
@@ -601,6 +602,7 @@ end
---@param msg string Content of the notification to show to the user.
---@param level integer|nil One of the values from |vim.log.levels|.
---@param opts table|nil Optional parameters. Unused by default.
+---@diagnostic disable-next-line: unused-local
function vim.notify(msg, level, opts) -- luacheck: no unused args
if level == vim.log.levels.ERROR then
vim.api.nvim_err_writeln(msg)
@@ -700,6 +702,8 @@ end
---
--- 1. Can we get it to just return things in the global namespace with that name prefix
--- 2. Can we get it to return things from global namespace even with `print(` in front.
+---
+--- @param pat string
function vim._expand_pat(pat, env)
env = env or _G
@@ -801,11 +805,13 @@ function vim._expand_pat(pat, env)
return keys, #prefix_match_pat
end
+--- @param lua_string string
vim._expand_pat_get_parts = function(lua_string)
local parts = {}
local accumulator, search_index = '', 1
- local in_brackets, bracket_end = false, -1
+ local in_brackets = false
+ local bracket_end = -1 --- @type integer?
local string_char = nil
for idx = 1, #lua_string do
local s = lua_string:sub(idx, idx)
@@ -938,9 +944,12 @@ function vim.keycode(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
+--- @param server_addr string
+--- @param connect_error string
function vim._cs_remote(rcid, server_addr, connect_error, args)
+ --- @return string
local function connection_failure_errmsg(consequence)
- local explanation
+ local explanation --- @type string
if server_addr == '' then
explanation = 'No server specified with --server'
else
@@ -983,7 +992,7 @@ function vim._cs_remote(rcid, server_addr, connect_error, args)
local res = tostring(vim.rpcrequest(rcid, 'nvim_eval', args[2]))
return { result = res, should_exit = true, tabbed = false }
elseif subcmd ~= '' then
- return { errmsg = 'Unknown option argument: ' .. args[1] }
+ return { errmsg = 'Unknown option argument: ' .. tostring(args[1]) }
end
if rcid == 0 then
diff --git a/runtime/lua/vim/_init_packages.lua b/runtime/lua/vim/_init_packages.lua
index f8710f7fd7..97d483333e 100644
--- a/runtime/lua/vim/_init_packages.lua
+++ b/runtime/lua/vim/_init_packages.lua
@@ -12,6 +12,7 @@ for s in (package.cpath .. ';'):gmatch('[^;]*;') do
end
end
+--- @param name string
function vim._load_package(name)
local basename = name:gsub('%.', '/')
local paths = { 'lua/' .. basename .. '.lua', 'lua/' .. basename .. '/init.lua' }
diff --git a/runtime/lua/vim/_watch.lua b/runtime/lua/vim/_watch.lua
index 43fce3bf7f..092781826f 100644
--- a/runtime/lua/vim/_watch.lua
+++ b/runtime/lua/vim/_watch.lua
@@ -51,6 +51,7 @@ function M.watch(path, opts, callback)
local uvflags = opts and opts.uvflags or {}
local handle, new_err = vim.uv.new_fs_event()
assert(not new_err, new_err)
+ handle = assert(handle)
local _, start_err = handle:start(path, uvflags, function(err, filename, events)
assert(not err, err)
local fullpath = path
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index a82a61eeb7..ad40723737 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -108,6 +108,7 @@ local function filter_by_severity(severity, diagnostics)
severities[to_severity(s)] = true
end
+ --- @param t table
return vim.tbl_filter(function(t)
return severities[t.severity]
end, diagnostics)
diff --git a/runtime/lua/vim/health.lua b/runtime/lua/vim/health.lua
index 23771d2807..55a451bf56 100644
--- a/runtime/lua/vim/health.lua
+++ b/runtime/lua/vim/health.lua
@@ -1,8 +1,8 @@
local M = {}
-local s_output = {}
+local s_output = {} ---@type string[]
--- Returns the fold text of the current healthcheck section
+--- Returns the fold text of the current healthcheck section
function M.foldtext()
local foldtext = vim.fn.foldtext()
@@ -36,12 +36,13 @@ function M.foldtext()
return vim.b.failedchecks[foldtext] and '+WE' .. foldtext:sub(4) or foldtext
end
--- From a path return a list [{name}, {func}, {type}] representing a healthcheck
+--- @param path string path to search for the healthcheck
+--- @return string[] { name, func, type } representing a healthcheck
local function filepath_to_healthcheck(path)
path = vim.fs.normalize(path)
- local name
- local func
- local filetype
+ local name --- @type string
+ local func --- @type string
+ local filetype --- @type string
if path:find('vim$') then
name = vim.fs.basename(path):gsub('%.vim$', '')
func = 'health#' .. name .. '#check'
@@ -50,10 +51,10 @@ local function filepath_to_healthcheck(path)
local subpath = path:gsub('.*lua/', '')
if vim.fs.basename(subpath) == 'health.lua' then
-- */health.lua
- name = vim.fs.dirname(subpath)
+ name = assert(vim.fs.dirname(subpath))
else
-- */health/init.lua
- name = vim.fs.dirname(vim.fs.dirname(subpath))
+ name = assert(vim.fs.dirname(assert(vim.fs.dirname(subpath))))
end
name = name:gsub('/', '.')
@@ -63,11 +64,12 @@ local function filepath_to_healthcheck(path)
return { name, func, filetype }
end
--- Returns { {name, func, type}, ... } representing healthchecks
+--- @param plugin_names string
+--- @return table<any,string[]> { {name, func, type}, ... } representing healthchecks
local function get_healthcheck_list(plugin_names)
- local healthchecks = {}
- plugin_names = vim.split(plugin_names, ' ')
- for _, p in pairs(plugin_names) do
+ local healthchecks = {} --- @type table<any,string[]>
+ local plugin_names_list = vim.split(plugin_names, ' ')
+ for _, p in pairs(plugin_names_list) do
-- support vim/lsp/health{/init/}.lua as :checkhealth vim.lsp
p = p:gsub('%.', '/')
@@ -83,7 +85,7 @@ local function get_healthcheck_list(plugin_names)
if vim.tbl_count(paths) == 0 then
healthchecks[#healthchecks + 1] = { p, '', '' } -- healthcheck not found
else
- local unique_paths = {}
+ local unique_paths = {} --- @type table<string, boolean>
for _, v in pairs(paths) do
unique_paths[v] = true
end
@@ -100,10 +102,11 @@ local function get_healthcheck_list(plugin_names)
return healthchecks
end
--- Returns {name: [func, type], ..} representing healthchecks
+--- @param plugin_names string
+--- @return table<string, string[]> {name: [func, type], ..} representing healthchecks
local function get_healthcheck(plugin_names)
local health_list = get_healthcheck_list(plugin_names)
- local healthchecks = {}
+ local healthchecks = {} --- @type table<string, string[]>
for _, c in pairs(health_list) do
if c[1] ~= 'vim' then
healthchecks[c[1]] = { c[2], c[3] }
@@ -113,7 +116,11 @@ local function get_healthcheck(plugin_names)
return healthchecks
end
--- Indents lines *except* line 1 of a string if it contains newlines.
+--- Indents lines *except* line 1 of a string if it contains newlines.
+---
+--- @param s string
+--- @param columns integer
+--- @return string
local function indent_after_line1(s, columns)
local lines = vim.split(s, '\n')
local indent = string.rep(' ', columns)
@@ -123,13 +130,20 @@ local function indent_after_line1(s, columns)
return table.concat(lines, '\n')
end
--- Changes ':h clipboard' to ':help |clipboard|'.
+--- Changes ':h clipboard' to ':help |clipboard|'.
+---
+--- @param s string
+--- @return string
local function help_to_link(s)
return vim.fn.substitute(s, [[\v:h%[elp] ([^|][^"\r\n ]+)]], [[:help |\1|]], [[g]])
end
--- Format a message for a specific report item.
--- Variable args: Optional advice (string or list)
+--- Format a message for a specific report item.
+---
+--- @param status string
+--- @param msg string
+--- @param ... string|string[] Optional advice
+--- @return string
local function format_report_message(status, msg, ...)
local output = '- ' .. status
if status ~= '' then
@@ -159,42 +173,54 @@ local function format_report_message(status, msg, ...)
return help_to_link(output)
end
+--- @param output string
local function collect_output(output)
vim.list_extend(s_output, vim.split(output, '\n'))
end
--- Starts a new report.
+--- Starts a new report.
+---
+--- @param name string
function M.start(name)
local input = string.format('\n%s ~', name)
collect_output(input)
end
--- Reports a message in the current section.
+--- Reports a message in the current section.
+---
+--- @param msg string
function M.info(msg)
local input = format_report_message('', msg)
collect_output(input)
end
--- Reports a successful healthcheck.
+--- Reports a successful healthcheck.
+---
+--- @param msg string
function M.ok(msg)
local input = format_report_message('OK', msg)
collect_output(input)
end
--- Reports a health warning.
--- ...: Optional advice (string or table)
+--- Reports a health warning.
+---
+--- @param msg string
+--- @param ... string|string[] Optional advice
function M.warn(msg, ...)
local input = format_report_message('WARNING', msg, ...)
collect_output(input)
end
--- Reports a failed healthcheck.
--- ...: Optional advice (string or table)
+--- Reports a failed healthcheck.
+---
+--- @param msg string
+--- @param ... string|string[] Optional advice
function M.error(msg, ...)
local input = format_report_message('ERROR', msg, ...)
collect_output(input)
end
+--- @param type string
local function deprecate(type)
local before = string.format('vim.health.report_%s()', type)
local after = string.format('vim.health.%s()', type)
@@ -206,22 +232,36 @@ local function deprecate(type)
vim.print('Running healthchecks...')
end
+--- @deprecated
+--- @param name string
function M.report_start(name)
deprecate('start')
M.start(name)
end
+
+--- @deprecated
+--- @param msg string
function M.report_info(msg)
deprecate('info')
M.info(msg)
end
+
+--- @deprecated
+--- @param msg string
function M.report_ok(msg)
deprecate('ok')
M.ok(msg)
end
+
+--- @deprecated
+--- @param msg string
function M.report_warn(msg, ...)
deprecate('warn')
M.warn(msg, ...)
end
+
+--- @deprecated
+--- @param msg string
function M.report_error(msg, ...)
deprecate('error')
M.error(msg, ...)
@@ -251,7 +291,7 @@ local path2name = function(path)
end
local PATTERNS = { '/autoload/health/*.vim', '/lua/**/**/health.lua', '/lua/**/**/health/init.lua' }
--- :checkhealth completion function used by cmdexpand.c get_healthcheck_names()
+--- :checkhealth completion function used by cmdexpand.c get_healthcheck_names()
M._complete = function()
local names = vim.tbl_flatten(vim.tbl_map(function(pattern)
return vim.tbl_map(path2name, vim.api.nvim_get_runtime_file(pattern, true))
@@ -270,6 +310,9 @@ end
--- Runs all discovered healthchecks if plugin_names is empty.
---
--- @param mods string command modifiers that affect splitting a window.
+--- @param plugin_names string glob of plugin names, split on whitespace. For example, using
+--- `:checkhealth vim.* nvim` will healthcheck `vim.lsp`, `vim.treesitter`
+--- and `nvim` modules.
function M._check(mods, plugin_names)
local healthchecks = plugin_names == '' and get_healthcheck('*') or get_healthcheck(plugin_names)
@@ -289,7 +332,8 @@ function M._check(mods, plugin_names)
vim.cmd.file('health://')
vim.cmd.setfiletype('checkhealth')
- if healthchecks == nil or next(healthchecks) == nil then
+ -- This should only happen when doing `:checkhealth vim`
+ if next(healthchecks) == nil then
vim.fn.setline(1, 'ERROR: No healthchecks found.')
return
end
@@ -325,7 +369,7 @@ function M._check(mods, plugin_names)
local header = { string.rep('=', 78), name .. ': ' .. func, '' }
-- remove empty line after header from report_start
if s_output[1] == '' then
- local tmp = {}
+ local tmp = {} ---@type string[]
for i = 2, #s_output do
tmp[#tmp + 1] = s_output[i]
end
diff --git a/runtime/lua/vim/iter.lua b/runtime/lua/vim/iter.lua
index 8e602c406a..b658dde099 100644
--- a/runtime/lua/vim/iter.lua
+++ b/runtime/lua/vim/iter.lua
@@ -432,6 +432,7 @@ end
--- ```
---
---@return any
+---@diagnostic disable-next-line: unused-local
function Iter.next(self) -- luacheck: no unused args
-- This function is provided by the source iterator in Iter.new. This definition exists only for
-- the docstring
@@ -489,6 +490,7 @@ end
--- ```
---
---@return any
+---@diagnostic disable-next-line: unused-local
function Iter.peek(self) -- luacheck: no unused args
error('peek() requires a list-like table')
end
@@ -568,12 +570,13 @@ end
---@see Iter.find
---
---@return any
+---@diagnostic disable-next-line: unused-local
function Iter.rfind(self, f) -- luacheck: no unused args
error('rfind() requires a list-like table')
end
---@private
-function ListIter.rfind(self, f) -- luacheck: no unused args
+function ListIter.rfind(self, f)
if type(f) ~= 'function' then
local val = f
f = function(v)
@@ -640,6 +643,7 @@ end
--- ```
---
---@return any
+---@diagnostic disable-next-line: unused-local
function Iter.nextback(self) -- luacheck: no unused args
error('nextback() requires a list-like table')
end
@@ -669,6 +673,7 @@ end
--- ```
---
---@return any
+---@diagnostic disable-next-line: unused-local
function Iter.peekback(self) -- luacheck: no unused args
error('peekback() requires a list-like table')
end
@@ -725,6 +730,7 @@ end
---
---@param n number Number of values to skip.
---@return Iter
+---@diagnostic disable-next-line: unused-local
function Iter.skipback(self, n) -- luacheck: no unused args
error('skipback() requires a list-like table')
return self
@@ -791,6 +797,7 @@ end
---@param first number
---@param last number
---@return Iter
+---@diagnostic disable-next-line: unused-local
function Iter.slice(self, first, last) -- luacheck: no unused args
error('slice() requires a list-like table')
return self
diff --git a/runtime/lua/vim/loader.lua b/runtime/lua/vim/loader.lua
index ee01111337..b77251cf7e 100644
--- a/runtime/lua/vim/loader.lua
+++ b/runtime/lua/vim/loader.lua
@@ -1,5 +1,5 @@
local uv = vim.uv
-local uri_encode = vim.uri_encode
+local uri_encode = vim.uri_encode --- @type function
--- @type (fun(modename: string): fun()|string)[]
local loaders = package.loaders
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua
index 1310239a5b..3105413b53 100644
--- a/runtime/lua/vim/lsp.lua
+++ b/runtime/lua/vim/lsp.lua
@@ -329,11 +329,11 @@ end
---@param fn (T) Function to run
---@return T
local function once(fn)
- local value --- @type any
+ local value --- @type function
local ran = false
return function(...)
if not ran then
- value = fn(...)
+ value = fn(...) --- @type function
ran = true
end
return value
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index 051b9d4550..d67b2ac8ea 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -46,7 +46,7 @@ function M.hover()
end
local function request_with_options(name, params, options)
- local req_handler
+ local req_handler --- @type function?
if options then
req_handler = function(err, result, ctx, config)
local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index 9542d93789..bbbc888727 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -617,11 +617,6 @@ function vim.tbl_islist(t)
local num_elem = vim.tbl_count(t)
if num_elem == 0 then
- -- TODO(bfredl): in the future, we will always be inside nvim
- -- then this check can be deleted.
- if vim._empty_dict_mt == nil then
- return nil
- end
return getmetatable(t) ~= vim._empty_dict_mt
else
for i = 1, num_elem do
diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua
index b6ddf337ce..25ced18daf 100644
--- a/runtime/lua/vim/ui.lua
+++ b/runtime/lua/vim/ui.lua
@@ -133,7 +133,7 @@ function M.open(path)
path = vim.fn.expand(path)
end
- local cmd
+ local cmd --- @type string[]
if vim.fn.has('mac') == 1 then
cmd = { 'open', path }