refactor(man): add type annotations

This commit is contained in:
Lewis Russell 2023-02-21 12:19:09 +00:00 committed by GitHub
parent fec1181ecd
commit 2d99830706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,8 @@ local api, fn = vim.api, vim.fn
local FIND_ARG = '-w' local FIND_ARG = '-w'
local localfile_arg = true -- Always use -l if possible. #6683 local localfile_arg = true -- Always use -l if possible. #6683
---@type table[]
local buf_hls = {} local buf_hls = {}
local M = {} local M = {}
@ -12,26 +14,28 @@ local function man_error(msg)
end end
-- Run a system command and timeout after 30 seconds. -- Run a system command and timeout after 30 seconds.
---@param cmd_ string[]
---@param silent boolean?
---@param env string[]
---@return string
local function system(cmd_, silent, env) local function system(cmd_, silent, env)
local stdout_data = {} local stdout_data = {} ---@type string[]
local stderr_data = {} local stderr_data = {} ---@type string[]
local stdout = vim.loop.new_pipe(false) local stdout = assert(vim.loop.new_pipe(false))
local stderr = vim.loop.new_pipe(false) local stderr = assert(vim.loop.new_pipe(false))
local done = false local done = false
local exit_code local exit_code ---@type integer?
-- We use the `env` command here rather than the env option to vim.loop.spawn since spawn will -- We use the `env` command here rather than the env option to vim.loop.spawn since spawn will
-- completely overwrite the environment when we just want to modify the existing one. -- completely overwrite the environment when we just want to modify the existing one.
-- --
-- Overwriting mainly causes problems NixOS which relies heavily on a non-standard environment. -- Overwriting mainly causes problems NixOS which relies heavily on a non-standard environment.
local cmd local cmd = cmd_
if env then if env then
cmd = { 'env' } cmd = { 'env' }
vim.list_extend(cmd, env) vim.list_extend(cmd, env)
vim.list_extend(cmd, cmd_) vim.list_extend(cmd, cmd_)
else
cmd = cmd_
end end
local handle local handle
@ -84,11 +88,17 @@ local function system(cmd_, silent, env)
return table.concat(stdout_data) return table.concat(stdout_data)
end end
---@param line string
---@param linenr integer
local function highlight_line(line, linenr) local function highlight_line(line, linenr)
---@type string[]
local chars = {} local chars = {}
local prev_char = '' local prev_char = ''
local overstrike, escape = false, false local overstrike, escape = false, false
---@type table<integer,{attr:integer,start:integer,final:integer}>
local hls = {} -- Store highlight groups as { attr, start, final } local hls = {} -- Store highlight groups as { attr, start, final }
local NONE, BOLD, UNDERLINE, ITALIC = 0, 1, 2, 3 local NONE, BOLD, UNDERLINE, ITALIC = 0, 1, 2, 3
local hl_groups = { [BOLD] = 'manBold', [UNDERLINE] = 'manUnderline', [ITALIC] = 'manItalic' } local hl_groups = { [BOLD] = 'manBold', [UNDERLINE] = 'manUnderline', [ITALIC] = 'manItalic' }
local attr = NONE local attr = NONE
@ -194,11 +204,12 @@ local function highlight_line(line, linenr)
-- We only want to match against SGR sequences, which consist of ESC -- We only want to match against SGR sequences, which consist of ESC
-- followed by '[', then a series of parameter and intermediate bytes in -- followed by '[', then a series of parameter and intermediate bytes in
-- the range 0x20 - 0x3f, then 'm'. (See ECMA-48, sections 5.4 & 8.3.117) -- the range 0x20 - 0x3f, then 'm'. (See ECMA-48, sections 5.4 & 8.3.117)
---@type string?
local sgr = prev_char:match('^%[([\032-\063]*)m$') local sgr = prev_char:match('^%[([\032-\063]*)m$')
-- Ignore escape sequences with : characters, as specified by ITU's T.416 -- Ignore escape sequences with : characters, as specified by ITU's T.416
-- Open Document Architecture and interchange format. -- Open Document Architecture and interchange format.
if sgr and not string.find(sgr, ':') then if sgr and not string.find(sgr, ':') then
local match local match ---@type string?
while sgr and #sgr > 0 do while sgr and #sgr > 0 do
-- Match against SGR parameters, which may be separated by ';' -- Match against SGR parameters, which may be separated by ';'
match, sgr = sgr:match('^(%d*);?(.*)') match, sgr = sgr:match('^(%d*);?(.*)')
@ -261,11 +272,16 @@ end
-- intended for PostgreSQL, which has man pages like 'CREATE_TABLE(7)'; -- intended for PostgreSQL, which has man pages like 'CREATE_TABLE(7)';
-- while editing SQL source code, it's nice to visually select 'CREATE TABLE' -- while editing SQL source code, it's nice to visually select 'CREATE TABLE'
-- and hit 'K', which requires this transformation -- and hit 'K', which requires this transformation
---@param str string
---@return string
local function spaces_to_underscores(str) local function spaces_to_underscores(str)
local res = str:gsub('%s', '_') local res = str:gsub('%s', '_')
return res return res
end end
---@param sect string|nil
---@param name string|nil
---@param silent boolean
local function get_path(sect, name, silent) local function get_path(sect, name, silent)
name = name or '' name = name or ''
sect = sect or '' sect = sect or ''
@ -287,7 +303,7 @@ local function get_path(sect, name, silent)
-- --
-- Finally, we can avoid relying on -S or -s here since they are very -- Finally, we can avoid relying on -S or -s here since they are very
-- inconsistently supported. Instead, call -w with a section and a name. -- inconsistently supported. Instead, call -w with a section and a name.
local cmd local cmd ---@type string[]
if sect == '' then if sect == '' then
cmd = { 'man', FIND_ARG, name } cmd = { 'man', FIND_ARG, name }
else else
@ -310,12 +326,14 @@ local function get_path(sect, name, silent)
end end
-- find any that match the specified name -- find any that match the specified name
---@param v string
local namematches = vim.tbl_filter(function(v) local namematches = vim.tbl_filter(function(v)
return fn.fnamemodify(v, ':t'):match(name) return fn.fnamemodify(v, ':t'):match(name)
end, results) or {} end, results) or {}
local sectmatches = {} local sectmatches = {}
if #namematches > 0 and sect ~= '' then if #namematches > 0 and sect ~= '' then
---@param v string
sectmatches = vim.tbl_filter(function(v) sectmatches = vim.tbl_filter(function(v)
return fn.fnamemodify(v, ':e') == sect return fn.fnamemodify(v, ':e') == sect
end, namematches) end, namematches)
@ -324,9 +342,12 @@ local function get_path(sect, name, silent)
return fn.substitute(sectmatches[1] or namematches[1] or results[1], [[\n\+$]], '', '') return fn.substitute(sectmatches[1] or namematches[1] or results[1], [[\n\+$]], '', '')
end end
---@param text string
---@param pat_or_re string
local function matchstr(text, pat_or_re) local function matchstr(text, pat_or_re)
local re = type(pat_or_re) == 'string' and vim.regex(pat_or_re) or pat_or_re local re = type(pat_or_re) == 'string' and vim.regex(pat_or_re) or pat_or_re
---@type integer, integer
local s, e = re:match_str(text) local s, e = re:match_str(text)
if s == nil then if s == nil then
@ -338,6 +359,8 @@ end
-- attempt to extract the name and sect out of 'name(sect)' -- attempt to extract the name and sect out of 'name(sect)'
-- otherwise just return the largest string of valid characters in ref -- otherwise just return the largest string of valid characters in ref
---@param ref string
---@return string, string
local function extract_sect_and_name_ref(ref) local function extract_sect_and_name_ref(ref)
ref = ref or '' ref = ref or ''
if ref:sub(1, 1) == '-' then -- try ':Man -pandoc' with this disabled. if ref:sub(1, 1) == '-' then -- try ':Man -pandoc' with this disabled.
@ -368,6 +391,9 @@ end
-- 2. If it still could not be found, then we try again without a section. -- 2. If it still could not be found, then we try again without a section.
-- 3. If still not found but $MANSECT is set, then we try again with $MANSECT -- 3. If still not found but $MANSECT is set, then we try again with $MANSECT
-- unset. -- unset.
---@param sect string?
---@param name string
---@param silent boolean?
local function verify_exists(sect, name, silent) local function verify_exists(sect, name, silent)
if sect and sect ~= '' then if sect and sect ~= '' then
local ret = get_path(sect, name, true) local ret = get_path(sect, name, true)
@ -416,6 +442,8 @@ local EXT_RE = vim.regex([[\.\%([glx]z\|bz2\|lzma\|Z\)$]])
-- more specific than what we provided to `man` (try `:Man 3 App::CLI`). -- more specific than what we provided to `man` (try `:Man 3 App::CLI`).
-- Also on linux, name seems to be case-insensitive. So for `:Man PRIntf`, we -- Also on linux, name seems to be case-insensitive. So for `:Man PRIntf`, we
-- still want the name of the buffer to be 'printf'. -- still want the name of the buffer to be 'printf'.
---@param path string
---@return string, string
local function extract_sect_and_name_path(path) local function extract_sect_and_name_path(path)
local tail = fn.fnamemodify(path, ':t') local tail = fn.fnamemodify(path, ':t')
if EXT_RE:match_str(path) then -- valid extensions if EXT_RE:match_str(path) then -- valid extensions
@ -425,6 +453,7 @@ local function extract_sect_and_name_path(path)
return sect, name return sect, name
end end
---@return boolean
local function find_man() local function find_man()
if vim.bo.filetype == 'man' then if vim.bo.filetype == 'man' then
return true return true
@ -442,6 +471,7 @@ local function find_man()
return false return false
end end
---@param pager boolean
local function set_options(pager) local function set_options(pager)
vim.bo.swapfile = false vim.bo.swapfile = false
vim.bo.buftype = 'nofile' vim.bo.buftype = 'nofile'
@ -453,11 +483,14 @@ local function set_options(pager)
vim.bo.filetype = 'man' vim.bo.filetype = 'man'
end end
---@param path string
---@param silent boolean?
---@return string
local function get_page(path, silent) local function get_page(path, silent)
-- Disable hard-wrap by using a big $MANWIDTH (max 1000 on some systems #9065). -- Disable hard-wrap by using a big $MANWIDTH (max 1000 on some systems #9065).
-- Soft-wrap: ftplugin/man.lua sets wrap/breakindent/…. -- Soft-wrap: ftplugin/man.lua sets wrap/breakindent/….
-- Hard-wrap: driven by `man`. -- Hard-wrap: driven by `man`.
local manwidth local manwidth ---@type integer|string
if (vim.g.man_hardwrap or 1) ~= 1 then if (vim.g.man_hardwrap or 1) ~= 1 then
manwidth = 999 manwidth = 999
elseif vim.env.MANWIDTH then elseif vim.env.MANWIDTH then
@ -478,6 +511,14 @@ local function get_page(path, silent)
}) })
end end
---@param lnum integer
---@return string
local function getline(lnum)
---@diagnostic disable-next-line
return fn.getline(lnum)
end
---@param page string
local function put_page(page) local function put_page(page)
vim.bo.modifiable = true vim.bo.modifiable = true
vim.bo.readonly = false vim.bo.readonly = false
@ -485,7 +526,7 @@ local function put_page(page)
api.nvim_buf_set_lines(0, 0, -1, false, vim.split(page, '\n')) api.nvim_buf_set_lines(0, 0, -1, false, vim.split(page, '\n'))
while fn.getline(1):match('^%s*$') do while getline(1):match('^%s*$') do
api.nvim_buf_set_lines(0, 0, 1, false, {}) api.nvim_buf_set_lines(0, 0, 1, false, {})
end end
-- XXX: nroff justifies text by filling it with whitespace. That interacts -- XXX: nroff justifies text by filling it with whitespace. That interacts
@ -512,13 +553,21 @@ local function format_candidate(path, psect)
return '' return ''
end end
---@generic T
---@param list T[]
---@param elem T
---@return T[]
local function move_elem_to_head(list, elem) local function move_elem_to_head(list, elem)
---@diagnostic disable-next-line:no-unknown
local list1 = vim.tbl_filter(function(v) local list1 = vim.tbl_filter(function(v)
return v ~= elem return v ~= elem
end, list) end, list)
return { elem, unpack(list1) } return { elem, unpack(list1) }
end end
---@param sect string
---@param name string
---@return string[]
local function get_paths(sect, name) local function get_paths(sect, name)
-- Try several sources for getting the list man directories: -- Try several sources for getting the list man directories:
-- 1. `man -w` (works on most systems) -- 1. `man -w` (works on most systems)
@ -533,6 +582,7 @@ local function get_paths(sect, name)
end end
local mandirs = table.concat(vim.split(mandirs_raw, '[:\n]', { trimempty = true }), ',') local mandirs = table.concat(vim.split(mandirs_raw, '[:\n]', { trimempty = true }), ',')
---@type string[]
local paths = fn.globpath(mandirs, 'man?/' .. name .. '*.' .. sect .. '*', false, true) local paths = fn.globpath(mandirs, 'man?/' .. name .. '*.' .. sect .. '*', false, true)
-- Prioritize the result from verify_exists as it obeys b:man_default_sects. -- Prioritize the result from verify_exists as it obeys b:man_default_sects.
@ -544,6 +594,10 @@ local function get_paths(sect, name)
return paths return paths
end end
---@param sect string
---@param psect string
---@param name string
---@return string[]
local function complete(sect, psect, name) local function complete(sect, psect, name)
local pages = get_paths(sect, name) local pages = get_paths(sect, name)
-- We remove duplicates in case the same manpage in different languages was found. -- We remove duplicates in case the same manpage in different languages was found.
@ -553,6 +607,8 @@ local function complete(sect, psect, name)
end end
-- see extract_sect_and_name_ref on why tolower(sect) -- see extract_sect_and_name_ref on why tolower(sect)
---@param arg_lead string
---@param cmd_line string
function M.man_complete(arg_lead, cmd_line, _) function M.man_complete(arg_lead, cmd_line, _)
local args = vim.split(cmd_line, '%s+', { trimempty = true }) local args = vim.split(cmd_line, '%s+', { trimempty = true })
local cmd_offset = fn.index(args, 'Man') local cmd_offset = fn.index(args, 'Man')
@ -589,6 +645,7 @@ function M.man_complete(arg_lead, cmd_line, _)
end end
if #args == 2 then if #args == 2 then
---@type string, string
local name, sect local name, sect
if arg_lead == '' then if arg_lead == '' then
-- cursor (|) is at ':Man 1 |' -- cursor (|) is at ':Man 1 |'
@ -618,10 +675,13 @@ function M.man_complete(arg_lead, cmd_line, _)
return complete(sect, sect, name) return complete(sect, sect, name)
end end
---@param pattern string
---@return {name:string,filename:string,cmd:string}[]
function M.goto_tag(pattern, _, _) function M.goto_tag(pattern, _, _)
local sect, name = extract_sect_and_name_ref(pattern) local sect, name = extract_sect_and_name_ref(pattern)
local paths = get_paths(sect, name) local paths = get_paths(sect, name)
---@type {name:string,title:string}[]
local structured = {} local structured = {}
for _, path in ipairs(paths) do for _, path in ipairs(paths) do
@ -634,6 +694,7 @@ function M.goto_tag(pattern, _, _)
end end
end end
---@param entry {name:string,title:string}
return vim.tbl_map(function(entry) return vim.tbl_map(function(entry)
return { return {
name = entry.name, name = entry.name,
@ -645,7 +706,7 @@ end
-- Called when Nvim is invoked as $MANPAGER. -- Called when Nvim is invoked as $MANPAGER.
function M.init_pager() function M.init_pager()
if fn.getline(1):match('^%s*$') then if getline(1):match('^%s*$') then
api.nvim_buf_set_lines(0, 0, 1, false, {}) api.nvim_buf_set_lines(0, 0, 1, false, {})
else else
vim.cmd('keepjumps 1') vim.cmd('keepjumps 1')
@ -653,7 +714,7 @@ function M.init_pager()
highlight_man_page() highlight_man_page()
-- Guess the ref from the heading (which is usually uppercase, so we cannot -- Guess the ref from the heading (which is usually uppercase, so we cannot
-- know the correct casing, cf. `man glDrawArraysInstanced`). -- know the correct casing, cf. `man glDrawArraysInstanced`).
local ref = fn.substitute(matchstr(fn.getline(1), [[^[^)]\+)]]) or '', ' ', '_', 'g') local ref = fn.substitute(matchstr(getline(1), [[^[^)]\+)]]) or '', ' ', '_', 'g')
local ok, res = pcall(extract_sect_and_name_ref, ref) local ok, res = pcall(extract_sect_and_name_ref, ref)
vim.b.man_sect = ok and res or '' vim.b.man_sect = ok and res or ''
@ -664,12 +725,14 @@ function M.init_pager()
set_options(true) set_options(true)
end end
---@param count integer
---@param args string[]
function M.open_page(count, smods, args) function M.open_page(count, smods, args)
if #args > 2 then if #args > 2 then
man_error('too many arguments') man_error('too many arguments')
end end
local ref local ref ---@type string
if #args == 0 then if #args == 0 then
ref = vim.bo.filetype == 'man' and fn.expand('<cWORD>') or fn.expand('<cword>') ref = vim.bo.filetype == 'man' and fn.expand('<cWORD>') or fn.expand('<cword>')
if ref == '' then if ref == '' then
@ -731,24 +794,27 @@ function M.read_page(ref)
end end
function M.show_toc() function M.show_toc()
local bufname = fn.bufname('%') local bufnr = api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(bufnr)
local info = fn.getloclist(0, { winid = 1 }) local info = fn.getloclist(0, { winid = 1 })
if info ~= '' and vim.w[info.winid].qf_toc == bufname then if info ~= '' and vim.w[info.winid].qf_toc == bufname then
vim.cmd.lopen() vim.cmd.lopen()
return return
end end
---@type {bufnr:integer, lnum:integer, text:string}[]
local toc = {} local toc = {}
local lnum = 2 local lnum = 2
local last_line = fn.line('$') - 1 local last_line = fn.line('$') - 1
local section_title_re = vim.regex([[^\%( \{3\}\)\=\S.*$]]) local section_title_re = vim.regex([[^\%( \{3\}\)\=\S.*$]])
local flag_title_re = vim.regex([[^\s\+\%(+\|-\)\S\+]]) local flag_title_re = vim.regex([[^\s\+\%(+\|-\)\S\+]])
while lnum and lnum < last_line do while lnum and lnum < last_line do
local text = fn.getline(lnum) local text = getline(lnum)
if section_title_re:match_str(text) then if section_title_re:match_str(text) then
-- if text is a section title -- if text is a section title
toc[#toc + 1] = { toc[#toc + 1] = {
bufnr = fn.bufnr('%'), bufnr = bufnr,
lnum = lnum, lnum = lnum,
text = text, text = text,
} }
@ -756,7 +822,7 @@ function M.show_toc()
-- if text is a flag title. we strip whitespaces and prepend two -- if text is a flag title. we strip whitespaces and prepend two
-- spaces to have a consistent format in the loclist. -- spaces to have a consistent format in the loclist.
toc[#toc + 1] = { toc[#toc + 1] = {
bufnr = fn.bufnr('%'), bufnr = bufnr,
lnum = lnum, lnum = lnum,
text = ' ' .. fn.substitute(text, [[^\s*\(.\{-}\)\s*$]], [[\1]], ''), text = ' ' .. fn.substitute(text, [[^\s*\(.\{-}\)\s*$]], [[\1]], ''),
} }
@ -772,7 +838,7 @@ end
local function init() local function init()
local path = get_path('', 'man', true) local path = get_path('', 'man', true)
local page local page ---@type string?
if path ~= nil then if path ~= nil then
-- Check for -l support. -- Check for -l support.
page = get_page(path, true) page = get_page(path, true)