在 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 控制開發伺服器的狀態,您可以使用 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