Skip to content

Commit

Permalink
fix(dap-keymap): properly create and destroy keymaps (#902)
Browse files Browse the repository at this point in the history
* fix(dap-keymap): properly create and destroy keymaps

* fixup! fix(dap-keymap): properly create and destroy keymaps
  • Loading branch information
Jint-lzxy authored Jul 28, 2023
1 parent 1790384 commit 430375e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
- uses: actions/checkout@v3
- uses: lunarmodules/luacheck@v1
with:
args: . --std luajit --globals vim _toggle_lazygit _command_panel --max-line-length 150 --no-config
args: . --std luajit --globals vim _toggle_lazygit _command_panel _lspkeymap_loaded_bufnr --max-line-length 150 --no-config
8 changes: 6 additions & 2 deletions lua/core/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ function autocmd.nvim_create_augroups(definitions)
end

local mapping = require("keymap.completion")
_G._lspkeymap_loaded_bufnr = {}
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
group = vim.api.nvim_create_augroup("LspKeymapLoader", { clear = true }),
callback = function(event)
mapping.lsp(event.buf)
if not _lspkeymap_loaded_bufnr[event.buf] then
mapping.lsp(event.buf)
_lspkeymap_loaded_bufnr[event.buf] = true
end
end,
})

Expand Down
49 changes: 47 additions & 2 deletions lua/modules/configs/tool/dap/dap-keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ local map_cr = bind.map_cr
local map_cmd = bind.map_cmd

local did_load_debug_mappings = false
local keymap_info_debug = {
n = { K = false },
v = { K = false },
}
local keymap_info_original = {
n = { K = true },
v = { K = false },
}
local debug_keymap = {
["nv|K"] = map_cmd("<Cmd>lua require('dapui').eval()<CR>")
:with_noremap()
Expand All @@ -16,16 +24,53 @@ local original_keymap = {
["v|K"] = map_cmd(":m '<-2<CR>gv=gv"),
}

local function del_keymap(mappings, keymap_info)
for key in pairs(mappings) do
local modes, keymap = key:match("([^|]*)|?(.*)")
for _, mode in ipairs(vim.split(modes, "")) do
if vim.fn.maparg(keymap, mode, false) ~= "" then
if keymap_info[mode][keymap] == true then
vim.api.nvim_buf_del_keymap(0, mode, keymap)
else
vim.api.nvim_del_keymap(mode, keymap)
end
end
end
end
end

local function load_keymap(mappings, keymap_info)
for key, value in pairs(mappings) do
local modes, keymap = key:match("([^|]*)|?(.*)")
if type(value) == "table" then
for _, mode in ipairs(vim.split(modes, "")) do
local rhs = value.cmd
local options = value.options
if keymap_info[mode][keymap] == true then
for buf in pairs(_G._lspkeymap_loaded_bufnr) do
-- Restore lsp keymaps
vim.api.nvim_buf_set_keymap(buf, mode, keymap, rhs, options)
end
else
vim.api.nvim_set_keymap(mode, keymap, rhs, options)
end
end
end
end
end

function M.load()
if not did_load_debug_mappings then
bind.nvim_load_mapping(debug_keymap)
del_keymap(original_keymap, keymap_info_original)
load_keymap(debug_keymap, keymap_info_debug)
did_load_debug_mappings = true
end
end

function M.restore()
if did_load_debug_mappings then
bind.nvim_load_mapping(original_keymap)
del_keymap(debug_keymap, keymap_info_debug)
load_keymap(original_keymap, keymap_info_original)
did_load_debug_mappings = false
end
end
Expand Down

0 comments on commit 430375e

Please sign in to comment.