在 Neovim 中调试
有许多不同的插件可用于在 Neovim 中调试 Rust 代码。本指南将向您展示如何设置 nvim-dap
和一些额外插件来调试 Tauri 应用程序。
nvim-dap
扩展需要 codelldb
二进制文件。从 https://github.com/vadimcn/codelldb/releases 下载适用于您系统的版本并解压缩。我们稍后会在 nvim-dap
配置中指向它。
安装 nvim-dap
和 nvim-dap-ui
插件。按照其 GitHub 页面上的说明操作,或者简单地使用您喜欢的插件管理器。请注意,nvim-dap-ui
插件需要 nvim-nio
插件。
接下来,在您的 Neovim 配置中设置插件
local dap = require("dap")
dap.adapters.codelldb = { type = 'server', port = "${port}", executable = { -- Change this to your path! command = '/opt/codelldb/adapter/codelldb', args = {"--port", "${port}"}, }}
dap.configurations.rust= { { name = "Launch file", type = "codelldb", request = "launch", program = function() return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/target/debug/', 'file') end, cwd = '${workspaceFolder}', stopOnEntry = false },}
每次启动调试器时,此设置都会要求您指向要调试的 Tauri 应用程序二进制文件。
或者,您可以设置 nvim-dap-ui
插件,以便在每次调试会话开始和停止时自动切换调试器视图
local dapui = require("dapui")dapui.setup()
dap.listeners.before.attach.dapui_config = function() dapui.open()enddap.listeners.before.launch.dapui_config = function() dapui.open()enddap.listeners.before.event_terminated.dapui_config = function() dapui.close()enddap.listeners.before.event_exited.dapui_config = function() dapui.close()end
最后,您可以更改编辑器中显示断点的默认方式
vim.fn.sign_define('DapBreakpoint',{ text ='🟥', texthl ='', linehl ='', numhl =''})vim.fn.sign_define('DapStopped',{ text ='▶️', texthl ='', linehl ='', numhl =''})
由于我们没有使用 Tauri CLI 来启动应用程序,所以开发服务器不会自动启动。为了从 Neovim 控制开发服务器的状态,您可以使用 overseer 插件。
控制后台任务的最佳方式是使用 VS Code 风格的任务配置。为此,请在项目目录中创建一个 .vscode/tasks.json
文件。
您可以在下面找到使用 trunk
的项目任务配置示例。
{ "version": "2.0.0", "tasks": [ { "type": "process", "label": "dev server", "command": "trunk", "args": ["serve"], "isBackground": true, "presentation": { "revealProblems": "onProblem" }, "problemMatcher": { "pattern": { "regexp": "^error:.*", "file": 1, "line": 2 }, "background": { "activeOnStart": false, "beginsPattern": ".*Rebuilding.*", "endsPattern": ".*server listening at:.*" } } } ]}
您可以在下面找到启动和控制调试会话的按键绑定示例。
vim.keymap.set('n', '<F5>', function() dap.continue() end)vim.keymap.set('n', '<F6>', function() dap.disconnect({ terminateDebuggee = true }) end)vim.keymap.set('n', '<F10>', function() dap.step_over() end)vim.keymap.set('n', '<F11>', function() dap.step_into() end)vim.keymap.set('n', '<F12>', function() dap.step_out() end)vim.keymap.set('n', '<Leader>b', function() dap.toggle_breakpoint() end)vim.keymap.set('n', '<Leader>o', function() overseer.toggle() end)vim.keymap.set('n', '<Leader>R', function() overseer.run_template() end)
© 2025 Tauri 贡献者。CC-BY / MIT