Skip to content

NeoVim调试Python、Golang

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

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

在plug.vim中添加:

Plug 'puremourning/vimspector'

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

let g:vimspector_enable_mappings='HUMAN'

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

KeyMappingFunction
F5<Plug>VimspectorContinueWhen debugging, continue. Otherwise start debugging.
F3<Plug>VimspectorStopStop debugging.
F4<Plug>VimspectorRestartRestart debugging with the same configuration.
F6<Plug>VimspectorPausePause debuggee.
F9<Plug>VimspectorToggleBreakpointToggle line breakpoint on the current line.
<leader>F9<Plug>VimspectorToggleConditionalBreakpointToggle conditional line breakpoint or logpoint on the current line.
F8<Plug>VimspectorAddFunctionBreakpointAdd a function breakpoint for the expression under cursor
<leader>F8<Plug>VimspectorRunToCursorRun to Cursor
F10<Plug>VimspectorStepOverStep Over
F11<Plug>VimspectorStepIntoStep Into
F12<Plug>VimspectorStepOutStep out of current function scope

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

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

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

{
"executionEnvironments": [
{"root": "."}
]
}

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

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

{
"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:

{
"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": ""
}
}
}
}
}

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

{
"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