-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathautocmds.lua
More file actions
119 lines (105 loc) · 3.59 KB
/
autocmds.lua
File metadata and controls
119 lines (105 loc) · 3.59 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
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', {
group = group,
callback = function(event)
local state = require('opencode.state')
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