在 Neovim 中调试
有许多不同的插件可用于在 Neovim 中调试 Rust 代码。本指南将向你展示如何设置 nvim-dap
和一些其他插件来调试 Tauri 应用。
¥There are many different plugins that can be used to debug Rust code in Neovim. This guide will show you how to set up nvim-dap
and some additional plugins to debug Tauri application.
先决条件
¥Prerequisites
nvim-dap
扩展需要 codelldb
二进制文件。从 https://github.com/vadimcn/codelldb/releases 下载适合你系统的版本并解压。我们将稍后在 nvim-dap
配置中指向它。
¥nvim-dap
extension requires codelldb
binary. Download the version for your system from https://github.com/vadimcn/codelldb/releases and unzip it. We will point to it later in the nvim-dap
configuration.
配置 nvim-dap
¥Configuring nvim-dap
安装 nvim-dap
和 nvim-dap-ui
插件。按照其 github 页面上提供的说明进行操作,或者直接使用你最喜欢的插件管理器。请注意,nvim-dap-ui
需要 nvim-nio
插件。
¥Install nvim-dap
and nvim-dap-ui
plugins. Follow the instructions provided on their github pages or simply use your favourite plugin manager.
Note that nvim-dap-ui
requires nvim-nio
plugin.
接下来,在你的 Neovim 配置中设置插件:
¥Next, setup the plugin in your Neovim configuration:
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 App 二进制文件。
¥This setup will ask you to point to the Tauri App binary you want to debug each time you lanuch the debugger.
或者,你可以设置 nvim-dap-ui
插件以在每次调试会话启动和停止时自动切换调试器视图:
¥Optionally, you can setup nvim-dap-ui
plugin to toggle debugger view automatically each time debugging session starts and stops:
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
最后,你可以更改断点在编辑器中显示的默认方式:
¥Lastly, you can change the default way the breakpoints are displayed in the editor:
vim.fn.sign_define('DapBreakpoint',{ text ='🟥', texthl ='', linehl ='', numhl =''})vim.fn.sign_define('DapStopped',{ text ='▶️', texthl ='', linehl ='', numhl =''})
启动开发服务器
¥Starting the dev server
由于我们没有使用 Tauri CLI 来启动应用,因此开发服务器不会自动启动。要从 Neovim 控制开发服务器的状态,你可以使用 overseer 插件。
¥Since we’re not using Tauri CLI to launch the app the development server will not start automatically. To control the state of development server from Neovim you can use the overseer plugin.
控制后台运行任务的最佳方法是使用 VS Code 样式任务 配置。为此,请在项目目录中创建一个 .vscode/tasks.json
文件。
¥Best way to control tasks running in background is to use VS Code style task configuration. To do this create a .vscode/tasks.json
file in the projects directory.
你可以在下面找到使用 trunk
的项目的示例任务配置。
¥You can find example task configuration for project using trunk
below.
{ "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:.*" } } } ]}
示例键绑定
¥Example key bindings
下面你可以找到启动和控制调试会话的示例键绑定。
¥Below you can find example key bindings to start and control debugging sessions.
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)
Tauri 中文网 - 粤ICP备13048890号