From 1ce3efbfe68c472ef99ca1096be41740e31d6535 Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Fri, 14 Feb 2025 12:42:51 +0800 Subject: [PATCH] show provider selector --- lua/avante/config.lua | 4 +++ lua/avante/init.lua | 13 ++++++++ lua/avante/sidebar.lua | 70 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 3f81548..bf8b7c3 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -256,8 +256,12 @@ M._defaults = { files = { add_current = "ac", -- Add current buffer to selected files }, + providers = { + show = "ap", -- Show providers selector + }, }, windows = { + ---@alias AvantePosition "right" | "left" | "top" | "bottom" | "smart" position = "right", wrap = true, -- similar to vim.o.wrap diff --git a/lua/avante/init.lua b/lua/avante/init.lua index 4c15bf4..f008326 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -68,6 +68,11 @@ H.keymaps = function() vim.keymap.set("n", "(AvanteToggleHint)", function() M.toggle.hint() end) vim.keymap.set("n", "(AvanteToggleSuggestion)", function() M.toggle.suggestion() end) + vim.keymap.set("n", "(AvanteShowProviders)", function() + local sidebar = M.get() + if sidebar then sidebar:show_providers_selector() end + end) + vim.keymap.set({ "n", "v" }, "(AvanteConflictOurs)", function() Diff.choose("ours") end) vim.keymap.set({ "n", "v" }, "(AvanteConflictBoth)", function() Diff.choose("both") end) vim.keymap.set({ "n", "v" }, "(AvanteConflictTheirs)", function() Diff.choose("theirs") end) @@ -121,6 +126,14 @@ H.keymaps = function() function() M.toggle.suggestion() end, { desc = "avante: toggle suggestion" } ) + Utils.safe_keymap_set("n", Config.mappings.show_providers, function() + local sidebar = M.get() + if sidebar then sidebar:show_providers_selector() end + end, { + desc = "avante: show providers selector", + noremap = true, + silent = true, + }) Utils.safe_keymap_set("n", Config.mappings.toggle.repomap, function() require("avante.repo_map").show() end, { desc = "avante: display repo map", noremap = true, diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index c8c5a3c..2818252 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1219,6 +1219,12 @@ end --- Initialize the sidebar instance. --- @return avante.Sidebar The Sidebar instance. function Sidebar:initialize() + -- Add keymap to show providers selector + vim.keymap.set("n", "ap", function() self:show_providers_selector() end, { noremap = true, silent = true }) + + self.code.winid = api.nvim_get_current_win() + self.code.bufnr = api.nvim_get_current_buf() + self.code.selection = Utils.get_visual_selection_and_range() self.code.winid = api.nvim_get_current_win() self.code.bufnr = api.nvim_get_current_buf() self.code.selection = Utils.get_visual_selection_and_range() @@ -2256,6 +2262,70 @@ function Sidebar:render(opts) return self end +function Sidebar:show_providers_selector() + local providers = require("avante.providers") + local current_provider = Config.provider + + -- Create a new buffer + local bufnr = api.nvim_create_buf(false, true) + + -- Set buffer content + local lines = {} + for provider, _ in pairs(providers) do + if provider == current_provider then + table.insert(lines, "> " .. provider .. " <") + else + table.insert(lines, " " .. provider) + end + end + api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + + -- Create a floating window + local width = 40 + local height = #lines + local win_opts = { + relative = "editor", + width = width, + height = height, + col = (vim.o.columns - width) / 2, + row = (vim.o.lines - height) / 2 - 1, + style = "minimal", + border = "rounded", + } + + local winid = api.nvim_open_win(bufnr, true, win_opts) + + -- Highlight current provider + api.nvim_buf_add_highlight(bufnr, -1, "AvanteProviderCurrent", 0, 0, -1) + + -- Key mappings + local function move_selection(direction) + local cursor = api.nvim_win_get_cursor(winid) + local line = cursor[1] + + if direction == "up" and line > 1 then + api.nvim_win_set_cursor(winid, { line - 1, 0 }) + elseif direction == "down" and line < #lines then + api.nvim_win_set_cursor(winid, { line + 1, 0 }) + end + end + + local function select_provider() + local cursor = api.nvim_win_get_cursor(winid) + local line = cursor[1] + local selected_provider = lines[line]:match("%S+$") + + if selected_provider and selected_provider ~= current_provider then providers.refresh(selected_provider) end + + api.nvim_win_close(winid, true) + end + + vim.keymap.set("n", "", function() move_selection("up") end, { buffer = bufnr }) + vim.keymap.set("n", "", function() move_selection("down") end, { buffer = bufnr }) + vim.keymap.set("n", "", select_provider, { buffer = bufnr }) + vim.keymap.set("n", "q", function() api.nvim_win_close(winid, true) end, { buffer = bufnr }) +end + function Sidebar:create_selected_files_container() if self.selected_files_container then self.selected_files_container:unmount() end