From 9e157df077d24654d0cdefe08158cd4f76e85fe8 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 22 Mar 2024 08:58:36 +0100 Subject: [PATCH] feat: refactor all vim.loop -> vim.uv and add a shim when needed --- lua/lazy/core/cache.lua | 12 ++++++------ lua/lazy/core/config.lua | 2 +- lua/lazy/core/loader.lua | 4 ++-- lua/lazy/core/util.lua | 16 ++++++++-------- lua/lazy/health.lua | 4 ++-- lua/lazy/help.lua | 6 +++--- lua/lazy/init.lua | 12 +++++++----- lua/lazy/manage/process.lua | 2 +- lua/lazy/manage/reloader.lua | 4 ++-- lua/lazy/manage/runner.lua | 2 +- lua/lazy/manage/task/fs.lua | 8 ++++---- lua/lazy/manage/task/git.lua | 4 ++-- lua/lazy/manage/task/init.lua | 6 +++--- lua/lazy/stats.lua | 2 +- lua/lazy/util.lua | 4 ++-- tests/core/e2e_spec.lua | 4 ++-- tests/core/util_spec.lua | 4 ++-- tests/helpers.lua | 6 +++--- tests/init.lua | 2 +- 19 files changed, 53 insertions(+), 51 deletions(-) diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index ea9f4d9..6588c1e 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -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) diff --git a/lua/lazy/core/config.lua b/lua/lazy/core/config.lua index 60934a0..0e6b7ee 100644 --- a/lua/lazy/core/config.lua +++ b/lua/lazy/core/config.lua @@ -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 diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index ac5f471..9cd5018 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -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 diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 77cb8f5..e51e670 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -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) diff --git a/lua/lazy/health.lua b/lua/lazy/health.lua index 89feabe..bcc546a 100644 --- a/lua/lazy/health.lua +++ b/lua/lazy/health.lua @@ -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") diff --git a/lua/lazy/help.lua b/lua/lazy/help.lua index a33e808..aa7666e 100644 --- a/lua/lazy/help.lua +++ b/lua/lazy/help.lua @@ -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}[] diff --git a/lua/lazy/init.lua b/lua/lazy/init.lua index 75ccfa2..3bfa8c8 100644 --- a/lua/lazy/init.lua +++ b/lua/lazy/init.lua @@ -2,6 +2,8 @@ local M = {} M._start = 0 +vim.uv = vim.uv or vim.uv + local function profile_require() local done = {} ---@type table 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", diff --git a/lua/lazy/manage/process.lua b/lua/lazy/manage/process.lua index 1db3677..5bb2716 100644 --- a/lua/lazy/manage/process.lua +++ b/lua/lazy/manage/process.lua @@ -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[] diff --git a/lua/lazy/manage/reloader.lua b/lua/lazy/manage/reloader.lua index e94f038..ef9305c 100644 --- a/lua/lazy/manage/reloader.lua +++ b/lua/lazy/manage/reloader.lua @@ -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 diff --git a/lua/lazy/manage/runner.lua b/lua/lazy/manage/runner.lua index 0ac0c15..7e3a39d 100644 --- a/lua/lazy/manage/runner.lua +++ b/lua/lazy/manage/runner.lua @@ -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 diff --git a/lua/lazy/manage/task/fs.lua b/lua/lazy/manage/task/fs.lua index c753143..c99c8bf 100644 --- a/lua/lazy/manage/task/fs.lua +++ b/lua/lazy/manage/task/fs.lua @@ -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, diff --git a/lua/lazy/manage/task/git.lua b/lua/lazy/manage/task/git.lua index a1bfcf6..c838f30 100644 --- a/lua/lazy/manage/task/git.lua +++ b/lua/lazy/manage/task/git.lua @@ -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, }) diff --git a/lua/lazy/manage/task/init.lua b/lua/lazy/manage/task/init.lua index a68c7d7..2397cdf 100644 --- a/lua/lazy/manage/task/init.lua +++ b/lua/lazy/manage/task/init.lua @@ -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 diff --git a/lua/lazy/stats.lua b/lua/lazy/stats.lua index a898ff7..c4f1ec2 100644 --- a/lua/lazy/stats.lua +++ b/lua/lazy/stats.lua @@ -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) diff --git a/lua/lazy/util.lua b/lua/lazy/util.lua index d841255..1c73d43 100644 --- a/lua/lazy/util.lua +++ b/lua/lazy/util.lua @@ -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 diff --git a/tests/core/e2e_spec.lua b/tests/core/e2e_spec.lua index a7d02fc..37cdca5 100644 --- a/tests/core/e2e_spec.lua +++ b/tests/core/e2e_spec.lua @@ -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) diff --git a/tests/core/util_spec.lua b/tests/core/util_spec.lua index 1d3592c..a497881 100644 --- a/tests/core/util_spec.lua +++ b/tests/core/util_spec.lua @@ -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")) diff --git a/tests/helpers.lua b/tests/helpers.lua index 48d84f9..b722a4a 100644 --- a/tests/helpers.lua +++ b/tests/helpers.lua @@ -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 diff --git a/tests/init.lua b/tests/init.lua index a72b101..7b243c5 100644 --- a/tests/init.lua +++ b/tests/init.lua @@ -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({