2024-08-17 15:14:30 +08:00
|
|
|
local api = vim.api
|
2024-08-12 23:53:28 +08:00
|
|
|
|
2024-08-17 15:14:30 +08:00
|
|
|
local Tiktoken = require("avante.tiktoken")
|
|
|
|
local Sidebar = require("avante.sidebar")
|
|
|
|
local Config = require("avante.config")
|
|
|
|
local Diff = require("avante.diff")
|
2024-08-17 22:29:05 +08:00
|
|
|
local Selection = require("avante.selection")
|
2024-08-17 15:14:30 +08:00
|
|
|
|
|
|
|
---@class Avante
|
|
|
|
local M = {
|
|
|
|
---@type avante.Sidebar[] we use this to track chat command across tabs
|
|
|
|
sidebars = {},
|
|
|
|
---@type avante.Sidebar
|
|
|
|
current = nil,
|
2024-08-17 22:29:05 +08:00
|
|
|
selection = nil,
|
2024-08-17 15:14:30 +08:00
|
|
|
_once = 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" })
|
|
|
|
end
|
|
|
|
|
|
|
|
H.keymaps = function()
|
2024-08-17 22:29:05 +08:00
|
|
|
vim.keymap.set({ "n", "v" }, Config.mappings.ask, M.toggle, { noremap = true })
|
2024-08-17 15:14:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
H.autocmds = function()
|
2024-08-15 21:08:26 -04:00
|
|
|
local ok, LazyConfig = pcall(require, "lazy.core.config")
|
2024-08-17 15:14:30 +08:00
|
|
|
|
2024-08-15 21:08:26 -04:00
|
|
|
if ok then
|
|
|
|
local name = "avante.nvim"
|
2024-08-17 15:14:30 +08:00
|
|
|
local load_path = function()
|
|
|
|
require("tiktoken_lib").load()
|
|
|
|
Tiktoken.setup("gpt-4o")
|
|
|
|
end
|
|
|
|
|
2024-08-15 21:08:26 -04:00
|
|
|
if LazyConfig.plugins[name] and LazyConfig.plugins[name]._.loaded then
|
2024-08-17 15:14:30 +08:00
|
|
|
vim.schedule(load_path)
|
2024-08-15 21:08:26 -04:00
|
|
|
else
|
2024-08-17 15:14:30 +08:00
|
|
|
api.nvim_create_autocmd("User", {
|
2024-08-15 21:08:26 -04:00
|
|
|
pattern = "LazyLoad",
|
|
|
|
callback = function(event)
|
|
|
|
if event.data == name then
|
2024-08-17 15:14:30 +08:00
|
|
|
load_path()
|
2024-08-15 21:08:26 -04:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2024-08-17 15:14:30 +08:00
|
|
|
|
|
|
|
api.nvim_create_autocmd("User", {
|
|
|
|
pattern = "VeryLazy",
|
|
|
|
callback = load_path,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
api.nvim_create_autocmd("TabClosed", {
|
|
|
|
pattern = "*",
|
|
|
|
callback = function(ev)
|
|
|
|
local tab = tonumber(ev.file)
|
|
|
|
local s = M.sidebars[tab]
|
|
|
|
if s then
|
|
|
|
s:destroy()
|
|
|
|
end
|
2024-08-17 22:29:05 +08:00
|
|
|
if tab ~= nil then
|
|
|
|
M.sidebars[tab] = nil
|
|
|
|
end
|
2024-08-17 15:14:30 +08:00
|
|
|
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
|
|
|
|
function M._get(current)
|
|
|
|
local tab = api.nvim_get_current_tabpage()
|
|
|
|
local sidebar = M.sidebars[tab]
|
|
|
|
if current ~= false then
|
|
|
|
M.current = sidebar
|
|
|
|
end
|
|
|
|
return sidebar
|
|
|
|
end
|
|
|
|
|
|
|
|
M.open = function()
|
|
|
|
local tab = api.nvim_get_current_tabpage()
|
|
|
|
local sidebar = M.sidebars[tab]
|
|
|
|
|
|
|
|
if not sidebar then
|
|
|
|
sidebar = Sidebar:new(tab)
|
|
|
|
M.sidebars[tab] = sidebar
|
|
|
|
end
|
|
|
|
|
|
|
|
M.current = sidebar
|
|
|
|
|
|
|
|
return sidebar:open()
|
|
|
|
end
|
|
|
|
|
|
|
|
M.toggle = function()
|
|
|
|
local sidebar = M._get()
|
|
|
|
if not sidebar then
|
|
|
|
M.open()
|
|
|
|
return true
|
2024-08-15 21:08:26 -04:00
|
|
|
end
|
|
|
|
|
2024-08-17 15:14:30 +08:00
|
|
|
return sidebar:toggle()
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? avante.Config
|
|
|
|
function M.setup(opts)
|
|
|
|
---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._once then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
Diff.setup({
|
|
|
|
debug = false, -- log output to console
|
|
|
|
default_mappings = Config.mappings.diff, -- disable buffer local mapping created by this plugin
|
|
|
|
default_commands = true, -- disable commands created by this plugin
|
|
|
|
disable_diagnostics = true, -- This will disable the diagnostics in a buffer whilst it is conflicted
|
|
|
|
list_opener = "copen",
|
|
|
|
highlights = Config.highlights.diff,
|
|
|
|
})
|
|
|
|
|
2024-08-17 22:29:05 +08:00
|
|
|
local selection = Selection:new()
|
|
|
|
selection:setup()
|
|
|
|
M.selection = selection
|
|
|
|
|
2024-08-17 15:14:30 +08:00
|
|
|
-- setup helpers
|
|
|
|
H.autocmds()
|
|
|
|
H.commands()
|
|
|
|
H.keymaps()
|
|
|
|
|
|
|
|
M._once = true
|
2024-08-12 23:53:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|