aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraltermo <107814000+altermo@users.noreply.github.com>2024-03-06 17:18:00 +0100
committerLewis Russell <me@lewisr.dev>2024-03-06 21:56:47 +0000
commitae5095cac9b233cfb6785534de6f084c70dc6424 (patch)
tree046ba9e14005d66d2ff7c48c53212403dd1d78fe
parent0101bdaa1ad8b09ebd5ef5551faf077f39be238c (diff)
downloadrneovim-ae5095cac9b233cfb6785534de6f084c70dc6424.tar.gz
rneovim-ae5095cac9b233cfb6785534de6f084c70dc6424.tar.bz2
rneovim-ae5095cac9b233cfb6785534de6f084c70dc6424.zip
fix(fs): use generics for better typing
-rw-r--r--runtime/doc/lua.txt4
-rw-r--r--runtime/lua/vim/fs.lua11
2 files changed, 9 insertions, 6 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
index d187caad04..4f8c685a41 100644
--- a/runtime/doc/lua.txt
+++ b/runtime/doc/lua.txt
@@ -2848,7 +2848,7 @@ vim.fs.basename({file}) *vim.fs.basename()*
Return the basename of the given path
Parameters: ~
- • {file} (`string`) Path
+ • {file} (`string?`) Path
Return: ~
(`string?`) Basename of {file}
@@ -2876,7 +2876,7 @@ vim.fs.dirname({file}) *vim.fs.dirname()*
Return the parent directory of the given path
Parameters: ~
- • {file} (`string`) Path
+ • {file} (`string?`) Path
Return: ~
(`string?`) Parent directory of {file}
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua
index 28a531707f..f9fe122f01 100644
--- a/runtime/lua/vim/fs.lua
+++ b/runtime/lua/vim/fs.lua
@@ -39,8 +39,9 @@ end
--- Return the parent directory of the given path
---
----@param file (string) Path
----@return string|nil Parent directory of {file}
+---@generic T : string|nil
+---@param file T Path
+---@return T Parent directory of {file}
function M.dirname(file)
if file == nil then
return nil
@@ -53,6 +54,7 @@ function M.dirname(file)
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 .. '/'
@@ -62,8 +64,9 @@ end
--- Return the basename of the given path
---
----@param file string Path
----@return string|nil Basename of {file}
+---@generic T : string|nil
+---@param file T Path
+---@return T Basename of {file}
function M.basename(file)
if file == nil then
return nil