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 ---@class LazyVimConfig: LazyVimOptions
local M = {} local M = {}
M.lazy_version = ">=9.1.0" M.lazy_version = ">=10.8.0"
M.use_lazy_file = true M.use_lazy_file = true
M.lazy_file_events = { "BufReadPost", "BufNewFile" } M.lazy_file_events = { "BufReadPost", "BufNewFile" }
@ -92,33 +92,50 @@ M.renames = {
---@type LazyVimOptions ---@type LazyVimOptions
local options 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 ---@param opts? LazyVimOptions
function M.setup(opts) function M.setup(opts)
options = vim.tbl_deep_extend("force", defaults, opts or {}) or {} options = vim.tbl_deep_extend("force", defaults, opts or {}) or {}
if vim.fn.has("nvim-0.9.0") == 0 then if vim.fn.has("nvim-0.9.0") == 0 then
vim.api.nvim_echo({ M.msg({
{ {
"LazyVim requires Neovim >= 0.9.0\n", "LazyVim requires Neovim >= 0.9.0\n",
"ErrorMsg", "ErrorMsg",
}, },
{ "Press any key to exit", "MoreMsg" }, { "Press any key to exit", "MoreMsg" },
}, true, {}) })
vim.fn.getchar()
vim.cmd([[quit]]) vim.cmd([[quit]])
return return
end end
if not M.has() then if not M.has() then
require("lazy.core.util").error( M.msg({
"**LazyVim** needs **lazy.nvim** version " {
.. M.lazy_version "LazyVim requires lazy.nvim " .. M.lazy_version .. "\n",
.. " to work properly.\n" "WarningMsg",
.. "Please upgrade **lazy.nvim**", },
{ title = "LazyVim" } { "Press any key to attempt an upgrade", "MoreMsg" },
) })
error("Exiting")
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 end
-- autocmds can be loaded lazily when not opening a file -- 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 function map(mode, lhs, rhs, opts)
local keys = require("lazy.core.handler").handlers.keys local keys = require("lazy.core.handler").handlers.keys
---@cast keys LazyKeysHandler ---@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 -- 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 = opts or {}
opts.silent = opts.silent ~= false opts.silent = opts.silent ~= false
if opts.remap and not vim.g.vscode then if opts.remap and not vim.g.vscode then
opts.remap = nil opts.remap = nil
end end
vim.keymap.set(mode, lhs, rhs, opts) vim.keymap.set(modes, lhs, rhs, opts)
end end
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 -- mason = false, -- set to false if you don't want this server to be installed with mason
-- Use this to add any additional keymaps -- Use this to add any additional keymaps
-- for specific lsp servers -- for specific lsp servers
---@type LazyKeys[] ---@type LazyKeysSpec[]
-- keys = {}, -- keys = {},
settings = { settings = {
Lua = { Lua = {

View file

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