diff --git a/lua/avante/api.lua b/lua/avante/api.lua index bd010fcca..c790a706c 100644 --- a/lua/avante/api.lua +++ b/lua/avante/api.lua @@ -146,7 +146,7 @@ return setmetatable(M, { ---@class AvailableApi: ApiCaller ---@field api? boolean local has = module[k] - if type(has) ~= "table" or not has.api and not Config.silent_warning then + if type(has) ~= "table" or not has.api then Utils.warn(k .. " is not a valid avante's API method", { once = true }) return end diff --git a/lua/avante/clipboard.lua b/lua/avante/clipboard.lua index 10064959a..2644a338b 100644 --- a/lua/avante/clipboard.lua +++ b/lua/avante/clipboard.lua @@ -36,7 +36,7 @@ end ---@param line? string M.paste_image = function(line) line = line or nil - if not Config.support_paste_image(true) then return false end + if not Config.support_paste_image() then return false end local opts = { dir_path = paste_directory:absolute(), diff --git a/lua/avante/config.lua b/lua/avante/config.lua index d7f61659e..ebbc31eab 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -7,7 +7,6 @@ local Utils = require("avante.utils") local M = {} ---@class avante.Config ----@field silent_warning? boolean will be determined from debug M.defaults = { debug = false, ---@alias Provider "claude" | "openai" | "azure" | "gemini" | "cohere" | "copilot" | [string] @@ -146,6 +145,7 @@ You are an excellent programming expert. default = "at", debug = "ad", hint = "ah", + suggestion = "as", }, }, windows = { @@ -197,14 +197,10 @@ function M.setup(opts) ---@type avante.Config { behaviour = { - support_paste_from_clipboard = M.support_paste_image(true), + support_paste_from_clipboard = M.support_paste_image(), }, } ) - if M.options.silent_warning == nil then - -- set silent_warning to true if debug is false - M.options.silent_warning = not M.options.debug - end M.providers = vim .iter(M.defaults) :filter(function(_, value) return type(value) == "table" and value.endpoint ~= nil end) @@ -237,11 +233,6 @@ function M.override(opts) vim.validate({ opts = { opts, "table", true } }) M.options = vim.tbl_deep_extend("force", M.options, opts or {}) - if not M.options.silent_warning then - -- set silent_warning to true if debug is false - M.options.silent_warning = not M.options.debug - end - M.diff = vim.tbl_deep_extend( "force", {}, @@ -264,13 +255,7 @@ M = setmetatable(M, { end, }) ----@param skip_warning? boolean -M.support_paste_image = function(skip_warning) - skip_warning = skip_warning or M.silent_warning - if skip_warning then return end - - return Utils.has("img-clip.nvim") or Utils.has("img-clip") -end +M.support_paste_image = function() return Utils.has("img-clip.nvim") or Utils.has("img-clip") end M.get_window_width = function() return math.ceil(vim.o.columns * (M.windows.width / 100)) end diff --git a/lua/avante/init.lua b/lua/avante/init.lua index 9f3004699..9a63c9c90 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -105,12 +105,19 @@ end H.keymaps = function() vim.keymap.set({ "n", "v" }, "(AvanteAsk)", function() require("avante.api").ask() end, { noremap = true }) + vim.keymap.set( + { "n", "v" }, + "(AvanteChat)", + function() require("avante.api").ask({ ask = false }) end, + { noremap = true } + ) vim.keymap.set("v", "(AvanteEdit)", function() require("avante.api").edit() end, { noremap = true }) vim.keymap.set("n", "(AvanteRefresh)", function() require("avante.api").refresh() end, { noremap = true }) vim.keymap.set("n", "(AvanteBuild)", function() require("avante.api").build() end, { noremap = true }) vim.keymap.set("n", "(AvanteToggle)", function() M.toggle() end, { noremap = true }) vim.keymap.set("n", "(AvanteToggleDebug)", function() M.toggle.debug() end) vim.keymap.set("n", "(AvanteToggleHint)", function() M.toggle.hint() end) + vim.keymap.set("n", "(AvanteToggleSuggestion)", function() M.toggle.suggestion() end) vim.keymap.set({ "n", "v" }, "(AvanteConflictOurs)", function() Diff.choose("ours") end) vim.keymap.set({ "n", "v" }, "(AvanteConflictBoth)", function() Diff.choose("both") end) @@ -152,6 +159,50 @@ H.keymaps = function() function() M.toggle.hint() end, { desc = "avante: toggle hint" } ) + Utils.safe_keymap_set( + "n", + Config.mappings.toggle.suggestion, + function() M.toggle.suggestion() end, + { desc = "avante: toggle suggestion" } + ) + end + + if Config.behaviour.auto_suggestion then + Utils.safe_keymap_set("i", Config.mappings.suggestion.accept, function() + local _, _, sg = M.get() + sg:accept() + end, { + desc = "avante: accept suggestion", + noremap = true, + silent = true, + }) + + Utils.safe_keymap_set("i", Config.mappings.suggestion.dismiss, function() + local _, _, sg = M.get() + if sg:is_visible() then sg:dismiss() end + end, { + desc = "avante: dismiss suggestion", + noremap = true, + silent = true, + }) + + Utils.safe_keymap_set("i", Config.mappings.suggestion.next, function() + local _, _, sg = M.get() + sg:next() + end, { + desc = "avante: next suggestion", + noremap = true, + silent = true, + }) + + Utils.safe_keymap_set("i", Config.mappings.suggestion.prev, function() + local _, _, sg = M.get() + sg:prev() + end, { + desc = "avante: previous suggestion", + noremap = true, + silent = true, + }) end end @@ -318,6 +369,21 @@ M.toggle.hint = H.api(Utils.toggle_wrap({ set = function(state) Config.override({ hints = { enabled = state } }) end, })) +M.toggle.suggestion = H.api(Utils.toggle_wrap({ + name = "suggestion", + get = function() return Config.behaviour.auto_suggestion end, + set = function(state) + Config.override({ behaviour = { auto_suggestion = state } }) + local _, _, sg = M.get() + if state ~= false then + if sg then sg:setup_autocmds() end + H.keymaps() + else + if sg then sg:delete_autocmds() end + end + end, +})) + setmetatable(M.toggle, { __index = M.toggle, __call = function() M.toggle_sidebar() end, diff --git a/lua/avante/suggestion.lua b/lua/avante/suggestion.lua index 4cd063fa4..035cf6b5b 100644 --- a/lua/avante/suggestion.lua +++ b/lua/avante/suggestion.lua @@ -32,7 +32,6 @@ function Suggestion:new(id) local o = { id = id, suggestions = {} } setmetatable(o, self) self.__index = self - self.augroup = api.nvim_create_augroup("avante_suggestion_" .. id, { clear = true }) self.extmark_id = 1 self._timer = nil self._contexts = {} @@ -41,7 +40,6 @@ function Suggestion:new(id) api.nvim_exec_autocmds("User", { pattern = Provider.env.REQUEST_LOGIN_PATTERN }) vim.g.avante_login = true end - self:setup_mappings() self:setup_autocmds() end return o @@ -51,44 +49,6 @@ function Suggestion:destroy() self:stop_timer() self:reset() self:delete_autocmds() - api.nvim_del_namespace(SUGGESTION_NS) -end - -function Suggestion:setup_mappings() - if not Config.behaviour.auto_set_keymaps then return end - if Config.mappings.suggestion and Config.mappings.suggestion.accept then - vim.keymap.set("i", Config.mappings.suggestion.accept, function() self:accept() end, { - desc = "[avante] accept suggestion", - noremap = true, - silent = true, - }) - end - - if Config.mappings.suggestion and Config.mappings.suggestion.dismiss then - vim.keymap.set("i", Config.mappings.suggestion.dismiss, function() - if self:is_visible() then self:dismiss() end - end, { - desc = "[avante] dismiss suggestion", - noremap = true, - silent = true, - }) - end - - if Config.mappings.suggestion and Config.mappings.suggestion.next then - vim.keymap.set("i", Config.mappings.suggestion.next, function() self:next() end, { - desc = "[avante] next suggestion", - noremap = true, - silent = true, - }) - end - - if Config.mappings.suggestion and Config.mappings.suggestion.prev then - vim.keymap.set("i", Config.mappings.suggestion.prev, function() self:prev() end, { - desc = "[avante] previous suggestion", - noremap = true, - silent = true, - }) - end end function Suggestion:suggest() @@ -307,6 +267,7 @@ function Suggestion:accept() end function Suggestion:setup_autocmds() + self.augroup = api.nvim_create_augroup("avante_suggestion_" .. self.id, { clear = true }) local last_cursor_pos = {} local check_for_suggestion = Utils.debounce(function() diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index f31e391b9..6a42b8422 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -317,8 +317,6 @@ end ---@param msg string|string[] ---@param opts? LazyNotifyOpts function M.warn(msg, opts) - if require("avante.config").silent_warning then return end - opts = opts or {} opts.level = vim.log.levels.WARN M.notify(msg, opts) diff --git a/plugin/avante.lua b/plugin/avante.lua index 9659b837f..86a4a6e05 100644 --- a/plugin/avante.lua +++ b/plugin/avante.lua @@ -13,7 +13,7 @@ end local Clipboard = require("avante.clipboard") local Config = require("avante.config") -if Config.support_paste_image(true) then +if Config.support_paste_image() then vim.paste = (function(overriden) ---@param lines string[] ---@param phase -1|1|2|3