From 711834f17cbfe16c95dd37bab4bc4b2f6d82fa5c Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 24 Nov 2022 22:03:00 +0100 Subject: [PATCH] refactor: split util --- lua/lazy/core/util.lua | 198 ++++++++--------------------------------- lua/lazy/util.lua | 163 +++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 163 deletions(-) create mode 100644 lua/lazy/util.lua diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index a489372..cbad215 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -20,64 +20,13 @@ function M.track(name, time) end return entry else + ---@type LazyProfile local entry = table.remove(M._profiles) entry.time = vim.loop.hrtime() - entry.time return entry end end -function M.file_exists(file) - return vim.loop.fs_stat(file) ~= nil -end - -function M.open(uri) - if M.file_exists(uri) then - return vim.cmd.view(uri) - end - local cmd - if vim.fn.has("win32") == 1 then - cmd = { "cmd.exe", "/c", "start", '""', vim.fn.shellescape(uri) } - elseif vim.fn.has("macunix") == 1 then - cmd = { "open", uri } - else - cmd = { "xdg-open", uri } - end - - local ret = vim.fn.system(cmd) - if vim.v.shell_error ~= 0 then - local msg = { - "Failed to open uri", - ret, - vim.inspect(cmd), - } - vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR) - end -end - ----@param ms number ----@param fn fun() -function M.throttle(ms, fn) - local timer = vim.loop.new_timer() - local running = false - local first = true - - return function() - if not running then - if first then - fn() - first = false - end - - timer:start(ms, 0, function() - running = false - vim.schedule(fn) - end) - - running = true - end - end -end - function M.very_lazy() local function _load() vim.defer_fn(function() @@ -103,102 +52,58 @@ function M.very_lazy() }) end +---@alias FileType "file"|"directory"|"link" +---@alias DirEntry {name: string, path: string, type: FileType}[] ---@param path string -function M.scandir(path) - ---@type {name: string, path: string, type: "file"|"directory"|"link"}[] - local ret = {} - +---@param fn fun(path: string, name:string, type:FileType) +function M.scandir(path, fn) local dir = vim.loop.fs_opendir(path, nil, 100) - if dir then - ---@type {name: string, path: string, type: "file"|"directory"|"link"}[] - local entries = vim.loop.fs_readdir(dir) + local entries = vim.loop.fs_readdir(dir) --[[@as DirEntry[]] while entries do for _, entry in ipairs(entries) do entry.path = path .. "/" .. entry.name - table.insert(ret, entry) + fn(path .. "/" .. entry.name, entry.name, entry.type) end entries = vim.loop.fs_readdir(dir) end vim.loop.fs_closedir(dir) end - - return ret end - -function M.profile() - local lines = { "# Profile" } - - ---@param entry LazyProfile - local function _profile(entry, depth) - if entry.time < 0.5 then - -- Nothing +---@param path string +---@param fn fun(path: string, name:string, type:FileType) +function M.ls(path, fn) + local handle = vim.loop.fs_scandir(path) + while handle do + local name, t = vim.loop.fs_scandir_next(handle) + if not name then + break end + fn(path .. "/" .. name, name, t) + end +end - table.insert( - lines, - (" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) / 1e6 * 100) / 100 .. "ms**" - ) - - for _, child in ipairs(entry) do - _profile(child, depth + 1) +---@param path string +---@param fn fun(path: string, name:string, type:FileType) +function M.walk(path, fn) + M.ls(path, function(child, name, type) + if type == "directory" then + M.walk(child, fn) end - end - - for _, entry in ipairs(M._profiles[1]) do - _profile(entry, 1) - end - - M.markdown(lines) + fn(child, name, type) + end) end ----@return string? -function M.head(file) - local f = io.open(file) - if f then - local ret = f:read() - f:close() - return ret - end -end - ----@return {branch: string, hash:string}? -function M.git_info(dir) - local line = M.head(dir .. "/.git/HEAD") - if line then - ---@type string, string - local ref, branch = line:match("ref: (refs/heads/(.*))") - - if ref then - return { - branch = branch, - hash = M.head(dir .. "/.git/" .. ref), - } +---@param root string +---@param fn fun(modname:string, modpath:string) +function M.lsmod(root, fn) + M.ls(root, function(path, name, type) + if type == "file" and name:sub(-4) == ".lua" then + fn(name:sub(1, -5), path) + elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then + fn(name, path .. "/init.lua") end - end -end - ----@param msg string|string[] ----@param opts? table -function M.markdown(msg, opts) - if type(msg) == "table" then - msg = table.concat(msg, "\n") or msg - end - - vim.notify( - msg, - vim.log.levels.INFO, - vim.tbl_deep_extend("force", { - title = "lazy.nvim", - on_open = function(win) - vim.wo[win].conceallevel = 3 - vim.wo[win].concealcursor = "n" - vim.wo[win].spell = false - - vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown") - end, - }, opts or {}) - ) + end) end function M.error(msg) @@ -213,37 +118,4 @@ function M.info(msg) }) end -function M._dump(value, result) - local t = type(value) - if t == "number" or t == "boolean" then - table.insert(result, tostring(value)) - elseif t == "string" then - table.insert(result, ("%q"):format(value)) - elseif t == "table" then - table.insert(result, "{") - local i = 1 - ---@diagnostic disable-next-line: no-unknown - for k, v in pairs(value) do - if k == i then - elseif type(k) == "string" then - table.insert(result, ("[%q]="):format(k)) - else - table.insert(result, k .. "=") - end - M._dump(v, result) - table.insert(result, ",") - i = i + 1 - end - table.insert(result, "}") - else - error("Unsupported type " .. t) - end -end - -function M.dump(value) - local result = {} - M._dump(value, result) - return table.concat(result, "") -end - return M diff --git a/lua/lazy/util.lua b/lua/lazy/util.lua new file mode 100644 index 0000000..3ca2e8d --- /dev/null +++ b/lua/lazy/util.lua @@ -0,0 +1,163 @@ +local M = setmetatable({}, { __index = require("lazy.core.util") }) + +function M.file_exists(file) + return vim.loop.fs_stat(file) ~= nil +end + +function M.open(uri) + if M.file_exists(uri) then + return vim.cmd.view(uri) + end + local cmd + if vim.fn.has("win32") == 1 then + cmd = { "cmd.exe", "/c", "start", '""', vim.fn.shellescape(uri) } + elseif vim.fn.has("macunix") == 1 then + cmd = { "open", uri } + else + cmd = { "xdg-open", uri } + end + + local ret = vim.fn.system(cmd) + if vim.v.shell_error ~= 0 then + local msg = { + "Failed to open uri", + ret, + vim.inspect(cmd), + } + vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR) + end +end + +---@param ms number +---@param fn fun() +function M.throttle(ms, fn) + local timer = vim.loop.new_timer() + local running = false + local first = true + + return function() + if not running then + if first then + fn() + first = false + end + + timer:start(ms, 0, function() + running = false + vim.schedule(fn) + end) + + running = true + end + end +end + +function M.profile() + local lines = { "# Profile" } + + ---@param entry LazyProfile + local function _profile(entry, depth) + if entry.time < 0.5 then + -- Nothing + end + + table.insert( + lines, + (" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) / 1e6 * 100) / 100 .. "ms**" + ) + + for _, child in ipairs(entry) do + _profile(child, depth + 1) + end + end + + for _, entry in ipairs(M._profiles[1]) do + _profile(entry, 1) + end + + M.markdown(lines) +end + +---@return string? +function M.head(file) + local f = io.open(file) + if f then + local ret = f:read() + f:close() + return ret + end +end + +---@return {branch: string, hash:string}? +function M.git_info(dir) + local line = M.head(dir .. "/.git/HEAD") + if line then + ---@type string, string + local ref, branch = line:match("ref: (refs/heads/(.*))") + + if ref then + return { + branch = branch, + hash = M.head(dir .. "/.git/" .. ref), + } + end + end +end + +---@param msg string|string[] +---@param opts? table +function M.markdown(msg, opts) + if type(msg) == "table" then + msg = table.concat(msg, "\n") or msg + end + + vim.notify( + msg, + vim.log.levels.INFO, + vim.tbl_deep_extend("force", { + title = "lazy.nvim", + on_open = function(win) + vim.wo[win].conceallevel = 3 + vim.wo[win].concealcursor = "n" + vim.wo[win].spell = false + + vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown") + end, + }, opts or {}) + ) +end + +function M._dump(value, result) + local t = type(value) + if t == "number" or t == "boolean" then + table.insert(result, tostring(value)) + elseif t == "string" then + table.insert(result, ("%q"):format(value)) + elseif t == "table" then + table.insert(result, "{") + local i = 1 + ---@diagnostic disable-next-line: no-unknown + for k, v in pairs(value) do + if k == i then + elseif type(k) == "string" then + table.insert(result, ("[%q]="):format(k)) + else + table.insert(result, k .. "=") + end + M._dump(v, result) + table.insert(result, ",") + i = i + 1 + end + table.insert(result, "}") + else + error("Unsupported type " .. t) + end +end + +function M.dump(value) + local result = {} + M._dump(value, result) + return table.concat(result, "") +end + +return M