-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathautocmds.lua
More file actions
189 lines (166 loc) · 5.98 KB
/
autocmds.lua
File metadata and controls
189 lines (166 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
local input_window = require('opencode.ui.input_window')
local output_window = require('opencode.ui.output_window')
local M = {}
function M.setup_autocmds(windows)
local group = vim.api.nvim_create_augroup('OpencodeWindows', { clear = true })
input_window.setup_autocmds(windows, group)
output_window.setup_autocmds(windows, group)
-- Only keep shared autocmds here (e.g., WinClosed, WinLeave for all windows)
local wins = { windows.input_win, windows.output_win, windows.footer_win }
vim.api.nvim_create_autocmd('WinClosed', {
group = group,
pattern = table.concat(wins, ','),
callback = function(opts)
-- Don't close everything if we're just toggling the input window
if input_window._toggling then
return
end
local closed_win = tonumber(opts.match)
if vim.tbl_contains(wins, closed_win) then
vim.schedule(function()
require('opencode.ui.ui').teardown_visible_windows(windows)
end)
end
end,
})
vim.api.nvim_create_autocmd({ 'BufWinEnter', 'BufFilePost', 'WinLeave' }, {
group = group,
pattern = '*',
callback = function(args)
if args.file == '' then
return
end
local state = require('opencode.state')
state.last_code_win_before_opencode = vim.api.nvim_get_current_win()
state.current_code_buf = vim.api.nvim_get_current_buf()
end,
})
vim.api.nvim_create_autocmd('WinEnter', {
group = group,
pattern = '*',
callback = function()
require('opencode.state').is_opencode_focused = require('opencode.ui.ui').is_opencode_focused()
end,
})
vim.api.nvim_create_autocmd('DirChanged', {
pattern = { 'global', 'tabpage' },
group = group,
callback = function(event)
local state = require('opencode.state')
if state.current_cwd == event.file then
return
end
if event.match == 'tabpage' then
local windows = state.windows
if not windows or not windows.output_win or not vim.api.nvim_win_is_valid(windows.output_win) then
return
end
local ok, opencode_tab = pcall(vim.api.nvim_win_get_tabpage, windows.output_win)
if not ok then
return
end
local changed_tab = vim.api.nvim_get_current_tabpage()
local changed_window = event.data and event.data.changed_window
if changed_window and vim.api.nvim_win_is_valid(changed_window) then
local win_ok, win_tab = pcall(vim.api.nvim_win_get_tabpage, changed_window)
if win_ok then
changed_tab = win_tab
end
end
if changed_tab ~= opencode_tab then
return
end
end
state.current_cwd = event.file
local core = require('opencode.core')
core.handle_directory_change()
end,
})
if require('opencode.config').ui.position == 'current' then
vim.api.nvim_create_autocmd('BufEnter', {
group = group,
callback = function()
local current_win = vim.api.nvim_get_current_win()
local current_buf = vim.api.nvim_get_current_buf()
if current_win ~= windows.output_win and current_win ~= windows.input_win then
return
end
local is_opencode_buf = (
current_buf == windows.output_buf
or current_buf == windows.input_buf
or (windows.footer_buf and current_buf == windows.footer_buf)
)
if not is_opencode_buf then
vim.schedule(function()
require('opencode.ui.ui').teardown_visible_windows(windows)
end)
end
end,
})
end
M.setup_window_only_keymap(windows)
end
--- Set <C-w>o / <C-w><C-o> on both opencode buffers to close all other
--- non-floating windows while keeping the opencode pair intact.
--- Also clears the saved width ratio so the next open uses the config default.
---@param windows OpencodeWindowState
function M.setup_window_only_keymap(windows)
local opencode_wins = function()
local t = {}
for _, w in ipairs({ windows.input_win, windows.output_win, windows.footer_win }) do
if w then
t[w] = true
end
end
return t
end
local handler = function()
local keep = opencode_wins()
keep[vim.api.nvim_get_current_win()] = true
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if not keep[win] and vim.api.nvim_win_is_valid(win) then
local cfg = vim.api.nvim_win_get_config(win)
if cfg.relative == '' then
pcall(vim.api.nvim_win_close, win, false)
end
end
end
-- Don't remember the current opencode width; next open will use config default
require('opencode.state').last_window_width_ratio = nil
end
for _, buf in ipairs({ windows.input_buf, windows.output_buf }) do
if buf and vim.api.nvim_buf_is_valid(buf) then
vim.keymap.set('n', '<C-w>o', handler, { buffer = buf, desc = 'Keep only opencode windows', nowait = true })
vim.keymap.set('n', '<C-w><C-o>', handler, { buffer = buf, desc = 'Keep only opencode windows', nowait = true })
end
end
end
---@param windows OpencodeWindowState?
function M.setup_resize_handler(windows)
local resize_group = vim.api.nvim_create_augroup('OpencodeResize', { clear = true })
vim.api.nvim_create_autocmd('VimResized', {
group = resize_group,
callback = function()
require('opencode.ui.topbar').render()
require('opencode.ui.footer').update_window(windows)
input_window.update_dimensions(windows)
output_window.update_dimensions(windows)
end,
})
vim.api.nvim_create_autocmd('WinResized', {
group = resize_group,
callback = function(args)
local win = tonumber(args.match) --[[@as integer]]
if not win or not vim.api.nvim_win_is_valid(win) or not output_window.mounted() then
return
end
local floating = vim.api.nvim_win_get_config(win).relative ~= ''
if floating then
return
end
require('opencode.ui.topbar').render()
require('opencode.ui.footer').update_window(windows)
end,
})
end
return M