diff options
author | James Trew <66286082+jamestrew@users.noreply.github.com> | 2024-03-29 12:23:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-29 17:23:01 +0100 |
commit | 38e38d1b401e38459842f0e4da431e3dd6c2e527 (patch) | |
tree | 3784f17a2835641bdf576c230e2eddfc14bb04b3 /runtime/lua/vim/fs.lua | |
parent | 36acb2a8ec905069e382bb3b6db6b6ac677bce39 (diff) | |
download | rneovim-38e38d1b401e38459842f0e4da431e3dd6c2e527.tar.gz rneovim-38e38d1b401e38459842f0e4da431e3dd6c2e527.tar.bz2 rneovim-38e38d1b401e38459842f0e4da431e3dd6c2e527.zip |
fix(fs): allow backslash characters in unix paths
Backslashes are valid characters in unix style paths.
Fix the conversion of backslashes to forward slashes in several `vim.fs`
functions when not on Windows. On Windows, backslashes will still be converted
to forward slashes.
Diffstat (limited to 'runtime/lua/vim/fs.lua')
-rw-r--r-- | runtime/lua/vim/fs.lua | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index f9fe122f01..b7718ac87a 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -1,6 +1,7 @@ local M = {} local iswin = vim.uv.os_uname().sysname == 'Windows_NT' +local os_sep = iswin and '\\' or '/' --- Iterate over all the parents of the given path. --- @@ -47,19 +48,23 @@ function M.dirname(file) return nil end vim.validate({ file = { file, 's' } }) - if iswin and file:match('^%w:[\\/]?$') then - return (file:gsub('\\', '/')) - elseif not file:match('[\\/]') then + 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('^([/\\]?.+)[/\\]') + 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('\\', '/')) + return dir end --- Return the basename of the given path @@ -72,10 +77,13 @@ function M.basename(file) return nil end vim.validate({ file = { file, 's' } }) - if iswin and file:match('^%w:[\\/]?$') then - return '' + 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('[^\\/]*$'):gsub('\\', '/')) + return file:match('/$') and '' or (file:match('[^/]*$')) end --- Concatenate directories and/or file paths into a single path with normalization @@ -334,15 +342,16 @@ end --- @field expand_env boolean --- 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 ---- variables are also expanded. +--- beginning of the path is expanded to the user's home directory and +--- environment variables are also expanded. +--- +--- On Windows, backslash (\) characters are converted to forward slashes (/). --- --- Examples: --- --- ```lua --- vim.fs.normalize('C:\\\\Users\\\\jdoe') ---- -- 'C:/Users/jdoe' +--- -- On Windows: 'C:/Users/jdoe' --- --- vim.fs.normalize('~/src/neovim') --- -- '/home/jdoe/src/neovim' @@ -364,7 +373,7 @@ function M.normalize(path, opts) if path:sub(1, 1) == '~' then local home = vim.uv.os_homedir() or '~' - if home:sub(-1) == '\\' or home:sub(-1) == '/' then + if home:sub(-1) == os_sep then home = home:sub(1, -2) end path = home .. path:sub(2) @@ -374,7 +383,7 @@ function M.normalize(path, opts) path = path:gsub('%$([%w_]+)', vim.uv.os_getenv) end - path = path:gsub('\\', '/'):gsub('/+', '/') + path = path:gsub(os_sep, '/'):gsub('/+', '/') if iswin and path:match('^%w:/$') then return path end |