Skip to content

Commit

Permalink
refactor: use simpler file existence check
Browse files Browse the repository at this point in the history
The vimscript function `getftype` is an easier way to check for file
existence compared to vim.uv.
  • Loading branch information
dundargoc committed Dec 1, 2024
1 parent 883bd79 commit 90c1c6c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/fennel_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ return {
root_dir = function(dir)
local has_fls_project_cfg = function(path)
local fnlpath = vim.fs.joinpath(path, 'flsproject.fnl')
return (vim.loop.fs_stat(fnlpath) or {}).type == 'file'
return vim.fn.getftype(fnlpath) == 'file'
end
return util.search_ancestors(dir, has_fls_project_cfg) or vim.fs.root(0, '.git')
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/turtle_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if bin_path == nil then
end
for _, p in ipairs(paths) do
local candidate = util.path.join(p, bin_name)
if (vim.loop.fs_stat(candidate) or {}).type == 'file' then
if vim.fn.getftype(candidate) == 'file' then
full_path = candidate
break
end
Expand Down
8 changes: 4 additions & 4 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function M.find_git_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
-- Support git directories and git files (worktrees)
local gitpath = M.path.join(path, '.git')
if vim.fn.isdirectory(gitpath) == 1 or (uv.fs_stat(gitpath) or {}).type == 'file' then
if vim.fn.isdirectory(gitpath) == 1 or (vim.fn.getftype(gitpath) == 'file') then
return path
end
end)
Expand All @@ -258,7 +258,7 @@ end
function M.find_package_json_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
local jsonpath = M.path.join(path, 'package.json')
if (uv.fs_stat(jsonpath) or {}).type == 'file' then
if vim.fn.getftype(jsonpath) == 'file' then
return path
end
end)
Expand Down Expand Up @@ -375,11 +375,11 @@ function M.path.is_dir(filename)
return vim.fn.isdirectory(filename) == 1
end

--- @deprecated use `(vim.loop.fs_stat(path) or {}).type == 'file'` instead
--- @deprecated use `vim.fn.getftype(path) == 'file'` instead
--- @param path string
--- @return boolean
function M.path.is_file(path)
return (vim.loop.fs_stat(path) or {}).type == 'file'
return vim.fn.getftype(path) == 'file'
end

--- @deprecated use `vim.fs.dirname` instead
Expand Down
6 changes: 3 additions & 3 deletions scripts/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local function make_section(indentlvl, sep, parts)
end

local function readfile(path)
assert((uv.fs_stat(path) or {}).type == 'file')
assert(vim.fn.getftype(path) == 'file')
return io.open(path):read '*a'
end

Expand Down Expand Up @@ -181,10 +181,10 @@ local function make_lsp_sections()
function()
local package_json_name = util.path.join(tempdir, config_name .. '.package.json')
if docs.package_json then
if not ((uv.fs_stat(package_json_name) or {}).type == 'file') then
if vim.fn.getftype(package_json_name) ~= 'file' then
os.execute(string.format('curl -v -L -o %q %q', package_json_name, docs.package_json))
end
if not ((uv.fs_stat(package_json_name) or {}).type == 'file') then
if vim.fn.getftype(package_json_name) ~= 'file' then
print(string.format('Failed to download package.json for %q at %q', config_name, docs.package_json))
os.exit(1)
return
Expand Down

0 comments on commit 90c1c6c

Please sign in to comment.