jbyuki / one-small-step-for-vimkind

Debug adapter for Neovim plugins
MIT License
409 stars 11 forks source link

How to debug key mapping or auto commands #28

Closed yyy33 closed 1 year ago

yyy33 commented 1 year ago

Hi, I'm trying to debug some plugins I've written, which inevitably use some key mapping and automatic commands, but I can't debug it.For example, here is the file I want to debug, I hit the breakpoint to the third line, when I run lua require'dap'.continue() and then run lua require'dap'.step_into(), I get the message: No stopped threads. Cannot move

vim.keymap.set('n', '<BS>',
    function()
        print("start")
        for i=1,10 do
            print(i)
        end
        print("end")
    end 
)

This is my dap config

local ok, dap = pcall(require, 'dap')
if not ok then
    vim.notify('load module dap faild')
    return
end

local nvim_server
local nvim_chanID

local function dap_server(opts)

    assert(dap.adapters.nlua,
        "nvim-dap adapter configuration for nlua not found. "..
        "Please refer to the README.md or :help osv.txt")

    if nvim_chanID then
        local pid = vim.fn.jobpid(nvim_chanID)
        vim.fn.rpcnotify(nvim_chanID, "nvim_exec_lua", [[return require"osv".stop()]])
        vim.fn.jobstop(nvim_chanID)
        if type(vim.loop.os_getpriority(pid)) == 'number' then
            vim.loop.kill(pid, 9)
        end
        nvim_chanID = nil
    end

    nvim_chanID = vim.fn.jobstart({vim.v.progpath, '--embed', '--headless'}, {rpc = true})
    assert(nvim_chanID, "Could not create neovim instance with jobstart!")

    local mode = vim.fn.rpcrequest(nvim_chanID, "nvim_get_mode")
    assert(not mode.blocking, "Neovim is waiting for input at startup. Aborting.")

    vim.fn.rpcrequest(nvim_chanID, "nvim_command", "packadd one-small-step-for-vimkind")

    nvim_server = vim.fn.rpcrequest(nvim_chanID,
        "nvim_exec_lua",
        [[return require"osv".launch(...)]],
        { opts })

    vim.wait(100)

    return nvim_server
end

dap.adapters.nlua = function(callback, config)
    if not config.port then
        local server = dap_server()
        config.host = server.host
        config.port = server.port
    end
    callback({ type = 'server', host = config.host, port = config.port })
    if type(config.post) == 'function' then
        config.post()
    end
end

dap.configurations.lua = {
    {
        type = "nlua",
        request = "attach",
        name = "debug current file",
        post = function()
            dap.listeners.after['setBreakpoints']['osv'] = function(session, body)
                assert(nvim_chanID, "Fatal: neovim RPC channel is nil!")
                vim.fn.rpcnotify(nvim_chanID, "nvim_command", "luafile " .. vim.fn.expand("%:p"))
                dap.listeners.after['setBreakpoints']['osv'] = nil
            end
        end
    },
    {
        type = 'nlua',
        request = 'attach',
        name = "attach to nvim",
        host = function()
            local value = vim.fn.input('Host [127.0.0.1]: ')
            if value ~= "" then
                return value
            end
            return '127.0.0.1'
        end,
        port = function()
            local val = tonumber(vim.fn.input('Port: '))
            assert(val, "Please provide a port number")
            return val
        end,
    }
}
jbyuki commented 1 year ago

Just lua require'dap'.step_into() or lua require'dap'.step_over() instead of lua require'dap'.continue() and lua require'dap'.step_into()?

yyy33 commented 1 year ago

I tried just executing lua require'dap'.step_into() and nothing happened, don't we need to use lua require'dap'.continue() to start the dap server?

jbyuki commented 1 year ago

As a side note, I suggest using the new configuration that is available on the README, it avoids putting the port number everytime.

It looks like the function dap_server() is starting an embbeded nvim to launch the server which is itself running a neovim instance. It's probably better to use the standard method explained in the README, help doc or here. Let me know if it still doesn't work because it should.

yyy33 commented 1 year ago

As a side note, I suggest using the new configuration that is available on the README, it avoids putting the port number everytime.

It looks like the function dap_server() is starting an embbeded nvim to launch the server which is itself running a neovim instance. It's probably better to use the standard method explained in the README, help doc or here. Let me know if it still doesn't work because it should.

Thanks for the tip, it turns out that the quiet mapping is set to the embedded nvim, I can now debug normally after using the tutorial in the readme

jbyuki commented 1 year ago

Good, have fun debugging with osv 😄