@@ -3,6 +3,7 @@ local output_window = require('opencode.ui.output_window')
33local IdleDetector = require (' opencode.idle' )
44local M = {}
55
6+ --- @type IdleDetector | nil
67local idle_detector = nil
78
89function M .setup_autocmds (windows )
@@ -18,33 +19,55 @@ function M.setup_autocmds(windows)
1819 callback = function (opts )
1920 local closed_win = tonumber (opts .match )
2021 if vim .tbl_contains (wins , closed_win ) then
21- vim .schedule (function ()
22- require (' opencode.ui.ui' ).close_windows (windows )
23- end )
22+ -- Guard against git commit workflows that manage their own buffers/windows
23+ for _ , win in ipairs (vim .api .nvim_list_wins ()) do
24+ if vim .api .nvim_win_is_valid (win ) then
25+ local buf = vim .api .nvim_win_get_buf (win )
26+ local ft = vim .api .nvim_get_option_value (' filetype' , { buf = buf })
27+ if ft == ' gitcommit' or ft == ' NeogitCommitMessage' then
28+ return
29+ end
30+ end
31+ end
32+ -- Immediate protected close without schedule to avoid race conditions
33+ pcall (require (' opencode.ui.ui' ).close_windows , windows )
2434 end
2535 end ,
2636 })
2737
2838 -- Setup idle detection for automatic context updates
29- if idle_detector then
30- idle_detector :stop ()
31- end
32-
33- local config = require (' opencode.config' )
34- local threshold = config .get (' context' ).idle_threshold or 10000
35-
36- idle_detector = IdleDetector .new ({
37- threshold = threshold ,
39+ -- if idle_detector ~= nil then
40+ -- idle_detector:stop()
41+ -- end
42+ --
43+ -- local config = require('opencode.config')
44+ -- local threshold = config.get('context').idle_threshold or 10000
45+ --
46+ -- idle_detector = IdleDetector.new({
47+ -- threshold = threshold,
48+ -- callback = function()
49+ -- -- Update context only if user is not focused on opencode window
50+ -- if not require('opencode.ui.ui').is_opencode_focused() then
51+ -- local ft = vim.api.nvim_get_option_value('filetype', { buf = 0 })
52+ -- if ft == 'gitcommit' or ft == 'NeogitCommitMessage' then
53+ -- return
54+ -- end
55+ -- require('opencode.context').load()
56+ -- require('opencode.state').last_code_win_before_opencode = vim.api.nvim_get_current_win()
57+ -- end
58+ -- end,
59+ -- })
60+ --
61+ -- idle_detector:start()
62+
63+ -- Defensive cleanup on exit in case window close didn't trigger
64+ vim .api .nvim_create_autocmd (' VimLeavePre' , {
65+ group = group ,
66+ once = true ,
3867 callback = function ()
39- -- Update context only if user is not focused on opencode window
40- if not require (' opencode.ui.ui' ).is_opencode_focused () then
41- require (' opencode.context' ).load ()
42- require (' opencode.state' ).last_code_win_before_opencode = vim .api .nvim_get_current_win ()
43- end
68+ M .cleanup ()
4469 end ,
4570 })
46-
47- idle_detector :start ()
4871end
4972
5073function M .setup_resize_handler (windows )
@@ -79,7 +102,7 @@ function M.setup_resize_handler(windows)
79102end
80103
81104function M .cleanup ()
82- if idle_detector then
105+ if idle_detector ~= nil then
83106 idle_detector :stop ()
84107 idle_detector = nil
85108 end
0 commit comments