-
Notifications
You must be signed in to change notification settings - Fork 246
/
init.lua
277 lines (238 loc) · 6.54 KB
/
init.lua
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
local api = vim.api
local Utils = require("avante.utils")
local Sidebar = require("avante.sidebar")
local Selection = require("avante.selection")
local Config = require("avante.config")
---@class Avante
local M = {
---@type avante.Sidebar[] we use this to track chat command across tabs
sidebars = {},
---@type avante.Selection[]
selections = {},
---@type {sidebar?: avante.Sidebar, selection?: avante.Selection}
current = { sidebar = nil, selection = nil },
}
M.did_setup = false
local H = {}
H.commands = function()
local cmd = function(n, c, o)
o = vim.tbl_extend("force", { nargs = 0 }, o or {})
api.nvim_create_user_command("Avante" .. n, c, o)
end
cmd("Ask", function()
M.toggle()
end, { desc = "avante: ask AI for code suggestions" })
cmd("Close", function()
local sidebar, _ = M._get()
if not sidebar then
return
end
sidebar:close()
end, { desc = "avante: close chat window" })
cmd("Refresh", function()
M.refresh()
end, { desc = "avante: refresh windows" })
end
H.keymaps = function()
vim.keymap.set({ "n", "v" }, Config.mappings.ask, M.toggle, { noremap = true })
vim.keymap.set("v", Config.mappings.edit, M.edit, { noremap = true })
vim.keymap.set("n", Config.mappings.refresh, M.refresh, { noremap = true })
Utils.toggle_map("n", Config.mappings.toggle.debug, {
name = "debug",
get = function()
return Config.debug
end,
set = function(state)
Config.override({ debug = state })
end,
})
Utils.toggle_map("n", Config.mappings.toggle.hint, {
name = "hint",
get = function()
return Config.hints.enabled
end,
set = function(state)
Config.override({ hints = { enabled = state } })
end,
})
end
H.signs = function()
vim.fn.sign_define("AvanteInputPromptSign", { text = Config.windows.input.prefix })
end
H.augroup = api.nvim_create_augroup("avante_autocmds", { clear = true })
H.autocmds = function()
local ok, LazyConfig = pcall(require, "lazy.core.config")
if ok then
local name = "avante.nvim"
local load_path = function()
require("tiktoken_lib").load()
require("avante.tiktoken").setup("gpt-4o")
end
if LazyConfig.plugins[name] and LazyConfig.plugins[name]._.loaded then
vim.schedule(load_path)
else
api.nvim_create_autocmd("User", {
pattern = "LazyLoad",
callback = function(event)
if event.data == name then
load_path()
return true
end
end,
})
end
api.nvim_create_autocmd("User", {
pattern = "VeryLazy",
callback = load_path,
})
end
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())
if Config.hints.enabled and not M.current.selection.did_setup then
M.current.selection:setup_autocmds()
end
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("TabClosed", {
group = H.augroup,
pattern = "*",
callback = function(ev)
local tab = tonumber(ev.file)
local s = M.sidebars[tab]
local sl = M.selections[tab]
if s then
s:reset()
end
if sl then
sl:delete_autocmds()
end
if tab ~= nil then
M.sidebars[tab] = nil
end
end,
})
vim.schedule(function()
M._init(api.nvim_get_current_tabpage())
if Config.hints.enabled then
M.current.selection:setup_autocmds()
end
end)
api.nvim_create_autocmd("ColorSchemePre", {
group = H.augroup,
callback = function()
require("avante.highlights").setup()
end,
})
-- automatically setup Avante filetype to markdown
vim.treesitter.language.register("markdown", "Avante")
end
---@param current boolean? false to disable setting current, otherwise use this to track across tabs.
---@return avante.Sidebar, avante.Selection
function M._get(current)
local tab = api.nvim_get_current_tabpage()
local sidebar = M.sidebars[tab]
local selection = M.selections[tab]
if current ~= false then
M.current.sidebar = sidebar
M.current.selection = selection
end
return sidebar, selection
end
---@param id integer
function M._init(id)
local sidebar = M.sidebars[id]
local selection = M.selections[id]
if not sidebar then
sidebar = Sidebar:new(id)
M.sidebars[id] = sidebar
end
if not selection then
selection = Selection:new(id)
M.selections[id] = selection
end
M.current = { sidebar = sidebar, selection = selection }
return M
end
M.toggle = function()
local sidebar, _ = M._get()
if not sidebar then
M._init(api.nvim_get_current_tabpage())
M.current.sidebar:open()
return true
end
return sidebar:toggle()
end
M.edit = function()
local _, selection = M._get()
if not selection then
return
end
selection:create_editing_input()
end
M.refresh = function()
local sidebar, _ = M._get()
if not sidebar then
return
end
local curbuf = vim.api.nvim_get_current_buf()
local focused = sidebar.result.bufnr == curbuf or sidebar.input.bufnr == curbuf
if focused or not sidebar:is_open() then
return
end
local listed = vim.api.nvim_get_option_value("buflisted", { buf = curbuf })
if Utils.is_sidebar_buffer(curbuf) or not listed then
return
end
local curwin = vim.api.nvim_get_current_win()
sidebar:close()
sidebar.code.winid = curwin
sidebar.code.bufnr = curbuf
sidebar:render()
end
---@param opts? avante.Config
function M.setup(opts)
if vim.fn.has("nvim-0.10") == 0 then
vim.api.nvim_echo({
{ "Avante requires at least nvim-0.10", "ErrorMsg" },
{ "Please upgrade your neovim version", "WarningMsg" },
{ "Press any key to exit", "ErrorMsg" },
}, true, {})
vim.fn.getchar()
vim.cmd([[quit]])
end
---PERF: we can still allow running require("avante").setup() multiple times to override config if users wish to
---but most of the other functionality will only be called once from lazy.nvim
Config.setup(opts)
if M.did_setup then
return
end
require("avante.history").setup()
require("avante.highlights").setup()
require("avante.diff").setup()
require("avante.providers").setup()
-- setup helpers
H.autocmds()
H.commands()
H.keymaps()
H.signs()
M.did_setup = true
end
return M