From 2e19c266588ef9b6f8c4e13f47f915f1da533a49 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Fri, 31 May 2024 11:00:39 -0600 Subject: [PATCH] fix(api): `maps.nop` behaviour improved. --- init.lua | 5 +--- lua/user/maps.lua | 46 ++++++++++++++++++++++++++++++++++-- lua/user/types/user/maps.lua | 2 +- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/init.lua b/init.lua index 42a935ed..b88683f9 100644 --- a/init.lua +++ b/init.lua @@ -20,11 +20,8 @@ local desc = Kmap.desc _G.is_windows = vim_has('win32') --- WARNING: DO NOT CHANGE ANYTHING BUT THE FIRST PARAMETER --- Keymaps will break for yet unknown reasons. --- -- Set `` as Leader Key. -nop('', User.maps.map.desc('Leader Key', true, true)) +nop('', desc('Leader Key', true, nil, true)) vim.g.mapleader = ' ' vim.g.maplocalleader = ' ' diff --git a/lua/user/maps.lua b/lua/user/maps.lua index 183def9c..dd4ec317 100644 --- a/lua/user/maps.lua +++ b/lua/user/maps.lua @@ -23,6 +23,44 @@ local bufmap = vim.api.nvim_buf_set_keymap ---@type Modes local MODES = { 'n', 'i', 'v', 't', 'o', 'x' } +---@type fun(T: UserMaps.Api.Opts|UserMaps.Keymap.Opts|UserMaps.Buf.Opts, fields: string|string[]): UserMaps.Api.Opts|UserMaps.Keymap.Opts|UserMaps.Buf.Opts +local strip_options = function(T, fields) + if not is_tbl(T) then + error('(maps:strip_options): Empty table') + end + + if empty(T) then + return T + end + + if not (is_str(fields) or is_tbl(fields)) or empty(fields) then + return T + end + + ---@type UserMaps.Keymap.Opts + local res = {} + + if is_str(fields) then + if not field(fields, T) then + return T + end + + for k, v in next, T do + if k ~= fields then + res[k] = v + end + end + else + for k, v in next, T do + if not vim.tbl_contains(fields, k) then + res[k] = v + end + end + end + + return res +end + ---@type fun(mode: string, func: MapFuncs, with_buf: boolean?): KeyMapFunction|ApiMapFunction|BufMapFunction local function variant(mode, func, with_buf) if not (is_fun(func) and is_str(mode) and vim.tbl_contains(MODES, mode)) then @@ -138,11 +176,15 @@ function M.nop(T, opts, mode) opts.silent = is_bool(opts.silent) and opts.silent or true + if is_int(opts.buffer) then + opts = strip_options(vim.deepcopy(opts), 'buffer') + end + if is_str(T) then - M.map[mode](T, '', opts) + M.kmap[mode](T, '', opts) else for _, v in next, T do - M.map[mode](v, '', opts) + M.kmap[mode](v, '', opts) end end end diff --git a/lua/user/types/user/maps.lua b/lua/user/types/user/maps.lua index a28ca956..40248d7f 100644 --- a/lua/user/types/user/maps.lua +++ b/lua/user/types/user/maps.lua @@ -119,6 +119,6 @@ require('user.types.which_key') ---@field kmap UserMaps.Keymap ---@field map UserMaps.Api ---@field buf_map UserMaps.Buf ----@field nop fun(T: string|string[], opts: UserMaps.Api.Opts?, mode: MapModes?) +---@field nop fun(T: string|string[], opts: UserMaps.Keymap.Opts?, mode: MapModes?) ---@field wk UserMaps.WK ---@field modes Modes