Skip to content

Commit

Permalink
feat: refactor all vim.loop -> vim.uv and add a shim when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 22, 2024
1 parent 83493db commit 9e157df
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 51 deletions.
12 changes: 6 additions & 6 deletions lua/lazy/core/cache.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local uv = vim.loop
local uv = vim.uv

local M = {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()|
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions lua/lazy/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down Expand Up @@ -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}[]
Expand Down
12 changes: 7 additions & 5 deletions lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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" }
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/manage/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/manage/reloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/manage/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lua/lazy/manage/task/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/manage/task/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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,
})
Expand Down
6 changes: 3 additions & 3 deletions lua/lazy/manage/task/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/stats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/core/e2e_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/core/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"))
Expand Down
Loading

0 comments on commit 9e157df

Please sign in to comment.