Skip to content

Commit

Permalink
refactor(ui): bounding popover
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm committed Aug 15, 2024
1 parent f7de743 commit 3cda13b
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 240 deletions.
103 changes: 100 additions & 3 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,108 @@
local M = {}
local sidebar = require("avante.sidebar")
local tiktoken = require("avante.tiktoken")
local Sidebar = require("avante.sidebar")
local config = require("avante.config")
local diff = require("avante.diff")

local M = {
---@type avante.Sidebar
sidebars = {},
---@type avante.Sidebar
current = nil,
}

---@param current boolean? false to disable setting current, otherwise use this to track across tabs.
---@return avante.Sidebar
function M._get(current)
local tab = vim.api.nvim_get_current_tabpage()
local sidebar = M.sidebars[tab]
if current ~= false then
M.current = sidebar
end
return sidebar
end

---Run a sidebar method by getting the sidebar of current tabpage, with args
---noop if sidebar is nil
---@param method "open"|"close"|"toggle"|"focus"
---@param args table? arguments to parse
---@return any return_of_method
function M._call(method, args)
local sidebar = M._get()
if not sidebar then
return
end

args = args or {}
return sidebar[method](sidebar, unpack(args))
end

M.open = function()
local tab = vim.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.close = function()
return M._call("close")
end

M.focus = function()
return M._call("focus")
end

M.toggle = function()
local sidebar = M._get()
if not sidebar then
M.open()
return true
end

return sidebar:toggle()
end

function M.setup(opts)
require("tiktoken_lib").load()

config.update(opts)
sidebar.setup()

tiktoken.setup("gpt-4o")

diff.setup({
debug = false, -- log output to console
default_mappings = config.get().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.get().highlights.diff,
})

vim.api.nvim_create_user_command("AvanteAsk", function()
M.toggle()
end, { nargs = 0 })
vim.api.nvim_set_keymap(
"n",
config.get().mappings.show_sidebar,
"<cmd>AvanteAsk<CR>",
{ noremap = true, silent = true }
)

vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("Avante", { clear = true }),
pattern = "Avante",
callback = function(event)
vim.api.nvim_buf_set_keymap(event.buf, "n", "q", '<cmd>:lua require("avante").close()<cr>', { silent = true })
end,
})

vim.treesitter.language.register("markdown", "Avante")
end

return M
Loading

0 comments on commit 3cda13b

Please sign in to comment.