Skip to content

Commit 711834f

Browse files
committed
refactor: split util
1 parent 32ca1c4 commit 711834f

File tree

2 files changed

+197
-162
lines changed

2 files changed

+197
-162
lines changed

lua/lazy/core/util.lua

+34-162
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,13 @@ function M.track(name, time)
2020
end
2121
return entry
2222
else
23+
---@type LazyProfile
2324
local entry = table.remove(M._profiles)
2425
entry.time = vim.loop.hrtime() - entry.time
2526
return entry
2627
end
2728
end
2829

29-
function M.file_exists(file)
30-
return vim.loop.fs_stat(file) ~= nil
31-
end
32-
33-
function M.open(uri)
34-
if M.file_exists(uri) then
35-
return vim.cmd.view(uri)
36-
end
37-
local cmd
38-
if vim.fn.has("win32") == 1 then
39-
cmd = { "cmd.exe", "/c", "start", '""', vim.fn.shellescape(uri) }
40-
elseif vim.fn.has("macunix") == 1 then
41-
cmd = { "open", uri }
42-
else
43-
cmd = { "xdg-open", uri }
44-
end
45-
46-
local ret = vim.fn.system(cmd)
47-
if vim.v.shell_error ~= 0 then
48-
local msg = {
49-
"Failed to open uri",
50-
ret,
51-
vim.inspect(cmd),
52-
}
53-
vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR)
54-
end
55-
end
56-
57-
---@param ms number
58-
---@param fn fun()
59-
function M.throttle(ms, fn)
60-
local timer = vim.loop.new_timer()
61-
local running = false
62-
local first = true
63-
64-
return function()
65-
if not running then
66-
if first then
67-
fn()
68-
first = false
69-
end
70-
71-
timer:start(ms, 0, function()
72-
running = false
73-
vim.schedule(fn)
74-
end)
75-
76-
running = true
77-
end
78-
end
79-
end
80-
8130
function M.very_lazy()
8231
local function _load()
8332
vim.defer_fn(function()
@@ -103,102 +52,58 @@ function M.very_lazy()
10352
})
10453
end
10554

55+
---@alias FileType "file"|"directory"|"link"
56+
---@alias DirEntry {name: string, path: string, type: FileType}[]
10657
---@param path string
107-
function M.scandir(path)
108-
---@type {name: string, path: string, type: "file"|"directory"|"link"}[]
109-
local ret = {}
110-
58+
---@param fn fun(path: string, name:string, type:FileType)
59+
function M.scandir(path, fn)
11160
local dir = vim.loop.fs_opendir(path, nil, 100)
112-
11361
if dir then
114-
---@type {name: string, path: string, type: "file"|"directory"|"link"}[]
115-
local entries = vim.loop.fs_readdir(dir)
62+
local entries = vim.loop.fs_readdir(dir) --[[@as DirEntry[]]
11663
while entries do
11764
for _, entry in ipairs(entries) do
11865
entry.path = path .. "/" .. entry.name
119-
table.insert(ret, entry)
66+
fn(path .. "/" .. entry.name, entry.name, entry.type)
12067
end
12168
entries = vim.loop.fs_readdir(dir)
12269
end
12370
vim.loop.fs_closedir(dir)
12471
end
125-
126-
return ret
12772
end
128-
129-
function M.profile()
130-
local lines = { "# Profile" }
131-
132-
---@param entry LazyProfile
133-
local function _profile(entry, depth)
134-
if entry.time < 0.5 then
135-
-- Nothing
136-
end
137-
138-
table.insert(
139-
lines,
140-
(" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) / 1e6 * 100) / 100 .. "ms**"
141-
)
142-
143-
for _, child in ipairs(entry) do
144-
_profile(child, depth + 1)
73+
---@param path string
74+
---@param fn fun(path: string, name:string, type:FileType)
75+
function M.ls(path, fn)
76+
local handle = vim.loop.fs_scandir(path)
77+
while handle do
78+
local name, t = vim.loop.fs_scandir_next(handle)
79+
if not name then
80+
break
14581
end
146-
end
147-
148-
for _, entry in ipairs(M._profiles[1]) do
149-
_profile(entry, 1)
150-
end
151-
152-
M.markdown(lines)
153-
end
154-
155-
---@return string?
156-
function M.head(file)
157-
local f = io.open(file)
158-
if f then
159-
local ret = f:read()
160-
f:close()
161-
return ret
82+
fn(path .. "/" .. name, name, t)
16283
end
16384
end
16485

165-
---@return {branch: string, hash:string}?
166-
function M.git_info(dir)
167-
local line = M.head(dir .. "/.git/HEAD")
168-
if line then
169-
---@type string, string
170-
local ref, branch = line:match("ref: (refs/heads/(.*))")
171-
172-
if ref then
173-
return {
174-
branch = branch,
175-
hash = M.head(dir .. "/.git/" .. ref),
176-
}
86+
---@param path string
87+
---@param fn fun(path: string, name:string, type:FileType)
88+
function M.walk(path, fn)
89+
M.ls(path, function(child, name, type)
90+
if type == "directory" then
91+
M.walk(child, fn)
17792
end
178-
end
93+
fn(child, name, type)
94+
end)
17995
end
18096

181-
---@param msg string|string[]
182-
---@param opts? table
183-
function M.markdown(msg, opts)
184-
if type(msg) == "table" then
185-
msg = table.concat(msg, "\n") or msg
186-
end
187-
188-
vim.notify(
189-
msg,
190-
vim.log.levels.INFO,
191-
vim.tbl_deep_extend("force", {
192-
title = "lazy.nvim",
193-
on_open = function(win)
194-
vim.wo[win].conceallevel = 3
195-
vim.wo[win].concealcursor = "n"
196-
vim.wo[win].spell = false
197-
198-
vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown")
199-
end,
200-
}, opts or {})
201-
)
97+
---@param root string
98+
---@param fn fun(modname:string, modpath:string)
99+
function M.lsmod(root, fn)
100+
M.ls(root, function(path, name, type)
101+
if type == "file" and name:sub(-4) == ".lua" then
102+
fn(name:sub(1, -5), path)
103+
elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then
104+
fn(name, path .. "/init.lua")
105+
end
106+
end)
202107
end
203108

204109
function M.error(msg)
@@ -213,37 +118,4 @@ function M.info(msg)
213118
})
214119
end
215120

216-
function M._dump(value, result)
217-
local t = type(value)
218-
if t == "number" or t == "boolean" then
219-
table.insert(result, tostring(value))
220-
elseif t == "string" then
221-
table.insert(result, ("%q"):format(value))
222-
elseif t == "table" then
223-
table.insert(result, "{")
224-
local i = 1
225-
---@diagnostic disable-next-line: no-unknown
226-
for k, v in pairs(value) do
227-
if k == i then
228-
elseif type(k) == "string" then
229-
table.insert(result, ("[%q]="):format(k))
230-
else
231-
table.insert(result, k .. "=")
232-
end
233-
M._dump(v, result)
234-
table.insert(result, ",")
235-
i = i + 1
236-
end
237-
table.insert(result, "}")
238-
else
239-
error("Unsupported type " .. t)
240-
end
241-
end
242-
243-
function M.dump(value)
244-
local result = {}
245-
M._dump(value, result)
246-
return table.concat(result, "")
247-
end
248-
249121
return M

0 commit comments

Comments
 (0)