nvim dap 映射 r 快捷键

最近在研究如何使用 nvim-dap 进行 debugging。在 gdb,我们可以很方便的使用单个按键就可以触发一些行为,如

  • r: run
  • c: continue
  • s: step-in

那么,我的想法也是在 nvim-dap 实现这样快捷键 r 来模拟 run 的行为。现在的问题是:由于在 normal mode,单个按键 r 代表 replace one character

因此,我们需要在 nvimbuffers 去识别是否启动了 nvim-dap

  • 如果存在 dap-repl,则映射 r
  • 如果找不到,则回退到 replace 的功能。

按照以上的思路,我们可以写一个映射函数,用于识别当前打开的 buffers 是否能找到 dap-repl 即可。以下便是这个快捷键映射的实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
return {
    "mfussenegger/nvim-dap",
    keys = {
        { "r",
            function()
                -- Check if any buffer has dap-repl filetype
                local has_repl = false
                for _, buf in ipairs(vim.api.nvim_list_bufs()) do
                    local ft = vim.bo[buf].filetype
                    if ft:find('^dapui') or ft == 'dap-repl' then
                        has_repl = true
                        break
                    end
                end

                if has_repl then
                    return require("dap").continue()
                end

                return "r"  -- fallback to normal 'r' behavior
            end, desc = "Run/Continue", mode = {"n"}, expr = true
        },
    }
}

nvim-dap-repl
nvim-dap-repl

相关内容

william 支付宝支付宝
william 微信微信
0%