mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-23 17:28:57 +02:00
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:
parent
0cdc23a51d
commit
25f3587f3f
2 changed files with 26 additions and 19 deletions
|
@ -1,25 +1,9 @@
|
||||||
-- This file is automatically loaded by lazyvim.config.init
|
-- This file is automatically loaded by lazyvim.config.init
|
||||||
local Util = require("lazyvim.util")
|
local Util = require("lazyvim.util")
|
||||||
|
|
||||||
local function map(mode, lhs, rhs, opts)
|
-- DO NOT USE THIS IN YOU OWN CONFIG!!
|
||||||
local keys = require("lazy.core.handler").handlers.keys
|
-- use `vim.keymap.set` instead
|
||||||
---@cast keys LazyKeysHandler
|
local map = Util.safe_keymap_set
|
||||||
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
|
|
||||||
|
|
||||||
-- better up/down
|
-- better up/down
|
||||||
map({ "n", "x" }, "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
|
map({ "n", "x" }, "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
|
||||||
|
|
|
@ -320,4 +320,27 @@ function M.on_rename(from, to)
|
||||||
end
|
end
|
||||||
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
|
return M
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue