Skip to content

Commit fd79afe

Browse files
committed
feat: add type annotations for plenary.log
1 parent af98b9a commit fd79afe

File tree

2 files changed

+86
-32
lines changed

2 files changed

+86
-32
lines changed

lua/plenary/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
---@field functional PlenaryFunctional
88
---@field job PlenaryJob
99
---@field json PlenaryJson
10+
---@field log PlenaryLog
1011
---@field path PlenaryPath
1112
---@field scandir PlenaryScandir
1213
---@field tbl PlenaryTbl

lua/plenary/log.lua

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,57 @@
99

1010
local Path = require "plenary.path"
1111

12+
---@type boolean|string|vim.NIL
1213
local p_debug = vim.fn.getenv "DEBUG_PLENARY"
1314
if p_debug == vim.NIL then
1415
p_debug = false
1516
end
1617

18+
---@alias PlenaryLogLevel "trace"|"debug"|"info"|"warn"|"error"|"fatal"
19+
20+
-- luacheck: push ignore 631
21+
22+
---Adjust content as needed, but must keep function parameters to be filled
23+
---by library code.
24+
---* param is_console boolean If output is for console or log file.
25+
---* param mode_name string Level configuration 'modes' field 'name'
26+
---* param src_path string Path to source file given by debug.info.source
27+
---* param src_line integer Line into source file given by debug.info.currentline
28+
---* param msg string Message, which is later on escaped, if needed.
29+
---@alias PlenaryLogFmtMsg fun(is_console: boolean, mode_name: string, src_path: string, src_line: integer, msg: string): string
30+
31+
-- luacheck: pop
32+
33+
---@class PlenaryLogLevelConfig
34+
---@field name PlenaryLogLevel
35+
---@field hl string highight name for console.
36+
37+
---@class PlenaryLogConfig
38+
---@field plugin? string Name of the plugin. Prepended to log messages. (default: `plenary`)
39+
---@field use_console? '"async"'|"sync"|false Should print the output to neovim. (default: `"async"`)
40+
---@field highlights? boolean Should highlighting be used in console (using echohl). (default: `true`)
41+
---@field info_level? integer Level of function call stack. (default: `2`)
42+
---@field use_file? boolean Should write to a file. Default logging file is `stdpath("cache")/plugin`. (default: `true`)
43+
---@field outfile? string Output file has precedence over plugin, if not nil and use_file == true. (default: `nil`)
44+
---@field use_quickfix? boolean Should write to the quickfix list. (default: `false`)
45+
---@field level? PlenaryLogLevel Any messages above this level will be logged. (default: `"info"` or `"debug"`)
46+
---@field modes? PlenaryLogLevelConfig[] Level configuration.
47+
---@field float_precision? float Can limit the number of decimals displayed for floats. (default: `0.01`)
48+
---@field fmt_msg? PlenaryLogFmtMsg
49+
1750
-- User configuration section
51+
---@type PlenaryLogConfig
1852
local default_config = {
19-
-- Name of the plugin. Prepended to log messages.
2053
plugin = "plenary",
21-
22-
-- Should print the output to neovim while running.
23-
-- values: 'sync','async',false
2454
use_console = "async",
25-
26-
-- Should highlighting be used in console (using echohl).
2755
highlights = true,
2856

2957
-- Should write to a file.
3058
-- Default output for logging file is `stdpath("log")/plugin.log`.
3159
use_file = true,
32-
33-
-- Output file has precedence over plugin, if not nil.
34-
-- Used for the logging file, if not nil and use_file == true.
3560
outfile = nil,
36-
37-
-- Should write to the quickfix list.
3861
use_quickfix = false,
39-
40-
-- Any messages above this level will be logged.
4162
level = p_debug and "debug" or "info",
42-
43-
-- Level configuration.
4463
modes = {
4564
{ name = "trace", hl = "Comment" },
4665
{ name = "debug", hl = "Comment" },
@@ -49,17 +68,7 @@ local default_config = {
4968
{ name = "error", hl = "ErrorMsg" },
5069
{ name = "fatal", hl = "ErrorMsg" },
5170
},
52-
53-
-- Can limit the number of decimals displayed for floats.
5471
float_precision = 0.01,
55-
56-
-- Adjust content as needed, but must keep function parameters to be filled
57-
-- by library code.
58-
---@param is_console boolean If output is for console or log file.
59-
---@param mode_name string Level configuration 'modes' field 'name'
60-
---@param src_path string Path to source file given by debug.info.source
61-
---@param src_line integer Line into source file given by debug.info.currentline
62-
---@param msg string Message, which is later on escaped, if needed.
6372
fmt_msg = function(is_console, mode_name, src_path, src_line, msg)
6473
local nameupper = mode_name:upper()
6574
local lineinfo = src_path .. ":" .. src_line
@@ -72,30 +81,64 @@ local default_config = {
7281
}
7382

7483
-- {{{ NO NEED TO CHANGE
84+
85+
---@class PlenaryLog
86+
---@field trace fun(...) Write TRACE log.
87+
---@field debug fun(...) Write DEBUG log.
88+
---@field info fun(...) Write INFO log.
89+
---@field warn fun(...) Write WARN log.
90+
---@field error fun(...) Write ERROR log.
91+
---@field fatal fun(...) Write FATAL log.
92+
---@field fmt_trace fun(fmt: string, ...: any) Write TRACE log with formatting by string.format.
93+
---@field fmt_debug fun(fmt: string, ...: any) Write DEBUG log with formatting by string.format.
94+
---@field fmt_info fun(fmt: string, ...: any) Write INFO log with formatting by string.format.
95+
---@field fmt_warn fun(fmt: string, ...: any) Write WARN log with formatting by string.format.
96+
---@field fmt_error fun(fmt: string, ...: any) Write ERROR log with formatting by string.format.
97+
---@field fmt_fatal fun(fmt: string, ...: any) Write FATAL log with formatting by string.format.
98+
---@field lazy_trace fun(heavy_func: fun(...): string) Write TRACE log from output of heavy_func().
99+
---@field lazy_debug fun(heavy_func: fun(...): string) Write DEBUG log from output of heavy_func().
100+
---@field lazy_info fun(heavy_func: fun(...): string) Write INFO log from output of heavy_func().
101+
---@field lazy_warn fun(heavy_func: fun(...): string) Write WARN log from output of heavy_func().
102+
---@field lazy_error fun(heavy_func: fun(...): string) Write ERROR log from output of heavy_func().
103+
---@field lazy_fatal fun(heavy_func: fun(...): string) Write FATAL log from output of heavy_func().
104+
---@field file_trace fun(vals: table, override: PlenaryLogConfig) Write TRACE log into file instead of console.
105+
---@field file_debug fun(vals: table, override: PlenaryLogConfig) Write DEBUG log into file instead of console.
106+
---@field file_info fun(vals: table, override: PlenaryLogConfig) Write INFO log into file instead of console.
107+
---@field file_warn fun(vals: table, override: PlenaryLogConfig) Write WARN log into file instead of console.
108+
---@field file_error fun(vals: table, override: PlenaryLogConfig) Write ERROR log into file instead of console.
109+
---@field file_fatal fun(vals: table, override: PlenaryLogConfig) Write FATAL log into file instead of console.
75110
local log = {}
76111

77112
local unpack = unpack or table.unpack
78113

114+
---@param config PlenaryLogConfig
115+
---@param standalone boolean
116+
---@return PlenaryLog
79117
log.new = function(config, standalone)
80118
config = vim.tbl_deep_extend("force", default_config, config)
81119

82120
local outfile = vim.F.if_nil(
83121
config.outfile,
84122
Path:new(vim.api.nvim_call_function("stdpath", { "log" }), config.plugin .. ".log").filename
85-
)
123+
) --[[@as string]]
86124

125+
---@type PlenaryLog
87126
local obj
88127
if standalone then
89128
obj = log
90129
else
91-
obj = config
130+
obj = config --[[@as PlenaryLog]]
92131
end
93132

133+
---@type table<PlenaryLogLevel, integer>
94134
local levels = {}
95135
for i, v in ipairs(config.modes) do
96136
levels[v.name] = i
97137
end
98138

139+
---@param x float
140+
---@param increment? integer
141+
---@return float
99142
local round = function(x, increment)
100143
if x == 0 then
101144
return x
@@ -105,6 +148,8 @@ log.new = function(config, standalone)
105148
return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment
106149
end
107150

151+
---@param ... any
152+
---@return string
108153
local make_string = function(...)
109154
local t = {}
110155
for i = 1, select("#", ...) do
@@ -123,6 +168,10 @@ log.new = function(config, standalone)
123168
return table.concat(t, " ")
124169
end
125170

171+
---@param level integer
172+
---@param level_config PlenaryLogLevelConfig
173+
---@param message_maker fun(...): string
174+
---@param ... any
126175
local log_at_level = function(level, level_config, message_maker, ...)
127176
-- Return early if we're below the config.level
128177
if level < levels[config.level] then
@@ -145,7 +194,7 @@ log.new = function(config, standalone)
145194
for _, v in ipairs(split_console) do
146195
local formatted_msg = string.format("[%s] %s", config.plugin, vim.fn.escape(v, [["\]]))
147196

148-
local ok = pcall(vim.cmd, string.format([[echom "%s"]], formatted_msg))
197+
local ok = pcall(vim.cmd --[[@as fun(command: string)]], string.format([[echom "%s"]], formatted_msg))
149198
if not ok then
150199
vim.api.nvim_out_write(msg .. "\n")
151200
end
@@ -190,12 +239,14 @@ log.new = function(config, standalone)
190239
end
191240

192241
for i, x in ipairs(config.modes) do
193-
-- log.info("these", "are", "separated")
242+
---log.info("these", "are", "separated")
243+
---@param ... any
194244
obj[x.name] = function(...)
195245
return log_at_level(i, x, make_string, ...)
196246
end
197247

198-
-- log.fmt_info("These are %s strings", "formatted")
248+
---log.fmt_info("These are %s strings", "formatted")
249+
---@param ... any
199250
obj[("fmt_%s"):format(x.name)] = function(...)
200251
return log_at_level(i, x, function(...)
201252
local passed = { ... }
@@ -208,14 +259,16 @@ log.new = function(config, standalone)
208259
end, ...)
209260
end
210261

211-
-- log.lazy_info(expensive_to_calculate)
262+
---log.lazy_info(expensive_to_calculate)
212263
obj[("lazy_%s"):format(x.name)] = function()
213264
return log_at_level(i, x, function(f)
214265
return f()
215266
end)
216267
end
217268

218-
-- log.file_info("do not print")
269+
---log.file_info("do not print")
270+
---@param vals table
271+
---@param override PlenaryLogConfig
219272
obj[("file_%s"):format(x.name)] = function(vals, override)
220273
local original_console = config.use_console
221274
config.use_console = false

0 commit comments

Comments
 (0)