aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/fs.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/fs.lua')
-rw-r--r--runtime/lua/vim/fs.lua32
1 files changed, 29 insertions, 3 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua
index d128c15233..8ea7f26575 100644
--- a/runtime/lua/vim/fs.lua
+++ b/runtime/lua/vim/fs.lua
@@ -1,5 +1,7 @@
local M = {}
+local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
+
--- Iterate over all the parents of the given file or directory.
---
--- Example:
@@ -40,7 +42,19 @@ function M.dirname(file)
if file == nil then
return nil
end
- return vim.fn.fnamemodify(file, ':h')
+ vim.validate({ file = { file, 's' } })
+ if iswin and file:match('^%w:[\\/]?$') then
+ return (file:gsub('\\', '/'))
+ elseif not file:match('[\\/]') then
+ return '.'
+ elseif file == '/' or file:match('^/[^/]+$') then
+ return '/'
+ end
+ 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:gsub('\\', '/'))
end
--- Return the basename of the given file or directory
@@ -48,7 +62,14 @@ end
---@param file (string) File or directory
---@return (string) Basename of {file}
function M.basename(file)
- return vim.fn.fnamemodify(file, ':t')
+ if file == nil then
+ return nil
+ end
+ vim.validate({ file = { file, 's' } })
+ if iswin and file:match('^%w:[\\/]?$') then
+ return ''
+ end
+ return file:match('[/\\]$') and '' or (file:match('[^\\/]*$'):gsub('\\', '/'))
end
--- Return an iterator over the files and directories located in {path}
@@ -229,7 +250,12 @@ end
---@return (string) Normalized path
function M.normalize(path)
vim.validate({ path = { path, 's' } })
- return (path:gsub('^~/', vim.env.HOME .. '/'):gsub('%$([%w_]+)', vim.env):gsub('\\', '/'))
+ return (
+ path
+ :gsub('^~/', vim.loop.os_homedir() .. '/')
+ :gsub('%$([%w_]+)', vim.loop.os_getenv)
+ :gsub('\\', '/')
+ )
end
return M