From b48b531d3d535ba1b2ef564d12df350f0e2cc0ef Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Sun, 3 Nov 2024 16:00:31 +0100 Subject: [PATCH] Swith to rustaceanvim and set up Rust debugging --- dotfiles/.config/nvim/lua/lsp.lua | 86 ++++++++++++--------- dotfiles/.config/nvim/lua/plugins/dap.lua | 87 ++++++++++++++++++++++ dotfiles/.config/nvim/lua/plugins/init.lua | 22 +++++- 3 files changed, 155 insertions(+), 40 deletions(-) create mode 100644 dotfiles/.config/nvim/lua/plugins/dap.lua diff --git a/dotfiles/.config/nvim/lua/lsp.lua b/dotfiles/.config/nvim/lua/lsp.lua index 5f337ea..2dde945 100644 --- a/dotfiles/.config/nvim/lua/lsp.lua +++ b/dotfiles/.config/nvim/lua/lsp.lua @@ -19,7 +19,6 @@ local common_on_attach = function(client, bufnr) end, }) end - -- Use vim's default coloring. client.server_capabilities.semanticTokensProvider = nil for _, group in ipairs(vim.fn.getcompletion("@lsp", "highlight")) do @@ -27,13 +26,61 @@ local common_on_attach = function(client, bufnr) end end +-- LSP diagnostics. +vim.lsp.handlers["textDocument/publishDiagnostics"] = + vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { + virtual_text = false, + underline = true, + signs = true, + update_in_insert = false + }) + +-- Setup rust outside of lspconfig. +vim.g.rustaceanvim = function() + return { + -- Plugin configuration. + tools = { + }, + -- LSP configuration. + server = { + on_attach = function(client, bufnr) + common_on_attach(client, bufnr) + end, + default_settings = { + -- rust-analyzer language server configuration + ['rust-analyzer'] = { + imports = { + granularity = { + group = "module", + }, + prefix = "self", + }, + cargo = { + buildScripts = { + enable = true, + }, + }, + procMacro = { + enable = true + }, + checkOnSave = { + command = "clippy", + }, + }, + }, + -- DAP configuration. + dap = { + }, + } + } +end + local nvim_lsp = require('lspconfig') local capabilities = vim.lsp.protocol.make_client_capabilities() -- LSPs local servers = { "clangd", - "gleam", "lua_ls", "ocamllsp", "pyright", @@ -45,32 +92,6 @@ for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup { capabilities = capabilities, on_attach = common_on_attach } end -nvim_lsp.rust_analyzer.setup({ - on_attach = common_on_attach, - capabilities = capabilities, - settings = { - ["rust-analyzer"] = { - imports = { - granularity = { - group = "module", - }, - prefix = "self", - }, - cargo = { - buildScripts = { - enable = true, - }, - }, - procMacro = { - enable = true - }, - checkOnSave = { - command = "clippy", - }, - } - } -}) - nvim_lsp.ruff.setup({ on_attach = function (client) common_on_attach(client) @@ -84,12 +105,3 @@ nvim_lsp.ruff.setup({ } }, }) - --- LSP diagnostics. -vim.lsp.handlers["textDocument/publishDiagnostics"] = - vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { - virtual_text = false, - underline = true, - signs = true, - update_in_insert = false - }) diff --git a/dotfiles/.config/nvim/lua/plugins/dap.lua b/dotfiles/.config/nvim/lua/plugins/dap.lua new file mode 100644 index 0000000..de6ae2f --- /dev/null +++ b/dotfiles/.config/nvim/lua/plugins/dap.lua @@ -0,0 +1,87 @@ +local dap, dapui = require('dap'), require('dapui') + +-- Open dapui once dap starts. +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 + +-- Keybindings used in probably all editors/IDEs. +vim.keymap.set('n', '', dap.continue) +vim.keymap.set('n', '', dap.step_over) +vim.keymap.set('n', '', dap.step_into) +vim.keymap.set('n', '', dap.step_out) +-- Breakpoints. +vim.keymap.set('n', 'b', dap.toggle_breakpoint) +vim.keymap.set('n', 'B', dap.set_breakpoint) +vim.keymap.set('n', 'lp', function() dap.set_breakpoint(nil, nil, vim.fn.input('Log point message: ')) end) +-- Repl, run and dap-ui bindings. +vim.keymap.set('n', 'dr', dap.repl.open) +vim.keymap.set('n', 'dl', dap.run_last) +vim.keymap.set({'n', 'v'}, 'dh', function() require('dap.ui.widgets').hover() end) +vim.keymap.set({'n', 'v'}, 'dp', function() require('dap.ui.widgets').preview() end) +vim.keymap.set('n', 'df', function() local widgets = require('dap.ui.widgets') widgets.centered_float(widgets.frames) end) +vim.keymap.set('n', 'ds', function() local widgets = require('dap.ui.widgets') widgets.centered_float(widgets.scopes) end) + +-- Set up lldb (LLVM debugger) +-- Install archlinux: sudo pacman -S lldb +-- Install ubuntu: sudo apt +dap.adapters.lldb = { + type = 'server', + port = "${port}", + executable = { + command = '/usr/bin/lldb-dap', + args = { "--port", "${port}" }, + }, +} + +-- Custom debuggable build runs (on top of rust-analyzer debuggables). +local ExecTypes = { + TEST = "cargo build --tests -q --message-format=json", + BIN = "cargo build -q --message-format=json" +} +local function runBuild(type) + local lines = vim.fn.systemlist(type) + local output = table.concat(lines, "\n") + local filename = output:match('^.*"executable":"(.*)",.*\n.*,"success":true}$') + + if filename == nil then + return error("failed to build cargo project") + end + + return filename +end +-- Custom debuggable configurations. +dap.configurations.rust = { + { + name = "Debug Test", + type = "lldb", + request = "launch", + program = function () + return runBuild(ExecTypes.TEST) + end, + cwd = "${workspaceFolder}", + stopOnEntry = false, + showDisassembly = "never" + }, + { + name = "Debug Bin", + type = "lldb", + request = "launch", + program = function () + return runBuild(ExecTypes.BIN) + end, + cwd = "${workspaceFolder}", + stopOnEntry = false, + showDisassembly = "never" + } +} diff --git a/dotfiles/.config/nvim/lua/plugins/init.lua b/dotfiles/.config/nvim/lua/plugins/init.lua index 61e0a61..94e4584 100644 --- a/dotfiles/.config/nvim/lua/plugins/init.lua +++ b/dotfiles/.config/nvim/lua/plugins/init.lua @@ -38,14 +38,29 @@ require('packer').startup(function() -- LSP and related use { 'neovim/nvim-lspconfig' } use { 'aznhe21/actions-preview.nvim' } - use { 'folke/trouble.nvim' } -- lsp diagnostics + use { 'folke/trouble.nvim' } -- diagnostics - -- Plugins to extend builtin language support + -- Plugins to extend builtin lsp use { 'cespare/vim-toml' } use { 'gleam-lang/gleam.vim' } - use { 'rust-lang/rust.vim' } use { 'folke/neodev.nvim' } use { 'hashivim/vim-terraform' } + use { + 'mrcjkb/rustaceanvim', + requires = { + 'mfussenegger/nvim-dap' + } + } + + -- Debugging + use { 'mfussenegger/nvim-dap' } + use { + "rcarriga/nvim-dap-ui", + requires = { + "mfussenegger/nvim-dap", + "nvim-neotest/nvim-nio", + } + } -- Git use { @@ -70,6 +85,7 @@ end) require('plugins.catppuccin') require('plugins.cmp') +require('plugins.dap') require('plugins.gitsigns') require('plugins.luasnip_config') require('plugins.neogit')