refactor(keymaps): move safe keymap.set wrapper to util and add message not to use it in a personal config

This commit is contained in:
Folke Lemaitre
2023-10-08 10:27:59 +02:00
parent 0cdc23a51d
commit 25f3587f3f
2 changed files with 26 additions and 19 deletions

View File

@ -1,25 +1,9 @@
-- This file is automatically loaded by lazyvim.config.init
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 #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(modes, lhs, rhs, opts)
end
end
-- DO NOT USE THIS IN YOU OWN CONFIG!!
-- use `vim.keymap.set` instead
local map = Util.safe_keymap_set
-- better up/down
map({ "n", "x" }, "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })

View File

@ -320,4 +320,27 @@ function M.on_rename(from, to)
end
end
-- Wrapper around vim.keymap.set that will
-- not create a keymap if a lazy key handler exists.
-- It will also set `silent` to true by default.
function M.safe_keymap_set(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 #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(modes, lhs, rhs, opts)
end
end
return M