在上两篇中NeoVim开发环境配置NeoVim配置Go开发环境(进阶), 完成了将NeoVim作为IDE最基本常用功能的,这里将进一步完成对Python、Golang调试环境的配置。

在NeoVim上进行调试(Python、Golang)有nvim-dapvimspector可做选择,这里 将使用vimspector进行调试工作。

插件安装

在plug.vim中添加:

1
Plug 'puremourning/vimspector'

:PlugInstall安装插件。 在init.vim中,添加:

1
let g:vimspector_enable_mappings='HUMAN'

将vimspector 快捷键映射配置为HUMAN模式:

Key Mapping Function
F5 <Plug>VimspectorContinue When debugging, continue. Otherwise start debugging.
F3 <Plug>VimspectorStop Stop debugging.
F4 <Plug>VimspectorRestart Restart debugging with the same configuration.
F6 <Plug>VimspectorPause Pause debuggee.
F9 <Plug>VimspectorToggleBreakpoint Toggle line breakpoint on the current line.
<leader>F9 <Plug>VimspectorToggleConditionalBreakpoint Toggle conditional line breakpoint or logpoint on the current line.
F8 <Plug>VimspectorAddFunctionBreakpoint Add a function breakpoint for the expression under cursor
<leader>F8 <Plug>VimspectorRunToCursor Run to Cursor
F10 <Plug>VimspectorStepOver Step Over
F11 <Plug>VimspectorStepInto Step Into
F12 <Plug>VimspectorStepOut Step out of current function scope

Adapter安装

下面是各个语言调试使用的Adapter:

通过:VimspectorInstall debugpy:VimspectorInstall delve 完成对python、go 调试adapter的安装。

调试Python

由于NeoVim的LSP指定当前根目录是各LSP Server提供的功能,在调试Ptyon时,在目录中添加.pyrightconfig.json文件:

1
2
3
4
5
{
    "executionEnvironments": [
        {"root": "."}
    ]
}

如上配置,将.pyrightconfig.json文件所在目录,作为Root目录。

配置调试器,添加配置文件 .vimspector.json:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
    "configurations": {
        "run": {
            "adapter": "debugpy",
            "default": true,
            "configuration": {
                "request": "launch",
                "type": "python",
                "cwd": "${workspaceRoot}",
                "stopOnEntry": true,
                "program": "${file}"
            },
            "breakpoints": {
                "exception": {
                    "raised": "N",
                    "uncaught": "",
                    "userUnhandled": ""
                }
            }
        }
    }
}

configuration.program指定要调试的程序,可以通过configuration.env配置调试时程序的环境变量,configuration.args配置参数。 如在调试一个scrapy的爬虫程序时,通过program指定scrapy位置,args指定启动的具体spider:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
    "configurations": {
        "run": {
            "adapter": "debugpy",
            "default": true,
            "configuration": {
                "request": "launch",
                "type": "python",
                "program": "~/opt/miniconda3/envs/p3/bin/scrapy",
                "cwd": "${workspaceRoot}",
                "stopOnEntry": true,
                "args": ["crawl", "${fileBasenameNoExtension}"]
            },
            "breakpoints": {
                "exception": {
                    "raised": "N",
                    "uncaught": "",
                    "userUnhandled": ""
                }
            }
        }
    }
}

调试Go

配置调试器,添加配置文件 .vimspector.json,如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "configurations": {
        "default": {
            "adapter": "delve",
            "default": true,
            "configuration": {
                "request": "launch",
                "mode": "debug",
                "cwd": "${workspaceRoot}",
                "stopOnEntry": true,
                "program": "${file}",
                "args": [
                    "-http_addr","localhost:8084"
                ]
            },
            "breakpoints": {
                "exception": {
                    "raised": "N",
                    "uncaught": "",
                    "userUnhandled": ""
                }
            }
        }
    }
}

指定adapter为delve来调试go程序,program指定要调试的程序文件,args指定参数http_addr=localhost:8084

参考

  1. vimspector
  2. Debugging python in neovim
  3. Neovim — Debugging Application
  4. Programming Go in Neovim
  5. nvim-lsconfig Configurations
  6. vim-delve
  7. golang: debugging application in neovim
  8. nvim-dap
  9. debug-adapter-configuration