diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index 375f9775..e8307f45 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -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 }) diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 2a2f69f3..cd92ac70 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -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