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". • |: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 • Treesitter captures can now be transformed by directives. This will allow
more complicated dynamic language injections. more complicated dynamic language injections.

View File

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

View File

@ -8,27 +8,27 @@ local nvim_prog = helpers.nvim_prog
local matches = helpers.matches local matches = helpers.matches
local write_file = helpers.write_file local write_file = helpers.write_file
local tmpname = helpers.tmpname local tmpname = helpers.tmpname
local eq = helpers.eq
local skip = helpers.skip local skip = helpers.skip
local is_ci = helpers.is_ci local is_ci = helpers.is_ci
local table_contains = vim.tbl_contains
-- Returns a table composed of all man page name arguments -- Collects all names passed to find_path() after attempting ":Man foo".
-- that were passed to search_for_path after attempting to
-- open 'name'.
local function get_search_history(name) local function get_search_history(name)
local as_table = string.gsub(name, ' ', '\', \'') local args = vim.split(name, ' ')
as_table = '\'' .. as_table .. '\'' local code = [[
local code = ([[ local args = ...
local man = require('runtime.lua.man') local man = require('runtime.lua.man')
local res = {} local res = {}
man.attempt_to_get_path = function(sect, name, silent) man.find_path = function(sect, name)
table.insert(res, name) table.insert(res, name)
return nil return nil
end 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 return res
]]):format(as_table) ]]
return exec_lua(code) return exec_lua(code, args)
end end
clear() clear()
@ -194,9 +194,18 @@ describe(':Man', function()
os.remove(actual_file) os.remove(actual_file)
end) end)
it('searches for manpage name with variants with spaces, underscores', function() it('tries variants with spaces, underscores #22503', function()
local tried = get_search_history('NAME WITH SPACES') eq({
table_contains(tried, 'NAME WITH SPACES') 'NAME WITH SPACES',
table_contains(tried, '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)
end) end)