|
| 1 | +--- |
| 2 | +title: Debug in Neovim |
| 3 | +i18nReady: true |
| 4 | +--- |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +### Prerequisites |
| 9 | + |
| 10 | +`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. |
| 11 | + |
| 12 | +### Configuring nvim-dap |
| 13 | + |
| 14 | +Install [`nvim-dap`](https://github.com/mfussenegger/nvim-dap) and [`nvim-dap-ui`](https://github.com/rcarriga/nvim-dap-ui) plugins. Follow the instructions provided on their github pages or simply use your favourite plugin manager. |
| 15 | +Note that `nvim-dap-ui` requires `nvim-nio` plugin. |
| 16 | + |
| 17 | +Next, setup the plugin in your Neovim configuration: |
| 18 | + |
| 19 | +```lua title="init.lua" |
| 20 | +local dap = require("dap") |
| 21 | + |
| 22 | +dap.adapters.codelldb = { |
| 23 | + type = 'server', |
| 24 | + port = "${port}", |
| 25 | + executable = { |
| 26 | + -- Change this to your path! |
| 27 | + command = '/opt/codelldb/adapter/codelldb', |
| 28 | + args = {"--port", "${port}"}, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +dap.configurations.rust= { |
| 33 | + { |
| 34 | + name = "Launch file", |
| 35 | + type = "codelldb", |
| 36 | + request = "launch", |
| 37 | + program = function() |
| 38 | + return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/target/debug/', 'file') |
| 39 | + end, |
| 40 | + cwd = '${workspaceFolder}', |
| 41 | + stopOnEntry = false |
| 42 | + }, |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +This setup will ask you to point to the Tauri App binary you want to debug each time you lanuch the debugger. |
| 47 | + |
| 48 | +Optionally, you can setup `nvim-dap-ui` plugin to toggle debugger view automatically each time debugging session starts and stops: |
| 49 | + |
| 50 | +```lua title="init.lua" |
| 51 | +local dapui = require("dapui") |
| 52 | +dapui.setup() |
| 53 | + |
| 54 | +dap.listeners.before.attach.dapui_config = function() |
| 55 | + dapui.open() |
| 56 | +end |
| 57 | +dap.listeners.before.launch.dapui_config = function() |
| 58 | + dapui.open() |
| 59 | +end |
| 60 | +dap.listeners.before.event_terminated.dapui_config = function() |
| 61 | + dapui.close() |
| 62 | +end |
| 63 | +dap.listeners.before.event_exited.dapui_config = function() |
| 64 | + dapui.close() |
| 65 | +end |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +Lastly, you can change the default way the breakpoints are displayed in the editor: |
| 70 | + |
| 71 | +```lua title="init.lua" |
| 72 | +vim.fn.sign_define('DapBreakpoint',{ text ='🟥', texthl ='', linehl ='', numhl =''}) |
| 73 | +vim.fn.sign_define('DapStopped',{ text ='▶️', texthl ='', linehl ='', numhl =''}) |
| 74 | +``` |
| 75 | + |
| 76 | +### Starting the dev server |
| 77 | + |
| 78 | +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](https://github.com/stevearc/overseer.nvim/tree/master) plugin. |
| 79 | + |
| 80 | +Best way to control tasks running in background is to use [VS Code style task](https://github.com/stevearc/overseer.nvim/blob/master/doc/guides.md#vs-code-tasks) configuration. To do this create a `.vscode/tasks.json` file in the projects directory. |
| 81 | + |
| 82 | +You can find example task configuration for project using `trunk` below. |
| 83 | + |
| 84 | +```json title=".vscode/tasks.json" |
| 85 | +{ |
| 86 | + "version": "2.0.0", |
| 87 | + "tasks": [ |
| 88 | + { |
| 89 | + "type": "process", |
| 90 | + "label": "dev server", |
| 91 | + "command": "trunk", |
| 92 | + "args": ["serve"], |
| 93 | + "isBackground": true, |
| 94 | + "presentation": { |
| 95 | + "revealProblems": "onProblem" |
| 96 | + }, |
| 97 | + "problemMatcher": { |
| 98 | + "pattern": { |
| 99 | + "regexp": "^error:.*", |
| 100 | + "file": 1, |
| 101 | + "line": 2 |
| 102 | + }, |
| 103 | + "background": { |
| 104 | + "activeOnStart": false, |
| 105 | + "beginsPattern": ".*Rebuilding.*", |
| 106 | + "endsPattern": ".*server listening at:.*" |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + ] |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### Example key bindings |
| 115 | + |
| 116 | +Below you can find example key bindings to start and control debugging sessions. |
| 117 | + |
| 118 | +```lua title="init.lua" |
| 119 | +vim.keymap.set('n', '<F5>', function() dap.continue() end) |
| 120 | +vim.keymap.set('n', '<F6>', function() dap.disconnect({ terminateDebuggee = true }) end) |
| 121 | +vim.keymap.set('n', '<F10>', function() dap.step_over() end) |
| 122 | +vim.keymap.set('n', '<F11>', function() dap.step_into() end) |
| 123 | +vim.keymap.set('n', '<F12>', function() dap.step_out() end) |
| 124 | +vim.keymap.set('n', '<Leader>b', function() dap.toggle_breakpoint() end) |
| 125 | +vim.keymap.set('n', '<Leader>o', function() overseer.toggle() end) |
| 126 | +vim.keymap.set('n', '<Leader>R', function() overseer.run_template() end) |
| 127 | +``` |
0 commit comments