@@ -11,6 +11,10 @@ M.defaults = {
1111}
1212
1313local api = vim .api
14+
15+ --- @type Logger
16+ local logger
17+
1418--- @class Remote
1519local remote
1620
@@ -22,18 +26,6 @@ local should_cache_lines = true
2226local cached_lines
2327local prev_lazyredraw
2428
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-
3729-- Inserts str_2 into str_1 at the given position.
3830local function string_insert (str_1 , str_2 , pos )
3931 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
5547 local line_a = splice (cached_lns [line ])
5648 local line_b = splice (updated_lines [line ])
5749 local line_diff = vim .diff (line_a , line_b , { result_type = " indices" })
58- log (function ()
50+ logger . trace (function ()
5951 return (" Changed lines (line %d):\n Original: '%s' (len=%d)\n Updated: '%s' (len=%d)\n\n Inline hunks: %s" ):format (
6052 line ,
6153 cached_lns [line ],
@@ -113,10 +105,10 @@ local function get_diff_highlights(cached_lns, updated_lines, line_range, opts)
113105 local hunks = vim .diff (table.concat (cached_lns , " \n " ), table.concat (updated_lines , " \n " ), {
114106 result_type = " indices" ,
115107 })
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 ]))
117109
118110 for i , hunk in ipairs (hunks ) do
119- log (function ()
111+ logger . trace (function ()
120112 return (" Hunk %d/%d: %s" ):format (i , # hunks , vim .inspect (hunk ))
121113 end )
122114
@@ -132,7 +124,7 @@ local function get_diff_highlights(cached_lns, updated_lines, line_range, opts)
132124 end_line = start_line + (count_a - count_b ) - 1
133125 end
134126
135- log (function ()
127+ logger . trace (function ()
136128 return (" Lines %d-%d:\n Original: %s\n Updated: %s" ):format (
137129 start_line ,
138130 end_line ,
@@ -187,10 +179,15 @@ M._preview_across_lines = get_diff_highlights
187179
188180--- @param cmd string
189181local 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+
190187 local cursor_pos = api .nvim_win_get_cursor (0 )
191188 cursor_row , cursor_col = cursor_pos [1 ], cursor_pos [2 ]
192189
193- log (function ()
190+ logger . trace (function ()
194191 return (" Previewing command: %s (l=%d,c=%d)" ):format (cmd , cursor_row , cursor_col )
195192 end )
196193 return remote .run_cmd (chan_id , cmd , cursor_row , cursor_col )
@@ -201,7 +198,6 @@ local function command_preview(opts, preview_ns, preview_buf)
201198 -- Any errors that occur in the preview function are not directly shown to the user but stored in vim.v.errmsg.
202199 -- Related: https://github.com/neovim/neovim/issues/18910.
203200 vim .v .errmsg = " "
204- logs = {}
205201 local args = opts .cmd_args
206202 local command = opts .command
207203
@@ -244,7 +240,7 @@ local function command_preview(opts, preview_ns, preview_buf)
244240 set_lines (updated_lines )
245241 -- This should not happen
246242 if not opts .line1 then
247- log (" No line1 range provided" , " ERROR " )
243+ logger . error (" No line1 range provided" )
248244 end
249245 return 2
250246 end
@@ -258,7 +254,7 @@ local function command_preview(opts, preview_ns, preview_buf)
258254 undo_deletions = command .hl_groups [" deletion" ] ~= false ,
259255 inline_highlighting = command .inline_highlighting ,
260256 })
261- log (function ()
257+ logger . trace (function ()
262258 return " Highlights: " .. vim .inspect (highlights )
263259 end )
264260
@@ -283,12 +279,12 @@ local function restore_buffer_state()
283279 vim .o .lazyredraw = prev_lazyredraw
284280 should_cache_lines = true
285281 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 )))
287283 end
288284end
289285
290286local function execute_command (command )
291- log (" Executing command: " .. command )
287+ logger . trace (" Executing command: " .. command )
292288 vim .cmd (command )
293289 restore_buffer_state ()
294290end
@@ -408,22 +404,18 @@ M.setup = function(user_config)
408404 local config = vim .tbl_deep_extend (" force" , M .defaults , user_config or {})
409405 validate_config (config )
410406 create_user_commands (config .commands )
407+ logger = require (" live-command.logger" )
411408 remote = require (" live-command.remote" )
412409
413- remote .init_rpc (function (id )
410+ remote .init_rpc (logger , function (id )
414411 chan_id = id
415412 end )
416413 create_autocmds ()
414+ end
417415
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_
427419end
428420
429421M .version = " 1.3.0"
0 commit comments