Skip to content

Commit

Permalink
feat(keymap): Adding user overrides for keymap
Browse files Browse the repository at this point in the history
  • Loading branch information
misumisumi committed Aug 1, 2023
1 parent af21c61 commit b127042
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions lua/core/mapping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ local core_map = {
["v|>"] = map_cmd(">gv"):with_desc("edit: Increase indent"),
}

core_map = bind.override_mapping("core", core_map)
bind.nvim_load_mapping(core_map)
18 changes: 18 additions & 0 deletions lua/keymap/bind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ function bind.escape_termcode(cmd_string)
return vim.api.nvim_replace_termcodes(cmd_string, true, true, true)
end

---@param orig table
function bind.override_mapping(name, orig)
local ok, custom = pcall(require, "user.keymap." .. name)
if ok then
if type(custom) == "table" then
for k, v in pairs(custom) do
if v == "" then
orig[k], custom[k] = nil, nil
end
end
orig = vim.tbl_extend("force", orig, custom)
elseif type(custom) == "function" then
orig = custom()
end
end
return orig
end

---@param mapping table<string, map_rhs>
function bind.nvim_load_mapping(mapping)
for key, value in pairs(mapping) do
Expand Down
11 changes: 5 additions & 6 deletions lua/keymap/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ local bind = require("keymap.bind")
local map_cr = bind.map_cr
local map_cmd = bind.map_cmd
local map_callback = bind.map_callback
local M = {}

local plug_map = {
M["plug_map"] = {
["n|<A-f>"] = map_cmd("<Cmd>FormatToggle<CR>"):with_noremap():with_desc("Formater: Toggle format on save"),
}
bind.nvim_load_mapping(plug_map)

local mapping = {}

function mapping.lsp(buf)
M["lsp"] = function(buf)
local map = {
-- LSP-related keymaps, work only when event = { "InsertEnter", "LspStart" }
["n|<leader>li"] = map_cr("LspInfo"):with_buffer(buf):with_desc("lsp: Info"),
Expand All @@ -34,7 +32,8 @@ function mapping.lsp(buf)
["n|<leader>ci"] = map_cr("Lspsaga incoming_calls"):with_buffer(buf):with_desc("lsp: Show incoming calls"),
["n|<leader>co"] = map_cr("Lspsaga outgoing_calls"):with_buffer(buf):with_desc("lsp: Show outgoing calls"),
}
map = bind.override_mapping("lsp", map)
bind.nvim_load_mapping(map)
end

return mapping
return M
2 changes: 1 addition & 1 deletion lua/keymap/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ local plug_map = {
["n|<A-s>"] = map_cu("SudaWrite"):with_silent():with_noremap():with_desc("editn: Save file using sudo"),
}

bind.nvim_load_mapping(plug_map)
return plug_map
18 changes: 11 additions & 7 deletions lua/keymap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ local plug_map = {
["n|<leader>px"] = map_cr("Lazy clean"):with_silent():with_noremap():with_nowait():with_desc("package: Clean"),
}

bind.nvim_load_mapping(plug_map)

-- Plugin keymaps
require("keymap.completion")
require("keymap.editor")
require("keymap.lang")
require("keymap.tool")
require("keymap.ui")
plug_map = vim.tbl_deep_extend(
"force",
plug_map,
require("keymap.completion").plug_map,
require("keymap.editor"),
require("keymap.lang"),
require("keymap.tool"),
require("keymap.ui").plug_map
)
plug_map = bind.override_mapping("init", plug_map)
bind.nvim_load_mapping(plug_map)
2 changes: 1 addition & 1 deletion lua/keymap/lang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ local plug_map = {
["n|<F12>"] = map_cr("MarkdownPreviewToggle"):with_noremap():with_silent():with_desc("tool: Preview markdown"),
}

bind.nvim_load_mapping(plug_map)
return plug_map
2 changes: 1 addition & 1 deletion lua/keymap/tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@ local plug_map = {
:with_desc("debug: Open REPL"),
}

bind.nvim_load_mapping(plug_map)
return plug_map
12 changes: 5 additions & 7 deletions lua/keymap/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ local map_cr = bind.map_cr
-- local map_cu = bind.map_cu
-- local map_cmd = bind.map_cmd
-- local map_callback = bind.map_callback
local M = {}

local plug_map = {
M["plug_map"] = {
-- Plugin: bufferline
["n|<A-j>"] = map_cr("BufferLineCycleNext"):with_noremap():with_silent():with_desc("buffer: Switch to next"),
["n|<A-k>"] = map_cr("BufferLineCyclePrev"):with_noremap():with_silent():with_desc("buffer: Switch to prev"),
Expand All @@ -23,11 +24,7 @@ local plug_map = {
["n|<A-9>"] = map_cr("BufferLineGoToBuffer 9"):with_noremap():with_silent():with_desc("buffer: Goto buffer 9"),
}

bind.nvim_load_mapping(plug_map)

local mapping = {}

function mapping.gitsigns(buf)
M["gitsigns"] = function(buf)
local actions = require("gitsigns.actions")
local map = {
["n|]g"] = bind.map_callback(function()
Expand Down Expand Up @@ -99,7 +96,8 @@ function mapping.gitsigns(buf)
actions.text_object()
end):with_buffer(buf),
}
map = bind.override_mapping("gitsings", map)
bind.nvim_load_mapping(map)
end

return mapping
return M

0 comments on commit b127042

Please sign in to comment.