fix: do not need compatible with old nvim (#242)

This commit is contained in:
yetone 2024-08-26 20:35:36 +08:00 committed by GitHub
parent 26409d94b5
commit 1cb68e9aa2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 142 deletions

View File

@ -1,5 +1,3 @@
local keymap = require("avante.utils.keymap")
local api = vim.api
local namespace = api.nvim_create_namespace("avante_floating_window")
@ -240,15 +238,21 @@ end
---@param mode string|string[] check `:h :map-modes`
---@param lhs string|string[]
---@param handler string | fun(): nil handler for the mapping
---@param handler string|fun(): nil handler for the mapping
---@param opts? vim.keymap.set.Opts
---@return nil
function FloatingWindow:map(mode, lhs, handler, opts)
if not self.bufnr then
error("floating buffer not found.")
end
local options = opts or {}
keymap.set(self.bufnr, mode, lhs, handler, options)
local options = vim.deepcopy(opts or {})
options.buffer = self.bufnr
if type(lhs) ~= "table" then
lhs = { lhs }
end
for _, lhs_ in ipairs(lhs) do
vim.keymap.set(mode, lhs_, handler, options)
end
end
return FloatingWindow

View File

@ -1,34 +0,0 @@
-- Copy from: https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/utils/buf_storage.lua
local utils = require("avante.utils")
local buf_storage = {
_registry = {},
}
---@param storage_name string
---@param default_value any
---@return table<number, any>
function buf_storage.create(storage_name, default_value)
local storage = setmetatable({}, {
__index = function(tbl, bufnr)
rawset(tbl, bufnr, vim.deepcopy(utils.fallback(default_value, {})))
-- TODO: can `buf_storage.cleanup` be automatically (and reliably) triggered on `BufWipeout`?
return tbl[bufnr]
end,
})
buf_storage._registry[storage_name] = storage
return storage
end
---@param bufnr number
function buf_storage.cleanup(bufnr)
for _, storage in pairs(buf_storage._registry) do
rawset(storage, bufnr, nil)
end
end
return buf_storage

View File

@ -1,103 +0,0 @@
-- Extracted from: https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/utils/keymap.lua
local buf_storage = require("avante.utils.buf_storage")
local utils = require("avante.utils")
local api = vim.api
local keymap = {
storage = buf_storage.create("avante.utils.keymap", { _next_handler_id = 1, keys = {}, handlers = {} }),
}
---@param mode string
---@param key string
---@return string key_id
local function get_key_id(mode, key)
return string.format("%s---%s", mode, vim.api.nvim_replace_termcodes(key, true, true, true))
end
---@param bufnr number
---@param key_id string
---@return integer|nil handler_id
local function get_handler_id(bufnr, key_id)
return keymap.storage[bufnr].keys[key_id]
end
---@param bufnr number
---@param mode string
---@param key string
---@param handler string|fun(): nil
---@return { rhs: string, callback?: fun(): nil }|nil
local function get_keymap_info(bufnr, mode, key, handler, overwrite)
local key_id = get_key_id(mode, key)
-- luacov: disable
if get_handler_id(bufnr, key_id) and not overwrite then
return nil
end
-- luacov: enable
local rhs, callback = "", nil
if type(handler) == "function" then
callback = handler
else
rhs = handler
end
return {
rhs = rhs,
callback = callback,
}
end
---@param bufnr number
---@param mode string|string[]
---@param lhs string|string[]
---@param handler string|fun(): nil
---@param opts? vim.keymap.set.Opts
---@return nil
function keymap.set(bufnr, mode, lhs, handler, opts, force)
if not utils.is_type("boolean", force) then
force = true
end
local keys = lhs
if type(lhs) ~= "table" then
keys = { lhs }
end
---@cast keys -string
opts = opts or {}
if not utils.is_type("nil", opts.remap) then
opts.noremap = not opts.remap
opts.remap = nil
end
local modes = {}
if type(mode) == "string" then
modes = { mode }
else
modes = mode
end
for _, key in ipairs(keys) do
for _, mode_ in ipairs(modes) do
local keymap_info = get_keymap_info(bufnr, mode_, key, handler, force)
-- luacov: disable
if not keymap_info then
return false
end
-- luacov: enable
local options = vim.deepcopy(opts)
options.callback = keymap_info.callback
api.nvim_buf_set_keymap(bufnr, mode_, key, keymap_info.rhs, options)
end
end
return true
end
return keymap