feat(keymaps): added support for lazy's per-mode keymap disabling

This commit is contained in:
Folke Lemaitre 2023-10-08 10:15:26 +02:00
parent af9e452854
commit 1bc78272da
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 68 additions and 53 deletions

View file

@ -1,7 +1,7 @@
---@class LazyVimConfig: LazyVimOptions
local M = {}
M.lazy_version = ">=9.1.0"
M.lazy_version = ">=10.8.0"
M.use_lazy_file = true
M.lazy_file_events = { "BufReadPost", "BufNewFile" }
@ -92,33 +92,50 @@ M.renames = {
---@type LazyVimOptions
local options
---@param lines {[1]:string, [2]:string}[]
function M.msg(lines)
vim.cmd([[clear]])
vim.api.nvim_echo(lines, true, {})
vim.fn.getchar()
end
---@param opts? LazyVimOptions
function M.setup(opts)
options = vim.tbl_deep_extend("force", defaults, opts or {}) or {}
if vim.fn.has("nvim-0.9.0") == 0 then
vim.api.nvim_echo({
M.msg({
{
"LazyVim requires Neovim >= 0.9.0\n",
"ErrorMsg",
},
{ "Press any key to exit", "MoreMsg" },
}, true, {})
vim.fn.getchar()
})
vim.cmd([[quit]])
return
end
if not M.has() then
require("lazy.core.util").error(
"**LazyVim** needs **lazy.nvim** version "
.. M.lazy_version
.. " to work properly.\n"
.. "Please upgrade **lazy.nvim**",
{ title = "LazyVim" }
)
error("Exiting")
M.msg({
{
"LazyVim requires lazy.nvim " .. M.lazy_version .. "\n",
"WarningMsg",
},
{ "Press any key to attempt an upgrade", "MoreMsg" },
})
vim.api.nvim_create_autocmd("User", {
pattern = "LazyVimStarted",
callback = function()
require("lazy").update({ plugins = { "lazy.nvim" }, wait = true })
M.msg({
{
"**lazy.nvim** has been upgraded.\nPlease restart **Neovim**",
"WarningMsg",
},
})
end,
})
end
-- autocmds can be loaded lazily when not opening a file

View file

@ -4,14 +4,20 @@ local Util = require("lazyvim.util")
local function map(mode, lhs, rhs, opts)
local keys = require("lazy.core.handler").handlers.keys
---@cast keys LazyKeysHandler
local modes = type(mode) == "string" and { mode } or mode
modes = vim.tbl_filter(function(mode)
return not (keys.have and keys:have(lhs, mode))
end, modes)
-- do not create the keymap if a lazy keys handler exists
if not keys.active[keys.parse({ lhs, mode = mode }).id] then
if #modes > 0 then
opts = opts or {}
opts.silent = opts.silent ~= false
if opts.remap and not vim.g.vscode then
opts.remap = nil
end
vim.keymap.set(mode, lhs, rhs, opts)
vim.keymap.set(modes, lhs, rhs, opts)
end
end

View file

@ -55,7 +55,7 @@ return {
-- mason = false, -- set to false if you don't want this server to be installed with mason
-- Use this to add any additional keymaps
-- for specific lsp servers
---@type LazyKeys[]
---@type LazyKeysSpec[]
-- keys = {},
settings = {
Lua = {

View file

@ -1,12 +1,16 @@
local M = {}
---@type PluginLspKeys
---@type LazyKeysLspSpec[]|nil
M._keys = nil
---@return (LazyKeys|{has?:string})[]
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string}
---@alias LazyKeysLsp LazyKeys|{has?:string}
---@return LazyKeysLspSpec[]
function M.get()
if not M._keys then
---@class PluginLspKeys
if M._keys then
return M._keys
end
-- stylua: ignore
M._keys = {
{ "<leader>cd", vim.diagnostic.open_float, desc = "Line Diagnostics" },
@ -56,7 +60,6 @@ function M.get()
else
M._keys[#M._keys + 1] = { "<leader>cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" }
end
end
return M._keys
end
@ -72,34 +75,23 @@ function M.has(buffer, method)
return false
end
---@return (LazyKeys|{has?:string})[]
function M.resolve(buffer)
local Keys = require("lazy.core.handler.keys")
local keymaps = {} ---@type table<string,LazyKeys|{has?:string}>
local function add(keymap)
local keys = Keys.parse(keymap)
if keys[2] == false then
keymaps[keys.id] = nil
else
keymaps[keys.id] = keys
if not Keys.resolve then
return {}
end
end
for _, keymap in ipairs(M.get()) do
add(keymap)
end
local spec = M.get()
local opts = require("lazyvim.util").opts("nvim-lspconfig")
local clients = vim.lsp.get_active_clients({ bufnr = buffer })
for _, client in ipairs(clients) do
local maps = opts.servers[client.name] and opts.servers[client.name].keys or {}
for _, keymap in ipairs(maps) do
add(keymap)
vim.list_extend(spec, maps)
end
end
return keymaps
return Keys.resolve(spec)
end
function M.on_attach(client, buffer)
function M.on_attach(_, buffer)
local Keys = require("lazy.core.handler.keys")
local keymaps = M.resolve(buffer)
@ -110,7 +102,7 @@ function M.on_attach(client, buffer)
opts.has = nil
opts.silent = opts.silent ~= false
opts.buffer = buffer
vim.keymap.set(keys.mode or "n", keys[1], keys[2], opts)
vim.keymap.set(keys.mode or "n", keys.lhs, keys.rhs, opts)
end
end
end