1- -- File watching logic inspired and adapted from sidekick.nvim
2- -- https://github.com/folke/sidekick.nvim
3-
41local config = require (" codecompanion.config" )
52local keymaps = require (" codecompanion.utils.keymaps" )
63local log = require (" codecompanion.utils.log" )
74local registry = require (" codecompanion.interactions.shared.registry" )
85local utils = require (" codecompanion.utils" )
6+ local watch = require (" codecompanion.interactions.shared.watch" )
97
108local api = vim .api
119
12- local uv = vim .uv or vim .loop
13-
1410--- @type table
1511_G .codecompanion_cli_metadata = {}
1612
@@ -22,20 +18,11 @@ _G.codecompanion_cli_metadata = {}
2218--- @field id number
2319--- @field provider CodeCompanion.CLI.Provider
2420--- @field ui CodeCompanion.CLI.UI
25- --- @field watch_cwd string | nil
2621local CLI = {}
2722
2823local clis = {} --- @type table<number , CodeCompanion.CLI>
2924local last_cli = nil --- @type CodeCompanion.CLI | nil
3025
31- --- @class CodeCompanion.CLI.Watcher
32- --- @field watcher uv.uv_fs_event_t
33- --- @field timer uv.uv_timer_t
34- --- @field count number
35-
36- --- Shared watchers keyed by cwd — one watcher per directory, ref-counted
37- local watchers = {} --- @type table<string , CodeCompanion.CLI.Watcher>
38-
3926--- Keymap callbacks for the CLI buffer
4027local keymap_callbacks = {
4128 next_chat = {
@@ -128,10 +115,7 @@ function CLI.create(args)
128115 })
129116 end
130117
131- if config .interactions .cli .opts .reload then
132- self .watch_cwd = vim .fn .getcwd ()
133- self ._start_watcher (self .watch_cwd )
134- end
118+ watch .enable ()
135119
136120 clis [bufnr ] = self
137121 last_cli = self
251235--- Close the CLI instance and clean up
252236--- @return nil
253237function CLI :close ()
254- if self .watch_cwd then
255- self ._stop_watcher (self .watch_cwd )
256- end
257238 self .provider :stop ()
258239
259240 pcall (api .nvim_del_augroup_by_id , self .aug )
@@ -274,70 +255,6 @@ function CLI:close()
274255 utils .fire (" CLIClosed" , { bufnr = self .bufnr })
275256end
276257
277- -- =============================================================================
278- -- Watchers - Monitor files in the cwd for any changes
279- -- =============================================================================
280-
281- --- Start a watcher for a given cwd
282- --- @param cwd string
283- --- @return nil
284- function CLI ._start_watcher (cwd )
285- if watchers [cwd ] then
286- watchers [cwd ].count = watchers [cwd ].count + 1
287- return
288- end
289-
290- local watcher = uv .new_fs_event ()
291- if not watcher then
292- return log :warn (" Could not create file watcher for %s" , cwd )
293- end
294-
295- local timer = uv .new_timer ()
296- if not timer then
297- watcher :close ()
298- return log :warn (" Could not create debounce timer for %s" , cwd )
299- end
300-
301- watcher :start (cwd , { recursive = true }, function ()
302- if not timer :is_closing () then
303- timer :stop ()
304- timer :start (100 , 0 , function ()
305- vim .schedule (vim .cmd .checktime )
306- end )
307- end
308- end )
309-
310- watchers [cwd ] = { watcher = watcher , timer = timer , count = 1 }
311- log :debug (" File watcher started on %s" , cwd )
312- end
313-
314- --- Stop a watcher for the given cwd
315- --- @param cwd string
316- --- @return nil
317- function CLI ._stop_watcher (cwd )
318- local entry = watchers [cwd ]
319- if not entry then
320- return
321- end
322-
323- entry .count = entry .count - 1
324- if entry .count > 0 then
325- return
326- end
327-
328- if not entry .timer :is_closing () then
329- entry .timer :stop ()
330- entry .timer :close ()
331- end
332- if not entry .watcher :is_closing () then
333- entry .watcher :stop ()
334- entry .watcher :close ()
335- end
336-
337- watchers [cwd ] = nil
338- log :debug (" File watcher stopped for %s" , cwd )
339- end
340-
341258-- =============================================================================
342259-- Public API
343260-- =============================================================================
0 commit comments