在 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 配置中稍后指向它。
🌐 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号
Nodejs.cn 旗下网站