refactor(vim.fs): deduplicate by using vim.fn

Problem:
61e99217e6 replaced usages of `vim.fn`. This duplicates non-trivial
logic and may have introduced bugs like 38e38d1b40.

Later on, b02eeb6a72 graduated `fnamemodify` to `fast`, so avoiding it
in `vim.fs` is no longer necessary.

Solution:
- Use `vim.fn` to deduplicate logic and increase quality.
- Use `nvim -l` instead of `nvim -ll` in the test runner.
This commit is contained in:
Justin M. Keyes 2024-09-23 14:36:42 +02:00
parent 845e563421
commit 8917da46e8
2 changed files with 3 additions and 31 deletions

View File

@ -68,7 +68,7 @@ endif()
execute_process(
# Note: because of "-ll" (low-level interpreter mode), some modules like
# _editor.lua are not loaded.
COMMAND ${NVIM_PRG} -ll ${WORKING_DIR}/test/lua_runner.lua ${DEPS_INSTALL_DIR} busted -v -o test.busted.outputHandlers.nvim
COMMAND ${NVIM_PRG} -u NONE -l ${WORKING_DIR}/test/lua_runner.lua ${DEPS_INSTALL_DIR} busted -v -o test.busted.outputHandlers.nvim
--lazy --helper=${TEST_DIR}/${TEST_TYPE}/preload.lua
--lpath=${BUILD_DIR}/?.lua
--lpath=${WORKING_DIR}/runtime/lua/?.lua

View File

@ -62,27 +62,8 @@ end
---@param file T Path
---@return T Parent directory of {file}
function M.dirname(file)
if file == nil then
return nil
end
vim.validate('file', file, 'string')
if iswin then
file = file:gsub(os_sep, '/') --[[@as string]]
if file:match('^%w:/?$') then
return file
end
end
if not file:match('/') then
return '.'
elseif file == '/' or file:match('^/[^/]+$') then
return '/'
end
---@type string
local dir = file:match('/$') and file:sub(1, #file - 1) or file:match('^(/?.+)/')
if iswin and dir:match('^%w:$') then
return dir .. '/'
end
return dir
return vim.fn.fnamemodify(file, ':h')
end
--- Return the basename of the given path
@ -92,17 +73,8 @@ end
---@param file T Path
---@return T Basename of {file}
function M.basename(file)
if file == nil then
return nil
end
vim.validate('file', file, 'string')
if iswin then
file = file:gsub(os_sep, '/') --[[@as string]]
if file:match('^%w:/?$') then
return ''
end
end
return file:match('/$') and '' or (file:match('[^/]*$'))
return vim.fn.fnamemodify(file, ':t')
end
--- Concatenate directories and/or file paths into a single path with normalization