-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinit.lua
More file actions
258 lines (220 loc) · 5.74 KB
/
init.lua
File metadata and controls
258 lines (220 loc) · 5.74 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
---@diagnostic disable: undefined-global
local api = vim.api
local Utils = require("eca.utils")
local Logger = require("eca.logger")
local Sidebar = require("eca.sidebar")
local Config = require("eca.config")
local Server = require("eca.server")
---@class Eca
local M = {
---@type eca.Sidebar[] we use this to track chat command across tabs
sidebars = {},
---@type {sidebar?: eca.Sidebar}
current = { sidebar = nil },
---@type eca.Server
server = nil,
}
M.did_setup = false
local H = {}
function H.keymaps()
vim.keymap.set({ "n", "v" }, "<Plug>(EcaChat)", function()
require("eca.api").chat()
end, { noremap = true })
vim.keymap.set("n", "<Plug>(EcaToggle)", function()
M.toggle()
end, { noremap = true })
vim.keymap.set("n", "<Plug>(EcaFocus)", function()
require("eca.api").focus()
end, { noremap = true })
if Config.behavior and Config.behavior.auto_set_keymaps then
Utils.safe_keymap_set({ "n", "v" }, Config.mappings.chat, function()
require("eca.api").chat()
end, { desc = "eca: open chat" })
Utils.safe_keymap_set("n", Config.mappings.focus, function()
require("eca.api").focus()
end, { desc = "eca: focus" })
Utils.safe_keymap_set("n", Config.mappings.toggle, function()
M.toggle()
end, { desc = "eca: toggle" })
end
end
function H.signs()
vim.fn.sign_define("EcaInputPromptSign", { text = Config.windows.input.prefix })
end
H.augroup = api.nvim_create_augroup("eca_autocmds", { clear = true })
function H.autocmds()
api.nvim_create_autocmd("TabEnter", {
group = H.augroup,
pattern = "*",
once = true,
callback = function(ev)
local tab = tonumber(ev.file)
M._init(tab or api.nvim_get_current_tabpage())
end,
})
api.nvim_create_autocmd("VimResized", {
group = H.augroup,
callback = function()
local sidebar = M.get()
if not sidebar then
return
end
if not sidebar:is_open() then
return
end
sidebar:resize()
end,
})
api.nvim_create_autocmd("QuitPre", {
group = H.augroup,
callback = function()
local current_buf = vim.api.nvim_get_current_buf()
if Utils.is_sidebar_buffer(current_buf) then
return
end
local non_sidebar_wins = 0
local sidebar_wins = {}
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_is_valid(win) then
local win_buf = vim.api.nvim_win_get_buf(win)
if Utils.is_sidebar_buffer(win_buf) then
table.insert(sidebar_wins, win)
else
non_sidebar_wins = non_sidebar_wins + 1
end
end
end
if non_sidebar_wins <= 1 then
for _, win in ipairs(sidebar_wins) do
pcall(vim.api.nvim_win_close, win, false)
end
end
end,
nested = true,
})
api.nvim_create_autocmd("TabClosed", {
group = H.augroup,
pattern = "*",
callback = function(ev)
local tab = tonumber(ev.file)
local s = M.sidebars[tab]
if s then
s:reset()
end
if tab ~= nil then
M.sidebars[tab] = nil
end
end,
})
vim.schedule(function()
M._init(api.nvim_get_current_tabpage())
end)
local function setup_colors()
Logger.debug("Setting up eca colors")
require("eca.highlights").setup()
end
api.nvim_create_autocmd("ColorSchemePre", {
group = H.augroup,
callback = function()
vim.schedule(function()
setup_colors()
end)
end,
})
api.nvim_create_autocmd("ColorScheme", {
group = H.augroup,
callback = function()
vim.schedule(function()
setup_colors()
end)
end,
})
-- automatically setup Eca filetype to markdown
vim.treesitter.language.register("markdown", "Eca")
end
---@param current boolean? false to disable setting current, otherwise use this to track across tabs.
---@return eca.Sidebar
function M.get(current)
local tab = api.nvim_get_current_tabpage()
local sidebar = M.sidebars[tab]
if current ~= false then
M.current.sidebar = sidebar
end
return sidebar
end
---@param id integer
function M._init(id)
local sidebar = M.sidebars[id]
if not sidebar then
sidebar = Sidebar.new(id, M.mediator)
M.sidebars[id] = sidebar
end
M.current = { sidebar = sidebar }
return M
end
M.toggle = { api = true }
---@param opts? table
function M.toggle_sidebar(opts)
opts = opts or {}
local sidebar = M.get()
if not sidebar then
M._init(api.nvim_get_current_tabpage())
M.current.sidebar:open(opts)
return true
end
return sidebar:toggle(opts)
end
function M.is_sidebar_open()
local sidebar = M.get()
if not sidebar then
return false
end
return sidebar:is_open()
end
---@param opts? table
function M.open_sidebar(opts)
opts = opts or {}
local sidebar = M.get()
if not sidebar then
M._init(api.nvim_get_current_tabpage())
end
M.current.sidebar:open(opts)
end
function M.close_sidebar()
local sidebar = M.get()
if not sidebar then
return
end
sidebar:close()
end
setmetatable(M.toggle, {
__index = M.toggle,
__call = function()
M.toggle_sidebar()
end,
})
---@param opts? eca.Config
function M.setup(opts)
Config.setup(opts or {})
if M.did_setup then
return
end
require("eca.logger").setup(Config.options.log)
require("eca.highlights").setup()
require("eca.commands").setup()
-- setup helpers
H.autocmds()
H.keymaps()
H.signs()
-- Initialize the ECA server with callbacks
M.state = require("eca.state").new()
M.server = Server.new()
M.mediator = require("eca.mediator").new(M.server, M.state)
if Config.behavior and Config.behavior.auto_start_server then
vim.defer_fn(function()
M.server:start()
end, 100) -- Small delay to ensure everything is loaded
end
M.did_setup = true
end
return M