在Neovim中调试
有许多不同的插件可用于在 Neovim 中调试 Rust 代码。本指南将向您展示如何设置 nvim-dap
及其他一些插件以调试 Tauri 应用程序。
先决条件
nvim-dap
扩展需要 codelldb
二进制文件。从 https://github.com/vadimcn/codelldb/releases 下载适合您系统的版本并解压。我们将在后面的 nvim-dap
配置中指向它。
配置 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 控制 Tauri 开发服务器的状态,您可以使用 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