Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling automatic keymaps #341

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,12 @@ H.commands = function()
end

H.keymaps = function()
vim.keymap.set({ "n", "v" }, Config.mappings.ask, M.toggle, { noremap = true, desc = "avante: Ask" })
vim.keymap.set("v", Config.mappings.edit, M.edit, { noremap = true, desc = "avante: Edit" })
vim.keymap.set("n", Config.mappings.refresh, M.refresh, { noremap = true, desc = "avante: Refresh" })

Utils.toggle_map("n", Config.mappings.toggle.debug, {
name = "debug",
get = function()
return Config.debug
end,
set = function(state)
Config.override({ debug = state })
end,
})
Utils.toggle_map("n", Config.mappings.toggle.hint, {
name = "hint",
get = function()
return Config.hints.enabled
end,
set = function(state)
Config.override({ hints = { enabled = state } })
end,
})
Utils.maybe_set_keymap({ "n", "v" }, Config.mappings.ask, M.toggle, { noremap = true, desc = "avante: Ask" })
Utils.maybe_set_keymap("v", Config.mappings.edit, M.edit, { noremap = true, desc = "avante: Edit" })
Utils.maybe_set_keymap("n", Config.mappings.refresh, M.refresh, { noremap = true, desc = "avante: Refresh" })

Utils.maybe_set_keymap("n", Config.mappings.toggle.debug, M.toggle_debug, { desc = "toggle(avante): debug" })
Utils.maybe_set_keymap("n", Config.mappings.toggle.hint, M.toggle_hint, { desc = "toggle(avante): hint" })
end

H.signs = function()
Expand Down Expand Up @@ -166,6 +150,26 @@ function M._init(id)
return M
end

M.toggle_debug = Utils.toggle({
name = "debug",
get = function()
return Config.debug
end,
set = function(state)
Config.override({ debug = state })
end,
})

M.toggle_hint = Utils.toggle({
name = "hint",
get = function()
return Config.hints.enabled
end,
set = function(state)
Config.override({ hints = { enabled = state } })
end,
})

M.toggle = function()
local sidebar, _ = M._get()
if not sidebar then
Expand Down
21 changes: 15 additions & 6 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ end

---@alias _ToggleSet fun(state: boolean): nil
---@alias _ToggleGet fun(): boolean
---
---@param lhs string

---@param toggle {name: string, set: _ToggleSet, get: _ToggleGet}
---@param mode? string | string[]
M.toggle_map = function(mode, lhs, toggle)
vim.keymap.set(mode or { "n" }, lhs, function()
---@return fun(): boolean
M.toggle = function(toggle)
return function()
toggle.set(not toggle.get())
local state = toggle.get()
if state then
Expand All @@ -85,7 +84,17 @@ M.toggle_map = function(mode, lhs, toggle)
M.warn("disabled: " .. toggle.name, { title = "Avante" })
end
return state
end, { desc = "toggle(avante): " .. toggle.name })
end
end

---@param mode string|string[]
---@param lhs string
---@param rhs string|function
---@param opts? vim.keymap.set.Opts
M.maybe_set_keymap = function(mode, lhs, rhs, opts)
if lhs then
vim.keymap.set(mode, lhs, rhs, opts)
end
end

---@param str string
Expand Down