fix(lua): change some vim.fn.expand() to vim.fs.normalize() (#29583)

Unlike vim.fn.expand(), vim.fs.normalize() doesn't expand wildcards.
This commit is contained in:
zeertzjq
2024-07-09 19:17:50 +08:00
committed by GitHub
parent fb6c059dc5
commit 487f44a6c1
3 changed files with 16 additions and 16 deletions

View File

@ -50,11 +50,11 @@ local function check_config()
local init_lua = vim.fn.stdpath('config') .. '/init.lua' local init_lua = vim.fn.stdpath('config') .. '/init.lua'
local init_vim = vim.fn.stdpath('config') .. '/init.vim' local init_vim = vim.fn.stdpath('config') .. '/init.vim'
local vimrc = vim.env.MYVIMRC and vim.fn.expand(vim.env.MYVIMRC) or init_lua local vimrc = vim.env.MYVIMRC and vim.fs.normalize(vim.env.MYVIMRC) or init_lua
if vim.fn.filereadable(vimrc) == 0 and vim.fn.filereadable(init_vim) == 0 then if vim.fn.filereadable(vimrc) == 0 and vim.fn.filereadable(init_vim) == 0 then
ok = false ok = false
local has_vim = vim.fn.filereadable(vim.fn.expand('~/.vimrc')) == 1 local has_vim = vim.fn.filereadable(vim.fs.normalize('~/.vimrc')) == 1
health.warn( health.warn(
('%s user config file: %s'):format( ('%s user config file: %s'):format(
-1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable', -1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable',
@ -114,7 +114,7 @@ local function check_config()
) )
shadafile = ( shadafile = (
vim.o.shadafile == '' vim.o.shadafile == ''
and (shadafile == '' and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fn.expand( and (shadafile == '' and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fs.normalize(
shadafile shadafile
)) ))
or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile) or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile)

View File

@ -136,7 +136,7 @@ function M.open(path)
}) })
local is_uri = path:match('%w+:') local is_uri = path:match('%w+:')
if not is_uri then if not is_uri then
path = vim.fn.expand(path) path = vim.fs.normalize(path)
end end
local cmd --- @type string[] local cmd --- @type string[]

View File

@ -1223,7 +1223,7 @@ end
function M._test() function M._test()
tagmap = get_helptags('$VIMRUNTIME/doc') tagmap = get_helptags('$VIMRUNTIME/doc')
helpfiles = get_helpfiles(vim.fn.expand('$VIMRUNTIME/doc')) helpfiles = get_helpfiles(vim.fs.normalize('$VIMRUNTIME/doc'))
ok(vim.tbl_count(tagmap) > 3000, '>3000', vim.tbl_count(tagmap)) ok(vim.tbl_count(tagmap) > 3000, '>3000', vim.tbl_count(tagmap))
ok( ok(
@ -1285,7 +1285,7 @@ function M.gen(help_dir, to_dir, include, commit, parser_path)
help_dir = { help_dir = {
help_dir, help_dir,
function(d) function(d)
return vim.fn.isdirectory(vim.fn.expand(d)) == 1 return vim.fn.isdirectory(vim.fs.normalize(d)) == 1
end, end,
'valid directory', 'valid directory',
}, },
@ -1295,7 +1295,7 @@ function M.gen(help_dir, to_dir, include, commit, parser_path)
parser_path = { parser_path = {
parser_path, parser_path,
function(f) function(f)
return f == nil or vim.fn.filereadable(vim.fn.expand(f)) == 1 return f == nil or vim.fn.filereadable(vim.fs.normalize(f)) == 1
end, end,
'valid vimdoc.{so,dll} filepath', 'valid vimdoc.{so,dll} filepath',
}, },
@ -1303,10 +1303,10 @@ function M.gen(help_dir, to_dir, include, commit, parser_path)
local err_count = 0 local err_count = 0
ensure_runtimepath() ensure_runtimepath()
tagmap = get_helptags(vim.fn.expand(help_dir)) tagmap = get_helptags(vim.fs.normalize(help_dir))
helpfiles = get_helpfiles(help_dir, include) helpfiles = get_helpfiles(help_dir, include)
to_dir = vim.fn.expand(to_dir) to_dir = vim.fs.normalize(to_dir)
parser_path = parser_path and vim.fn.expand(parser_path) or nil parser_path = parser_path and vim.fs.normalize(parser_path) or nil
print(('output dir: %s'):format(to_dir)) print(('output dir: %s'):format(to_dir))
vim.fn.mkdir(to_dir, 'p') vim.fn.mkdir(to_dir, 'p')
@ -1358,7 +1358,7 @@ function M.validate(help_dir, include, parser_path)
help_dir = { help_dir = {
help_dir, help_dir,
function(d) function(d)
return vim.fn.isdirectory(vim.fn.expand(d)) == 1 return vim.fn.isdirectory(vim.fs.normalize(d)) == 1
end, end,
'valid directory', 'valid directory',
}, },
@ -1366,7 +1366,7 @@ function M.validate(help_dir, include, parser_path)
parser_path = { parser_path = {
parser_path, parser_path,
function(f) function(f)
return f == nil or vim.fn.filereadable(vim.fn.expand(f)) == 1 return f == nil or vim.fn.filereadable(vim.fs.normalize(f)) == 1
end, end,
'valid vimdoc.{so,dll} filepath', 'valid vimdoc.{so,dll} filepath',
}, },
@ -1374,9 +1374,9 @@ function M.validate(help_dir, include, parser_path)
local err_count = 0 ---@type integer local err_count = 0 ---@type integer
local files_to_errors = {} ---@type table<string, string[]> local files_to_errors = {} ---@type table<string, string[]>
ensure_runtimepath() ensure_runtimepath()
tagmap = get_helptags(vim.fn.expand(help_dir)) tagmap = get_helptags(vim.fs.normalize(help_dir))
helpfiles = get_helpfiles(help_dir, include) helpfiles = get_helpfiles(help_dir, include)
parser_path = parser_path and vim.fn.expand(parser_path) or nil parser_path = parser_path and vim.fs.normalize(parser_path) or nil
for _, f in ipairs(helpfiles) do for _, f in ipairs(helpfiles) do
local helpfile = vim.fs.basename(f) local helpfile = vim.fs.basename(f)
@ -1411,7 +1411,7 @@ end
--- ---
--- @param help_dir? string e.g. '$VIMRUNTIME/doc' or './runtime/doc' --- @param help_dir? string e.g. '$VIMRUNTIME/doc' or './runtime/doc'
function M.run_validate(help_dir) function M.run_validate(help_dir)
help_dir = vim.fn.expand(help_dir or '$VIMRUNTIME/doc') help_dir = vim.fs.normalize(help_dir or '$VIMRUNTIME/doc')
print('doc path = ' .. vim.uv.fs_realpath(help_dir)) print('doc path = ' .. vim.uv.fs_realpath(help_dir))
local rv = M.validate(help_dir) local rv = M.validate(help_dir)
@ -1438,7 +1438,7 @@ end
--- @param help_dir? string e.g. '$VIMRUNTIME/doc' or './runtime/doc' --- @param help_dir? string e.g. '$VIMRUNTIME/doc' or './runtime/doc'
function M.test_gen(help_dir) function M.test_gen(help_dir)
local tmpdir = vim.fs.dirname(vim.fn.tempname()) local tmpdir = vim.fs.dirname(vim.fn.tempname())
help_dir = vim.fn.expand(help_dir or '$VIMRUNTIME/doc') help_dir = vim.fs.normalize(help_dir or '$VIMRUNTIME/doc')
print('doc path = ' .. vim.uv.fs_realpath(help_dir)) print('doc path = ' .. vim.uv.fs_realpath(help_dir))
-- Because gen() is slow (~30s), this test is limited to a few files. -- Because gen() is slow (~30s), this test is limited to a few files.