forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocmds.lua
More file actions
149 lines (130 loc) · 4.51 KB
/
autocmds.lua
File metadata and controls
149 lines (130 loc) · 4.51 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
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').close_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').close_windows(windows)
end)
end
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