Skip to content

Commit 6c7ef7e

Browse files
committed
refactor: logging
1 parent 765773a commit 6c7ef7e

File tree

10 files changed

+130
-80
lines changed

10 files changed

+130
-80
lines changed

lua/lazy/core/plugin.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ end
403403
---@param plugin LazyPlugin
404404
function M.has_errors(plugin)
405405
for _, task in ipairs(plugin._.tasks or {}) do
406-
if task.error then
406+
if task:has_errors() then
407407
return true
408408
end
409409
end

lua/lazy/manage/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function M.clear(plugins)
232232
if plugin._.tasks then
233233
---@param task LazyTask
234234
plugin._.tasks = vim.tbl_filter(function(task)
235-
return task:is_running() or task.error
235+
return task:is_running() or task:has_errors()
236236
end, plugin._.tasks)
237237
end
238238
end

lua/lazy/manage/runner.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function Runner:_start()
9696
wait_step = s.step
9797
elseif not running then
9898
local plugin = self:plugin(name)
99-
if s.task and s.task.error then
99+
if s.task and s.task:has_errors() then
100100
active = active - 1
101101
elseif s.step == #self._pipeline then
102102
s.task = nil

lua/lazy/manage/task/git.lua

+19-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ M.log = {
1818
local stat = vim.uv.fs_stat(plugin.dir .. "/.git")
1919
return not (stat and stat.type == "directory")
2020
end,
21+
---@async
2122
---@param opts {args?: string[], updated?:boolean, check?:boolean}
2223
run = function(self, opts)
2324
local args = {
@@ -68,6 +69,7 @@ M.clone = {
6869
skip = function(plugin)
6970
return plugin._.installed or plugin._.is_local
7071
end,
72+
---@async
7173
run = function(self)
7274
local args = {
7375
"clone",
@@ -129,6 +131,7 @@ M.branch = {
129131
local branch = assert(Git.get_branch(plugin))
130132
return Git.get_commit(plugin.dir, branch, true)
131133
end,
134+
---@async
132135
run = function(self)
133136
local args = {
134137
"remote",
@@ -154,14 +157,17 @@ M.origin = {
154157
local origin = Git.get_origin(plugin.dir)
155158
return origin == plugin.url
156159
end,
160+
---@async
157161
---@param opts {check?:boolean}
158162
run = function(self, opts)
159163
if opts.check then
160164
local origin = Git.get_origin(self.plugin.dir)
161-
self.error = "Origin has changed:\n"
162-
self.error = self.error .. " * old: " .. origin .. "\n"
163-
self.error = self.error .. " * new: " .. self.plugin.url .. "\n"
164-
self.error = self.error .. "Please run update to fix"
165+
self:error({
166+
"Origin has changed:",
167+
" * old: " .. origin,
168+
" * new: " .. self.plugin.url,
169+
"Please run update to fix",
170+
})
165171
return
166172
end
167173
require("lazy.manage.task.fs").clean.run(self, opts)
@@ -173,13 +179,15 @@ M.status = {
173179
skip = function(plugin)
174180
return not plugin._.installed or plugin._.is_local
175181
end,
182+
---@async
176183
run = function(self)
177184
self:spawn("git", {
178185
args = { "ls-files", "-d", "-m" },
179186
cwd = self.plugin.dir,
180187
on_exit = function(ok, output)
181188
if ok then
182189
local lines = vim.split(output, "\n")
190+
---@type string[]
183191
lines = vim.tbl_filter(function(line)
184192
-- Fix doc/tags being marked as modified
185193
if line:gsub("[\\/]", "/") == "doc/tags" then
@@ -190,12 +198,13 @@ M.status = {
190198
return line ~= ""
191199
end, lines)
192200
if #lines > 0 then
193-
self.error = "You have local changes in `" .. self.plugin.dir .. "`:\n"
201+
local msg = { "You have local changes in `" .. self.plugin.dir .. "`:" }
194202
for _, line in ipairs(lines) do
195-
self.error = self.error .. " * " .. line .. "\n"
203+
msg[#msg + 1] = " * " .. line
196204
end
197-
self.error = self.error .. "Please remove them to update.\n"
198-
self.error = self.error .. "You can also press `x` to remove the plugin and then `I` to install it again."
205+
msg[#msg + 1] = "Please remove them to update."
206+
msg[#msg + 1] = "You can also press `x` to remove the plugin and then `I` to install it again."
207+
self:error(msg)
199208
end
200209
end
201210
end,
@@ -209,6 +218,7 @@ M.fetch = {
209218
return not plugin._.installed or plugin._.is_local
210219
end,
211220

221+
---@async
212222
run = function(self)
213223
local args = {
214224
"fetch",
@@ -236,6 +246,7 @@ M.checkout = {
236246
return not plugin._.installed or plugin._.is_local
237247
end,
238248

249+
---@async
239250
---@param opts {lockfile?:boolean}
240251
run = function(self, opts)
241252
local info = assert(Git.info(self.plugin.dir))

lua/lazy/manage/task/init.lua

+55-27
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ local Process = require("lazy.manage.process")
33

44
---@class LazyTaskDef
55
---@field skip? fun(plugin:LazyPlugin, opts?:TaskOptions):any?
6-
---@field run fun(task:LazyTask, opts:TaskOptions)
6+
---@field run async fun(task:LazyTask, opts:TaskOptions)
77

88
---@alias LazyTaskFn async fun(task:LazyTask, opts:TaskOptions)
99

10+
---@class LazyMsg
11+
---@field msg string
12+
---@field level? number
13+
1014
---@class LazyTask
1115
---@field plugin LazyPlugin
1216
---@field name string
13-
---@field output string
14-
---@field status string
15-
---@field error? string
16-
---@field warn? string
17+
---@field private _log LazyMsg[]
1718
---@field private _started? number
1819
---@field private _ended? number
1920
---@field private _opts TaskOptions
2021
---@field private _running Async
22+
---@field private _level number
2123
local Task = {}
2224

2325
---@class TaskOptions: {[string]:any}
@@ -30,10 +32,10 @@ local Task = {}
3032
function Task.new(plugin, name, task, opts)
3133
local self = setmetatable({}, { __index = Task })
3234
self._opts = opts or {}
35+
self._log = {}
36+
self._level = vim.log.levels.TRACE
3337
self.plugin = plugin
3438
self.name = name
35-
self.output = ""
36-
self.status = ""
3739
---@param other LazyTask
3840
plugin._.tasks = vim.tbl_filter(function(other)
3941
return other.name ~= name or other:is_running()
@@ -43,6 +45,31 @@ function Task.new(plugin, name, task, opts)
4345
return self
4446
end
4547

48+
---@param level? number
49+
---@return LazyMsg[]
50+
function Task:get_log(level)
51+
level = level or vim.log.levels.DEBUG
52+
return vim.tbl_filter(function(msg)
53+
return msg.level >= level
54+
end, self._log)
55+
end
56+
57+
---@param level? number
58+
function Task:output(level)
59+
return table.concat(
60+
---@param m LazyMsg
61+
vim.tbl_map(function(m)
62+
return m.msg
63+
end, self:get_log(level)),
64+
"\n"
65+
)
66+
end
67+
68+
function Task:status()
69+
local ret = self._log[#self._log]
70+
return ret and ret.msg or ""
71+
end
72+
4673
function Task:has_started()
4774
return self._started ~= nil
4875
end
@@ -55,6 +82,14 @@ function Task:is_running()
5582
return not self:has_ended()
5683
end
5784

85+
function Task:has_errors()
86+
return self._level >= vim.log.levels.ERROR
87+
end
88+
89+
function Task:has_warnings()
90+
return self._level >= vim.log.levels.WARN
91+
end
92+
5893
---@private
5994
---@param task LazyTaskFn
6095
function Task:_start(task)
@@ -70,36 +105,33 @@ function Task:_start(task)
70105
self:_done()
71106
end,
72107
on_error = function(err)
73-
self:notify_error(err)
108+
self:error(err)
74109
end,
75110
on_yield = function(res)
76-
self:notify(res)
111+
self:log(res)
77112
end,
78113
})
79114
end
80115

81116
---@param msg string|string[]
82-
---@param severity? vim.diagnostic.Severity
83-
function Task:notify(msg, severity)
84-
local var = severity == vim.diagnostic.severity.ERROR and "error"
85-
or severity == vim.diagnostic.severity.WARN and "warn"
86-
or "output"
117+
---@param level? number
118+
function Task:log(msg, level)
119+
level = level or vim.log.levels.DEBUG
120+
self._level = math.max(self._level or 0, level or 0)
87121
msg = type(msg) == "table" and table.concat(msg, "\n") or msg
88122
---@cast msg string
89-
---@diagnostic disable-next-line: no-unknown
90-
self[var] = self[var] and (self[var] .. "\n" .. msg) or msg
91-
self.status = msg
123+
table.insert(self._log, { msg = msg, level = level })
92124
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
93125
end
94126

95127
---@param msg string|string[]
96-
function Task:notify_error(msg)
97-
self:notify(msg, vim.diagnostic.severity.ERROR)
128+
function Task:error(msg)
129+
self:log(msg, vim.log.levels.ERROR)
98130
end
99131

100132
---@param msg string|string[]
101-
function Task:notify_warn(msg)
102-
self:notify(msg, vim.diagnostic.severity.WARN)
133+
function Task:warn(msg)
134+
self:log(msg, vim.log.levels.WARN)
103135
end
104136

105137
---@private
@@ -141,20 +173,16 @@ function Task:spawn(cmd, opts)
141173
local on_exit = opts.on_exit
142174

143175
function opts.on_line(line)
144-
self.status = line
176+
self:log(line, vim.log.levels.TRACE)
145177
if on_line then
146178
pcall(on_line, line)
147179
end
148-
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
149180
end
150181

151182
local running = true
152183
---@param output string
153184
function opts.on_exit(ok, output)
154-
self.output = self.output .. output
155-
if not ok then
156-
self.error = self.error and (self.error .. "\n" .. output) or output
157-
end
185+
self:log(output, ok and vim.log.levels.DEBUG or vim.log.levels.ERROR)
158186
if on_exit then
159187
pcall(on_exit, ok, output)
160188
end

lua/lazy/manage/task/plugin.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local Config = require("lazy.core.config")
21
local Loader = require("lazy.core.loader")
32
local Rocks = require("lazy.pkg.rockspec")
43
local Util = require("lazy.util")
@@ -21,9 +20,10 @@ local B = {}
2120
---@param build string
2221
function B.cmd(task, build)
2322
local cmd = vim.api.nvim_parse_cmd(build:sub(2), {}) --[[@as vim.api.keyset.cmd]]
24-
task.output = vim.api.nvim_cmd(cmd, { output = true })
23+
task:log(vim.api.nvim_cmd(cmd, { output = true }))
2524
end
2625

26+
---@async
2727
---@param task LazyTask
2828
---@param build string
2929
function B.shell(task, build)
@@ -44,6 +44,7 @@ M.build = {
4444
end
4545
return not ((plugin._.dirty or plugin._.build) and (plugin.build or get_build_file(plugin)))
4646
end,
47+
---@async
4748
run = function(self)
4849
vim.cmd([[silent! runtime plugin/rplugin.vim]])
4950

@@ -92,7 +93,7 @@ M.docs = {
9293
run = function(self)
9394
local docs = self.plugin.dir .. "/doc/"
9495
if Util.file_exists(docs) then
95-
self.output = vim.api.nvim_cmd({ cmd = "helptags", args = { docs } }, { output = true })
96+
self:log(vim.api.nvim_cmd({ cmd = "helptags", args = { docs } }, { output = true }))
9697
end
9798
end,
9899
}

lua/lazy/pkg/rockspec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ function M.build(task)
106106
if
107107
not M.check({
108108
error = function(msg)
109-
task:notify_error(msg:gsub("[{}]", "`"))
109+
task:error(msg:gsub("[{}]", "`"))
110110
end,
111111
warn = function(msg)
112-
task:notify_warn(msg)
112+
task:warn(msg)
113113
end,
114114
ok = function(msg) end,
115115
})
116116
then
117-
task:notify_warn({
117+
task:log({
118118
"",
119119
"This plugin requires `luarocks`. Try one of the following:",
120120
" - fix your `luarocks` installation",

lua/lazy/view/colors.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ M.colors = {
3030
Button = "CursorLine",
3131
ButtonActive = "Visual",
3232
TaskOutput = "MsgArea", -- task output
33-
TaskError = "ErrorMsg", -- task errors
34-
TaskWarning = "WarningMsg", -- task errors
33+
Error = "DiagnosticError", -- task errors
34+
Warning = "DiagnosticWarn", -- task errors
35+
Info = "DiagnosticInfo", -- task errors
3536
Dir = "@markup.link", -- directory
3637
Url = "@markup.link", -- url
3738
Bold = { bold = true },

0 commit comments

Comments
 (0)