跳到内容
Tauri

在 Neovim 中调试

有许多不同的插件可以用于在 Neovim 中调试 Rust 代码。本指南将向您展示如何设置 nvim-dap 和一些额外的插件来调试 Tauri 应用程序。

前提条件

nvim-dap 扩展需要 codelldb 二进制文件。从 https://github.com/vadimcn/codelldb/releases 下载适合您系统的版本并解压。我们将在 nvim-dap 配置中稍后指向它。

配置 nvim-dap

安装 nvim-dapnvim-dap-ui 插件。按照它们 GitHub 页面上提供的说明进行操作,或者直接使用您喜欢的插件管理器。请注意,nvim-dap-ui 需要 nvim-nio 插件。

接下来,在您的 Neovim 配置中设置插件

init.lua
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 插件,以便在每次调试会话开始和停止时自动切换调试器视图

init.lua
local dapui = require("dapui")
dapui.setup()
dap.listeners.before.attach.dapui_config = function()
dapui.open()
end
dap.listeners.before.launch.dapui_config = function()
dapui.open()
end
dap.listeners.before.event_terminated.dapui_config = function()
dapui.close()
end
dap.listeners.before.event_exited.dapui_config = function()
dapui.close()
end

最后,您可以更改断点在编辑器中显示的默认方式

init.lua
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 的项目的示例任务配置。

.vscode/tasks.json
{
"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:.*"
}
}
}
]
}

示例按键绑定

下面您可以找到启动和控制调试会话的示例按键绑定。

init.lua
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