fix(man.lua): tests, naming

This commit is contained in:
Justin M. Keyes 2023-03-07 15:13:09 +01:00
parent 160a019ffa
commit 304477ff35
3 changed files with 33 additions and 22 deletions

View File

@ -162,6 +162,8 @@ The following new APIs or features were added.
• |:highlight| now supports an additional attribute "altfont".
• |:Man| manpage viewer supports manpage names containing spaces.
• Treesitter captures can now be transformed by directives. This will allow
more complicated dynamic language injections.

View File

@ -384,7 +384,7 @@ local function extract_sect_and_name_ref(ref)
return sect, name
end
-- search_for_path attempts to find the path to a manpage
-- find_path attempts to find the path to a manpage
-- based on the passed section and name.
--
-- 1. If manpage could not be found with the given sect and name,
@ -395,7 +395,7 @@ end
-- 4. If a path still wasn't found, return nil.
---@param sect string?
---@param name string
function M.search_for_path(sect, name)
function M.find_path(sect, name)
if sect and sect ~= '' then
local ret = get_path(sect, name, true)
if ret then
@ -584,8 +584,8 @@ local function get_paths(sect, name)
---@type string[]
local paths = fn.globpath(mandirs, 'man?/' .. name .. '*.' .. sect .. '*', false, true)
-- Prioritize the result from search_for_path as it obeys b:man_default_sects.
local first = M.search_for_path(sect, name)
-- Prioritize the result from find_path as it obeys b:man_default_sects.
local first = M.find_path(sect, name)
if first then
paths = move_elem_to_head(paths, first)
end
@ -754,9 +754,9 @@ function M.open_page(count, smods, args)
end
-- Try both spaces and underscores, use the first that exists.
local path = M.search_for_path(sect, name)
local path = M.find_path(sect, name)
if path == nil then
path = M.search_for_path(sect, spaces_to_underscores(name))
path = M.find_path(sect, spaces_to_underscores(name))
if path == nil then
man_error('no manual entry for ' .. name)
end
@ -793,7 +793,7 @@ end
-- Called when a man:// buffer is opened.
function M.read_page(ref)
local sect, name = extract_sect_and_name_ref(ref)
local path = M.search_for_path(sect, name)
local path = M.find_path(sect, name)
if path == nil then
man_error('no manual entry for ' .. name)
end

View File

@ -8,27 +8,27 @@ local nvim_prog = helpers.nvim_prog
local matches = helpers.matches
local write_file = helpers.write_file
local tmpname = helpers.tmpname
local eq = helpers.eq
local skip = helpers.skip
local is_ci = helpers.is_ci
local table_contains = vim.tbl_contains
-- Returns a table composed of all man page name arguments
-- that were passed to search_for_path after attempting to
-- open 'name'.
-- Collects all names passed to find_path() after attempting ":Man foo".
local function get_search_history(name)
local as_table = string.gsub(name, ' ', '\', \'')
as_table = '\'' .. as_table .. '\''
local code = ([[
local args = vim.split(name, ' ')
local code = [[
local args = ...
local man = require('runtime.lua.man')
local res = {}
man.attempt_to_get_path = function(sect, name, silent)
man.find_path = function(sect, name)
table.insert(res, name)
return nil
end
pcall(man.open_page, 0, {tab = 0}, {%s})
local ok, rv = pcall(man.open_page, 0, {tab = 0}, args)
assert(not ok)
assert(rv and rv:match('no manual entry'))
return res
]]):format(as_table)
return exec_lua(code)
]]
return exec_lua(code, args)
end
clear()
@ -194,9 +194,18 @@ describe(':Man', function()
os.remove(actual_file)
end)
it('searches for manpage name with variants with spaces, underscores', function()
local tried = get_search_history('NAME WITH SPACES')
table_contains(tried, 'NAME WITH SPACES')
table_contains(tried, 'NAME_WITH_SPACES')
it('tries variants with spaces, underscores #22503', function()
eq({
'NAME WITH SPACES',
'NAME_WITH_SPACES',
}, get_search_history('NAME WITH SPACES'))
eq({
'some other man',
'some_other_man',
}, get_search_history('3 some other man'))
eq({
'other_man',
'other_man',
}, get_search_history('other_man(1)'))
end)
end)