aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/lua.txt5
-rw-r--r--runtime/lua/vim/fs.lua34
-rw-r--r--runtime/lua/vim/loader.lua20
3 files changed, 34 insertions, 25 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
index 58522ac1f3..33833fa83e 100644
--- a/runtime/doc/lua.txt
+++ b/runtime/doc/lua.txt
@@ -2502,7 +2502,7 @@ find({names}, {opts}) *vim.fs.find()*
(table) Normalized paths |vim.fs.normalize()| of all matching files or
directories
-normalize({path}) *vim.fs.normalize()*
+normalize({path}, {opts}) *vim.fs.normalize()*
Normalize a path to a standard format. A tilde (~) character at the
beginning of the path is expanded to the user's home directory and any
backslash (\) characters are converted to forward slashes (/). Environment
@@ -2522,6 +2522,9 @@ normalize({path}) *vim.fs.normalize()*
Parameters: ~
• {path} (string) Path to normalize
+ • {opts} (table|nil) Options:
+ • expand_env: boolean Expand environment variables (default:
+ true)
Return: ~
(string) Normalized path
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua
index c6371e9163..407b334f20 100644
--- a/runtime/lua/vim/fs.lua
+++ b/runtime/lua/vim/fs.lua
@@ -321,16 +321,32 @@ end
--- </pre>
---
---@param path (string) Path to normalize
+---@param opts table|nil Options:
+--- - expand_env: boolean Expand environment variables (default: true)
---@return (string) Normalized path
-function M.normalize(path)
- vim.validate({ path = { path, 's' } })
- return (
- path
- :gsub('^~$', vim.loop.os_homedir())
- :gsub('^~/', vim.loop.os_homedir() .. '/')
- :gsub('%$([%w_]+)', vim.loop.os_getenv)
- :gsub('\\', '/')
- )
+function M.normalize(path, opts)
+ opts = opts or {}
+
+ vim.validate({
+ path = { path, { 'string' } },
+ expand_env = { opts.expand_env, { 'boolean' }, true },
+ })
+
+ if path:sub(1, 1) == '~' then
+ local home = vim.loop.os_homedir() or '~'
+ if home:sub(-1) == '\\' or home:sub(-1) == '/' then
+ home = home:sub(1, -2)
+ end
+ path = home .. path:sub(2)
+ end
+
+ if opts.expand_env == nil or opts.expand_env then
+ path = path:gsub('%$([%w_]+)', vim.loop.os_getenv)
+ end
+
+ path = path:gsub('\\', '/'):gsub('/+', '/')
+
+ return path:sub(-1) == '/' and path:sub(1, -2) or path
end
return M
diff --git a/runtime/lua/vim/loader.lua b/runtime/lua/vim/loader.lua
index 19df7e4383..eccf95e2f2 100644
--- a/runtime/lua/vim/loader.lua
+++ b/runtime/lua/vim/loader.lua
@@ -49,19 +49,9 @@ function Loader.track(stat, start)
Loader._stats[stat].time = Loader._stats[stat].time + uv.hrtime() - start
end
---- slightly faster/different version than vim.fs.normalize
---- we also need to have it here, since the loader will load vim.fs
---@private
-function Loader.normalize(path)
- if path:sub(1, 1) == '~' then
- local home = vim.loop.os_homedir() or '~'
- if home:sub(-1) == '\\' or home:sub(-1) == '/' then
- home = home:sub(1, -2)
- end
- path = home .. path:sub(2)
- end
- path = path:gsub('\\', '/'):gsub('/+', '/')
- return path:sub(-1) == '/' and path:sub(1, -2) or path
+local function normalize(path)
+ return vim.fs.normalize(path, { expand_env = false })
end
--- Gets the rtp excluding after directories.
@@ -80,7 +70,7 @@ function Loader.get_rtp()
if key ~= Loader._rtp_key then
Loader._rtp = {}
for _, path in ipairs(vim.api.nvim_get_runtime_file('', true)) do
- path = Loader.normalize(path)
+ path = normalize(path)
-- skip after directories
if
path:sub(-6, -1) ~= '/after'
@@ -210,7 +200,7 @@ end
-- luacheck: ignore 312
function Loader.loadfile(filename, mode, env, hash)
local start = uv.hrtime()
- filename = Loader.normalize(filename)
+ filename = normalize(filename)
mode = nil -- ignore mode, since we byte-compile the lua source files
local chunk, err = Loader.load(filename, { mode = mode, env = env, hash = hash })
Loader.track('loadfile', start)
@@ -383,7 +373,7 @@ end
---@param path string? path to reset
function M.reset(path)
if path then
- Loader._indexed[Loader.normalize(path)] = nil
+ Loader._indexed[normalize(path)] = nil
else
Loader._indexed = {}
end