@@ -11,6 +11,10 @@ M.defaults = {
11
11
}
12
12
13
13
local api = vim .api
14
+
15
+ --- @type Logger
16
+ local logger
17
+
14
18
--- @class Remote
15
19
local remote
16
20
@@ -22,18 +26,6 @@ local should_cache_lines = true
22
26
local cached_lines
23
27
local prev_lazyredraw
24
28
25
- local logs = {}
26
- local function log (msg , level )
27
- level = level or " TRACE"
28
- if M .debug or level ~= " TRACE" then
29
- msg = type (msg ) == " function" and msg () or msg
30
- logs [level ] = logs [level ] or {}
31
- for _ , line in ipairs (vim .split (msg .. " \n " , " \n " )) do
32
- table.insert (logs [level ], line )
33
- end
34
- end
35
- end
36
-
37
29
-- Inserts str_2 into str_1 at the given position.
38
30
local function string_insert (str_1 , str_2 , pos )
39
31
return str_1 :sub (1 , pos - 1 ) .. str_2 .. str_1 :sub (pos )
@@ -55,7 +47,7 @@ local function add_inline_highlights(line, cached_lns, updated_lines, undo_delet
55
47
local line_a = splice (cached_lns [line ])
56
48
local line_b = splice (updated_lines [line ])
57
49
local line_diff = vim .diff (line_a , line_b , { result_type = " indices" })
58
- log (function ()
50
+ logger . trace (function ()
59
51
return (" Changed lines (line %d):\n Original: '%s' (len=%d)\n Updated: '%s' (len=%d)\n\n Inline hunks: %s" ):format (
60
52
line ,
61
53
cached_lns [line ],
@@ -113,10 +105,10 @@ local function get_diff_highlights(cached_lns, updated_lines, line_range, opts)
113
105
local hunks = vim .diff (table.concat (cached_lns , " \n " ), table.concat (updated_lines , " \n " ), {
114
106
result_type = " indices" ,
115
107
})
116
- log ((" Visible line range: %d-%d" ):format (line_range [1 ], line_range [2 ]))
108
+ logger . trace ((" Visible line range: %d-%d" ):format (line_range [1 ], line_range [2 ]))
117
109
118
110
for i , hunk in ipairs (hunks ) do
119
- log (function ()
111
+ logger . trace (function ()
120
112
return (" Hunk %d/%d: %s" ):format (i , # hunks , vim .inspect (hunk ))
121
113
end )
122
114
@@ -132,7 +124,7 @@ local function get_diff_highlights(cached_lns, updated_lines, line_range, opts)
132
124
end_line = start_line + (count_a - count_b ) - 1
133
125
end
134
126
135
- log (function ()
127
+ logger . trace (function ()
136
128
return (" Lines %d-%d:\n Original: %s\n Updated: %s" ):format (
137
129
start_line ,
138
130
end_line ,
@@ -187,10 +179,15 @@ M._preview_across_lines = get_diff_highlights
187
179
188
180
--- @param cmd string
189
181
local function run_cmd (cmd )
182
+ if not chan_id then
183
+ logger .trace (" run_cmd: skipped as chan_id is not set" )
184
+ return
185
+ end
186
+
190
187
local cursor_pos = api .nvim_win_get_cursor (0 )
191
188
cursor_row , cursor_col = cursor_pos [1 ], cursor_pos [2 ]
192
189
193
- log (function ()
190
+ logger . trace (function ()
194
191
return (" Previewing command: %s (l=%d,c=%d)" ):format (cmd , cursor_row , cursor_col )
195
192
end )
196
193
return remote .run_cmd (chan_id , cmd , cursor_row , cursor_col )
@@ -201,7 +198,6 @@ local function command_preview(opts, preview_ns, preview_buf)
201
198
-- Any errors that occur in the preview function are not directly shown to the user but stored in vim.v.errmsg.
202
199
-- Related: https://github.com/neovim/neovim/issues/18910.
203
200
vim .v .errmsg = " "
204
- logs = {}
205
201
local args = opts .cmd_args
206
202
local command = opts .command
207
203
@@ -244,7 +240,7 @@ local function command_preview(opts, preview_ns, preview_buf)
244
240
set_lines (updated_lines )
245
241
-- This should not happen
246
242
if not opts .line1 then
247
- log (" No line1 range provided" , " ERROR " )
243
+ logger . error (" No line1 range provided" )
248
244
end
249
245
return 2
250
246
end
@@ -258,7 +254,7 @@ local function command_preview(opts, preview_ns, preview_buf)
258
254
undo_deletions = command .hl_groups [" deletion" ] ~= false ,
259
255
inline_highlighting = command .inline_highlighting ,
260
256
})
261
- log (function ()
257
+ logger . trace (function ()
262
258
return " Highlights: " .. vim .inspect (highlights )
263
259
end )
264
260
@@ -283,12 +279,12 @@ local function restore_buffer_state()
283
279
vim .o .lazyredraw = prev_lazyredraw
284
280
should_cache_lines = true
285
281
if vim .v .errmsg ~= " " then
286
- log ((" An error occurred in the preview function:\n %s" ):format (vim .inspect (vim .v .errmsg )), " ERROR " )
282
+ logger . error ((" An error occurred in the preview function:\n %s" ):format (vim .inspect (vim .v .errmsg )))
287
283
end
288
284
end
289
285
290
286
local function execute_command (command )
291
- log (" Executing command: " .. command )
287
+ logger . trace (" Executing command: " .. command )
292
288
vim .cmd (command )
293
289
restore_buffer_state ()
294
290
end
@@ -408,22 +404,18 @@ M.setup = function(user_config)
408
404
local config = vim .tbl_deep_extend (" force" , M .defaults , user_config or {})
409
405
validate_config (config )
410
406
create_user_commands (config .commands )
407
+ logger = require (" live-command.logger" )
411
408
remote = require (" live-command.remote" )
412
409
413
- remote .init_rpc (function (id )
410
+ remote .init_rpc (logger , function (id )
414
411
chan_id = id
415
412
end )
416
413
create_autocmds ()
414
+ end
417
415
418
- M .debug = user_config .debug
419
-
420
- api .nvim_create_user_command (" LiveCommandLog" , function ()
421
- local msg = (" live-command log\n ================\n\n %s%s" ):format (
422
- logs .ERROR and " [ERROR]\n " .. table.concat (logs .ERROR , " \n " ) .. (logs .TRACE and " \n " or " " ) or " " ,
423
- logs .TRACE and " [TRACE]\n " .. table.concat (logs .TRACE , " \n " ) or " "
424
- )
425
- vim .notify (msg )
426
- end , { nargs = 0 })
416
+ --- @param logger_ Logger
417
+ M ._set_logger = function (logger_ )
418
+ logger = logger_
427
419
end
428
420
429
421
M .version = " 1.3.0"
0 commit comments