Skip to content

Commit

Permalink
refactor: deprecate util.path.exists
Browse files Browse the repository at this point in the history
Use `vim.uv.fs_stat` instead.

Work on neovim#2079.
  • Loading branch information
dundargoc committed Nov 27, 2024
1 parent d651732 commit d063b5a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/ci/run_sanitizer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANC
exit 1
fi

SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize)'
SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists)'

if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then
echo
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ return {
-- Support Yarn2 (PnP) projects
local pnp_cjs = util.path.join(new_root_dir, '.pnp.cjs')
local pnp_js = util.path.join(new_root_dir, '.pnp.js')
if util.path.exists(pnp_cjs) or util.path.exists(pnp_js) then
if vim.loop.fs_stat(pnp_cjs) or vim.loop.fs_stat(pnp_js) then
config.cmd = vim.list_extend({ 'yarn', 'exec' }, config.cmd)
end
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/foam_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return {
filetypes = { 'foam', 'OpenFOAM' },
root_dir = function(fname)
return util.search_ancestors(fname, function(path)
if util.path.exists(util.path.join(path, 'system', 'controlDict')) then
if vim.loop.fs_stat(util.path.join(path, 'system', 'controlDict')) then
return path
end
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/relay_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ return {
if config.path_to_config then
config.path_to_config = vim.fs.normalize(config.path_to_config)
local path_to_config = util.path.join(root_dir, config.path_to_config)
if util.path.exists(path_to_config) then
if vim.loop.fs_stat(path_to_config) then
vim.list_extend(config.cmd, { config.path_to_config })
vim.list_extend(compiler_cmd, { config.path_to_config })
else
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/vdmj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local util = require 'lspconfig.util'

local function get_default_mavenrepo()
local repo = util.path.join(vim.env.HOME, '.m2', 'repository', 'dk', 'au', 'ece', 'vdmj')
if util.path.exists(repo) then
if vim.loop.fs_stat(repo) then
return repo
else
return util.path.join(vim.env.HOME, '.m2', 'repository', 'com', 'fujitsu')
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/volar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ local function get_typescript_server_path(root_dir)
local found_ts = ''
local function check_dir(path)
found_ts = util.path.join(path, 'node_modules', 'typescript', 'lib')
if util.path.exists(found_ts) then
if vim.uv.fs_stat(found_ts) then
return path
end
end
Expand Down
32 changes: 20 additions & 12 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,24 @@ M.path = (function()
return path:gsub('([%[%]%?%*])', '\\%1')
end

--- @param filename string
--- @return string|false
local function exists(filename)
local stat = uv.fs_stat(filename)
return stat and stat.type or false
end

--- @param filename string
--- @return boolean
local function is_dir(filename)
return exists(filename) == 'directory'
local stat = uv.fs_stat(filename)
if not stat then
return false
end
return stat.type == 'directory'
end

--- @param filename string
--- @return boolean
local function is_file(filename)
return exists(filename) == 'file'
local stat = uv.fs_stat(filename)
if not stat then
return false
end
return stat.type == 'file'
end

--- @param path string
Expand Down Expand Up @@ -203,7 +204,6 @@ M.path = (function()
is_dir = is_dir,
is_file = is_file,
is_absolute = is_absolute,
exists = exists,
join = path_join,
traverse_parents = traverse_parents,
iterate_parents = iterate_parents,
Expand Down Expand Up @@ -246,7 +246,7 @@ function M.root_pattern(...)
for _, pattern in ipairs(patterns) do
local match = M.search_ancestors(startpath, function(path)
for _, p in ipairs(vim.fn.glob(M.path.join(M.path.escape_wildcards(path), pattern), true, true)) do
if M.path.exists(p) then
if uv.fs_stat(p) then
return path
end
end
Expand Down Expand Up @@ -395,10 +395,18 @@ function M.strip_archive_subpath(path)
return path
end

---@deprecated use `vim.fs.dirname` instead
--- @deprecated use `vim.fs.dirname` instead
M.dirname = vim.fs.dirname

--- @deprecated use `vim.fs.normalize` instead
M.sanitize = vim.fs.normalize

--- @deprecated use `vim.loop.fs_stat` instead
--- @param filename string
--- @return string|false
function M.exists(filename)
local stat = uv.fs_stat(filename)
return stat and stat.type or false
end

return M
13 changes: 4 additions & 9 deletions test/lspconfig_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,27 @@ describe('lspconfig', function()
end)
describe('exists', function()
it('is present directory', function()
local lspconfig = require 'lspconfig'

local cwd = vim.fn.getcwd()
eq(true, lspconfig.util.path.exists(cwd) ~= false)
eq(true, vim.uv.fs_stat(cwd) ~= nil)
end)

it('is not present directory', function()
local lspconfig = require 'lspconfig'
local not_exist_dir = vim.fn.getcwd() .. '/not/exists'
eq(true, lspconfig.util.path.exists(not_exist_dir) == false)
eq(true, vim.uv.fs_stat(not_exist_dir) == nil)
end)

it('is present file', function()
local lspconfig = require 'lspconfig'
-- change the working directory to test directory
vim.api.nvim_command 'cd ./test/test_dir/'
local file = vim.fn.getcwd() .. '/root_marker.txt'
eq(true, lspconfig.util.path.exists(file) ~= false)
eq(true, vim.uv.fs_stat(file) ~= nil)
end)

it('is not present file', function()
local lspconfig = require 'lspconfig'
-- change the working directory to test directory
vim.api.nvim_command 'cd ./test/test_dir/'
local file = vim.fn.getcwd() .. '/not_exists.txt'
assert.is_false(lspconfig.util.path.exists(file))
assert.is_false(vim.uv.fs_stat(file) ~= nil)
end)
end)

Expand Down

0 comments on commit d063b5a

Please sign in to comment.