Skip to content

Commit

Permalink
chore(mapping): add support for toggling suggestion (yetone#546)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm authored Sep 6, 2024
1 parent 33c9ac2 commit 7015dde
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lua/avante/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/avante/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
21 changes: 3 additions & 18 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -146,6 +145,7 @@ You are an excellent programming expert.
default = "<leader>at",
debug = "<leader>ad",
hint = "<leader>ah",
suggestion = "<leader>as",
},
},
windows = {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
{},
Expand All @@ -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

Expand Down
66 changes: 66 additions & 0 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@ end

H.keymaps = function()
vim.keymap.set({ "n", "v" }, "<Plug>(AvanteAsk)", function() require("avante.api").ask() end, { noremap = true })
vim.keymap.set(
{ "n", "v" },
"<Plug>(AvanteChat)",
function() require("avante.api").ask({ ask = false }) end,
{ noremap = true }
)
vim.keymap.set("v", "<Plug>(AvanteEdit)", function() require("avante.api").edit() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteRefresh)", function() require("avante.api").refresh() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteBuild)", function() require("avante.api").build() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteToggle)", function() M.toggle() end, { noremap = true })
vim.keymap.set("n", "<Plug>(AvanteToggleDebug)", function() M.toggle.debug() end)
vim.keymap.set("n", "<Plug>(AvanteToggleHint)", function() M.toggle.hint() end)
vim.keymap.set("n", "<Plug>(AvanteToggleSuggestion)", function() M.toggle.suggestion() end)

vim.keymap.set({ "n", "v" }, "<Plug>(AvanteConflictOurs)", function() Diff.choose("ours") end)
vim.keymap.set({ "n", "v" }, "<Plug>(AvanteConflictBoth)", function() Diff.choose("both") end)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
41 changes: 1 addition & 40 deletions lua/avante/suggestion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugin/avante.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7015dde

Please sign in to comment.