feat: refactor all vim.loop -> vim.uv and add a shim when needed

This commit is contained in:
Folke Lemaitre 2024-03-22 08:58:36 +01:00
parent 83493db50a
commit 9e157df077
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
19 changed files with 53 additions and 51 deletions

View File

@ -1,4 +1,4 @@
local uv = vim.loop
local uv = vim.uv
local M = {}
@ -51,7 +51,7 @@ end
---@private
function Loader.normalize(path)
if path:sub(1, 1) == "~" then
local home = vim.loop.os_homedir() or "~"
local home = vim.uv.os_homedir() or "~"
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
home = home:sub(1, -2)
end
@ -222,7 +222,7 @@ end
--- Loads the given module path using the cache
---@param modpath string
---@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table} (table|nil) Options for loading the module:
--- - hash: (table) the hash of the file to load if it is already known. (defaults to `vim.loop.fs_stat({modpath})`)
--- - hash: (table) the hash of the file to load if it is already known. (defaults to `vim.uv.fs_stat({modpath})`)
--- - mode: (string) the mode to load the module with. "b"|"t"|"bt" (defaults to `nil`)
--- - env: (table) the environment to load the module in. (defaults to `nil`)
---@see |luaL_loadfile()|
@ -442,9 +442,9 @@ function Loader.lsmod(path)
if not Loader._indexed[path] then
local start = uv.hrtime()
Loader._indexed[path] = {}
local handle = vim.loop.fs_scandir(path .. "/lua")
local handle = vim.uv.fs_scandir(path .. "/lua")
while handle do
local name, t = vim.loop.fs_scandir_next(handle)
local name, t = vim.uv.fs_scandir_next(handle)
if not name then
break
end
@ -480,7 +480,7 @@ function M._profile_loaders()
for l, loader in pairs(package.loaders) do
local loc = debug.getinfo(loader, "Sn").source:sub(2)
package.loaders[l] = function(modname)
local start = vim.loop.hrtime()
local start = vim.uv.hrtime()
local ret = loader(modname)
Loader.track("loader " .. l .. ": " .. loc, start)
Loader.track("loader_all", start)

View File

@ -17,7 +17,7 @@ M.defaults = {
-- leave nil when passing the spec as the first argument to setup()
spec = nil, ---@type LazySpec
lockfile = vim.fn.stdpath("config") .. "/lazy-lock.json", -- lockfile generated after running update.
concurrency = jit.os:find("Windows") and (vim.loop.available_parallelism() * 2) or nil, ---@type number limit the maximum amount of concurrent tasks
concurrency = jit.os:find("Windows") and (vim.uv.available_parallelism() * 2) or nil, ---@type number limit the maximum amount of concurrent tasks
git = {
-- defaults for the `Lazy log` command
-- log = { "--since=3 days ago" }, -- show commits from the last 3 days

View File

@ -472,7 +472,7 @@ function M.add_to_rtp(plugin)
table.insert(rtp, idx_dir or (#rtp + 1), plugin.dir)
local after = plugin.dir .. "/after"
if vim.loop.fs_stat(after) then
if vim.uv.fs_stat(after) then
table.insert(rtp, idx_after or (#rtp + 1), after)
end
@ -495,7 +495,7 @@ function M.colorscheme(name)
if not plugin._.loaded then
for _, ext in ipairs({ "lua", "vim" }) do
local path = plugin.dir .. "/colors/" .. name .. "." .. ext
if vim.loop.fs_stat(path) then
if vim.uv.fs_stat(path) then
return M.load(plugin, { colorscheme = name })
end
end

View File

@ -12,7 +12,7 @@ function M.track(data, time)
if data then
local entry = {
data = data,
time = time or vim.loop.hrtime(),
time = time or vim.uv.hrtime(),
}
table.insert(M._profiles[#M._profiles], entry)
@ -23,7 +23,7 @@ function M.track(data, time)
else
---@type LazyProfile
local entry = table.remove(M._profiles)
entry.time = vim.loop.hrtime() - entry.time
entry.time = vim.uv.hrtime() - entry.time
return entry
end
end
@ -54,7 +54,7 @@ end
---@return string
function M.norm(path)
if path:sub(1, 1) == "~" then
local home = vim.loop.os_homedir()
local home = vim.uv.os_homedir()
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
home = home:sub(1, -2)
end
@ -175,9 +175,9 @@ end
---@param path string
---@param fn fun(path: string, name:string, type:FileType):boolean?
function M.ls(path, fn)
local handle = vim.loop.fs_scandir(path)
local handle = vim.uv.fs_scandir(path)
while handle do
local name, t = vim.loop.fs_scandir_next(handle)
local name, t = vim.uv.fs_scandir_next(handle)
if not name then
break
end
@ -187,7 +187,7 @@ function M.ls(path, fn)
-- HACK: type is not always returned due to a bug in luv,
-- so fecth it with fs_stat instead when needed.
-- see https://github.com/folke/lazy.nvim/issues/306
if fn(fname, name, t or vim.loop.fs_stat(fname).type) == false then
if fn(fname, name, t or vim.uv.fs_stat(fname).type) == false then
break
end
end
@ -263,7 +263,7 @@ function M.lsmod(modname, fn)
return
end
if vim.loop.fs_stat(root .. ".lua") then
if vim.uv.fs_stat(root .. ".lua") then
fn(modname, root .. ".lua")
end
@ -272,7 +272,7 @@ function M.lsmod(modname, fn)
fn(modname, path)
elseif (type == "file" or type == "link") and name:sub(-4) == ".lua" then
fn(modname .. "." .. name:sub(1, -5), path)
elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then
elseif type == "directory" and vim.uv.fs_stat(path .. "/init.lua") then
fn(modname .. "." .. name, path .. "/init.lua")
end
end)

View File

@ -26,7 +26,7 @@ function M.check()
local existing = false
for _, site in pairs(sites) do
for _, packs in ipairs(vim.fn.expand(site .. "/pack/*", false, true)) do
if not packs:find("[/\\]dist$") and vim.loop.fs_stat(packs) then
if not packs:find("[/\\]dist$") and vim.uv.fs_stat(packs) then
existing = true
warn("found existing packages at `" .. packs .. "`")
end
@ -46,7 +46,7 @@ function M.check()
end
local packer_compiled = vim.fn.stdpath("config") .. "/plugin/packer_compiled.lua"
if vim.loop.fs_stat(packer_compiled) then
if vim.uv.fs_stat(packer_compiled) then
error("please remove the file `" .. packer_compiled .. "`")
else
ok("packer_compiled.lua not found")

View File

@ -4,7 +4,7 @@ local Util = require("lazy.util")
local M = {}
function M.index(plugin)
if Config.options.readme.skip_if_doc_exists and vim.loop.fs_stat(plugin.dir .. "/doc") then
if Config.options.readme.skip_if_doc_exists and vim.uv.fs_stat(plugin.dir .. "/doc") then
return {}
end
@ -17,7 +17,7 @@ function M.index(plugin)
local tags = {}
for _, file in ipairs(files) do
file = Util.norm(file)
if vim.loop.fs_stat(file) then
if vim.uv.fs_stat(file) then
local rel_file = file:sub(#plugin.dir + 1)
local tag_filename = plugin.name .. vim.fn.fnamemodify(rel_file, ":h"):gsub("%W+", "-"):gsub("^%-$", "")
local lines = vim.split(Util.read_file(file), "\n")
@ -50,7 +50,7 @@ function M.update()
Util.ls(docs, function(path, name, type)
if type == "file" and name:sub(-2) == "md" then
vim.loop.fs_unlink(path)
vim.uv.fs_unlink(path)
end
end)
---@type {file:string, tag:string, line:string}[]

View File

@ -2,6 +2,8 @@
local M = {}
M._start = 0
vim.uv = vim.uv or vim.uv
local function profile_require()
local done = {} ---@type table<string, true>
local r = require
@ -35,7 +37,7 @@ function M.setup(spec, opts)
opts.spec = spec
end
M._start = M._start == 0 and vim.loop.hrtime() or M._start
M._start = M._start == 0 and vim.uv.hrtime() or M._start
if vim.g.lazy_did_setup then
return vim.notify(
"Re-sourcing your config is not supported with lazy.nvim",
@ -53,7 +55,7 @@ function M.setup(spec, opts)
if not (pcall(require, "ffi") and jit and jit.version) then
return vim.notify("lazy.nvim requires Neovim built with LuaJIT", vim.log.levels.ERROR, { title = "lazy.nvim" })
end
local start = vim.loop.hrtime()
local start = vim.uv.hrtime()
-- use the Neovim cache if available
if vim.loader and vim.fn.has("nvim-0.9.1") == 1 then
@ -89,7 +91,7 @@ function M.setup(spec, opts)
end
Util.track({ plugin = "lazy.nvim" }) -- setup start
Util.track("module", vim.loop.hrtime() - start)
Util.track("module", vim.uv.hrtime() - start)
-- load config
Util.track("config")
@ -100,7 +102,7 @@ function M.setup(spec, opts)
Loader.setup()
-- correct time delta and loaded
local delta = vim.loop.hrtime() - start
local delta = vim.uv.hrtime() - start
Util.track().time = delta -- end setup
if Config.plugins["lazy.nvim"] then
Config.plugins["lazy.nvim"]._.loaded = { time = delta, source = "init.lua" }
@ -120,7 +122,7 @@ end
function M.bootstrap()
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
if not vim.uv.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",

View File

@ -41,7 +41,7 @@ M.signals = {
}
---@diagnostic disable-next-line: no-unknown
local uv = vim.loop
local uv = vim.uv
---@class ProcessOpts
---@field args string[]

View File

@ -16,7 +16,7 @@ function M.enable()
M.timer:stop()
end
if #Config.spec.modules > 0 then
M.timer = assert(vim.loop.new_timer())
M.timer = assert(vim.uv.new_timer())
M.check(true)
M.timer:start(2000, 2000, M.check)
end
@ -44,7 +44,7 @@ function M.check(start)
-- spec is a module
local function check(_, modpath)
checked[modpath] = true
local hash = vim.loop.fs_stat(modpath)
local hash = vim.uv.fs_stat(modpath)
if hash then
if M.files[modpath] then
if not M.eq(M.files[modpath], hash) then

View File

@ -92,7 +92,7 @@ function Runner:start()
end
end
local check = vim.loop.new_check()
local check = vim.uv.new_check()
check:start(function()
if self:resume() then
return

View File

@ -12,17 +12,17 @@ M.clean = {
local dir = self.plugin.dir:gsub("/+$", "")
assert(dir:find(Config.options.root, 1, true) == 1, self.plugin.dir .. " should be under packpath!")
local stat = vim.loop.fs_lstat(dir)
local stat = vim.uv.fs_lstat(dir)
assert(stat and stat.type == "directory", self.plugin.dir .. " should be a directory!")
Util.walk(dir, function(path, _, type)
if type == "directory" then
vim.loop.fs_rmdir(path)
vim.uv.fs_rmdir(path)
else
vim.loop.fs_unlink(path)
vim.uv.fs_unlink(path)
end
end)
vim.loop.fs_rmdir(dir)
vim.uv.fs_rmdir(dir)
self.plugin._.installed = false
end,

View File

@ -15,7 +15,7 @@ M.log = {
if opts.updated and not (plugin._.updated and plugin._.updated.from ~= plugin._.updated.to) then
return true
end
local stat = vim.loop.fs_stat(plugin.dir .. "/.git")
local stat = vim.uv.fs_stat(plugin.dir .. "/.git")
return not (stat and stat.type == "directory")
end,
---@param opts {args?: string[], updated?:boolean, check?:boolean}
@ -106,7 +106,7 @@ M.clone = {
self.plugin._.cloned = true
self.plugin._.installed = true
self.plugin._.dirty = true
vim.loop.fs_unlink(marker)
vim.uv.fs_unlink(marker)
end
end,
})

View File

@ -65,7 +65,7 @@ function Task:start()
self:start()
end)
end
self._started = vim.loop.hrtime()
self._started = vim.uv.hrtime()
---@type boolean, string|any
local ok, err = pcall(self._task, self, self._opts)
if not ok then
@ -81,7 +81,7 @@ function Task:_check()
return
end
end
self._ended = vim.loop.hrtime()
self._ended = vim.uv.hrtime()
if self._opts.on_done then
self._opts.on_done(self)
end
@ -97,7 +97,7 @@ function Task:time()
return 0
end
if not self:is_done() then
return (vim.loop.hrtime() - self._started) / 1e6
return (vim.uv.hrtime() - self._started) / 1e6
end
return (self._ended - self._started) / 1e6
end

View File

@ -54,7 +54,7 @@ function M.cputime()
end
local function fallback()
return (vim.loop.hrtime() - require("lazy")._start) / 1e6
return (vim.uv.hrtime() - require("lazy")._start) / 1e6
end
local ok, ret = pcall(real)

View File

@ -2,7 +2,7 @@
local M = setmetatable({}, { __index = require("lazy.core.util") })
function M.file_exists(file)
return vim.loop.fs_stat(file) ~= nil
return vim.uv.fs_stat(file) ~= nil
end
---@param opts? LazyFloatOptions
@ -71,7 +71,7 @@ end
---@param fn F
---@return F
function M.throttle(ms, fn)
local timer = vim.loop.new_timer()
local timer = vim.uv.new_timer()
local running = false
local first = true

View File

@ -28,8 +28,8 @@ describe("lazy", function()
"folke/paint.nvim",
}, { install_missing = true, defaults = { lazy = true } })
assert(3 == vim.tbl_count(Config.plugins))
assert(vim.loop.fs_stat(root .. "/paint.nvim/README.md"))
assert(vim.loop.fs_stat(root .. "/neodev.nvim/README.md"))
assert(vim.uv.fs_stat(root .. "/paint.nvim/README.md"))
assert(vim.uv.fs_stat(root .. "/neodev.nvim/README.md"))
assert(not neodev)
assert(Config.plugins["neodev.nvim"]._.installed)
assert(not Config.plugins["neodev.nvim"]._.is_local)

View File

@ -12,7 +12,7 @@ describe("util", function()
end
end
Helpers.fs_rm("")
assert(not vim.loop.fs_stat(Helpers.path("")), "fs root should be deleted")
assert(not vim.uv.fs_stat(Helpers.path("")), "fs root should be deleted")
end)
it("lsmod lists all mods in dir", function()
@ -85,7 +85,7 @@ describe("util", function()
assert.same(Helpers.path("old/lua/foobar"), root)
Helpers.fs_rm("old")
assert(not vim.loop.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
assert(not vim.uv.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
-- vim.opt.rtp = rtp
vim.opt.rtp:append(Helpers.path("new"))

View File

@ -26,12 +26,12 @@ function M.fs_rm(dir)
dir = Util.norm(M.fs_root .. "/" .. dir)
Util.walk(dir, function(path, _, type)
if type == "directory" then
vim.loop.fs_rmdir(path)
vim.uv.fs_rmdir(path)
else
vim.loop.fs_unlink(path)
vim.uv.fs_unlink(path)
end
end)
vim.loop.fs_rmdir(dir)
vim.uv.fs_rmdir(dir)
end
return M

View File

@ -9,7 +9,7 @@ end
function M.load(plugin)
local name = plugin:match(".*/(.*)")
local package_root = M.root(".tests/site/pack/deps/start/")
if not vim.loop.fs_stat(package_root .. name) then
if not vim.uv.fs_stat(package_root .. name) then
print("Installing " .. plugin)
vim.fn.mkdir(package_root, "p")
vim.fn.system({