From: Kit Rhett Aultman Date: Mon, 24 Jun 2024 03:50:40 +0000 (-0400) Subject: nvim dotfile setup for Rust X-Git-Url: https://git.kitaultman.com/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=nvim_dotfiles.git nvim dotfile setup for Rust For details on everything set up in here, see https://rsdlt.github.io/posts/rust-nvim-ide-guide-walkthrough-development-debug/ Note this required setting up packer via its own git clone. See here: https://github.com/wbthomason/packer.rust-nvim-ide-guide-walkthrough-development-debug --- 38f2077023244256a82909037328a21d1440a036 diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..d6d0c92 --- /dev/null +++ b/init.lua @@ -0,0 +1,153 @@ +-- Mason Setup +require("mason").setup({ + ui = { + icons = { + package_installed = "", + package_pending = "", + package_uninstalled = "", + }, + } +}) +require("mason-lspconfig").setup() + +local rt = require("rust-tools") + +rt.setup({ + server = { + on_attach = function(_, bufnr) + -- Hover actions + vim.keymap.set("n", "", rt.hover_actions.hover_actions, { buffer = bufnr }) + -- Code action groups + vim.keymap.set("n", "a", rt.code_action_group.code_action_group, { buffer = bufnr }) + end, + }, +}) + +-- LSP Diagnostics Options Setup +local sign = function(opts) + vim.fn.sign_define(opts.name, { + texthl = opts.name, + text = opts.text, + numhl = '' + }) +end + +sign({name = 'DiagnosticSignError', text = ''}) +sign({name = 'DiagnosticSignWarn', text = ''}) +sign({name = 'DiagnosticSignHint', text = ''}) +sign({name = 'DiagnosticSignInfo', text = ''}) + +vim.diagnostic.config({ + virtual_text = false, + signs = true, + update_in_insert = true, + underline = true, + severity_sort = false, + float = { + border = 'rounded', + source = 'always', + header = '', + prefix = '', + }, +}) + +vim.cmd([[ +set signcolumn=yes +autocmd CursorHold * lua vim.diagnostic.open_float(nil, { focusable = false }) +]]) + +-- Completion Plugin Setup +local cmp = require('cmp') + +cmp.setup({ + -- Enable LSP snippets + snippet = { + expand = function(args) + vim.fn["vsnip#anonymous"](args.body) + end, + }, + mapping = { + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + -- Add tab support + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + [''] = cmp.mapping.confirm({ + behavior = cmp.ConfirmBehavior.Insert, + select = true, + }) + }, + -- Installed sources: + sources = { + { name = 'path' }, -- file paths + { name = 'nvim_lsp', keyword_length = 3 }, -- from language server + { name = 'nvim_lsp_signature_help'}, -- display function signatures with current parameter emphasized + { name = 'nvim_lua', keyword_length = 2}, -- complete neovim's Lua runtime API such vim.lsp.* + { name = 'buffer', keyword_length = 2 }, -- source current buffer + { name = 'vsnip', keyword_length = 2 }, -- nvim-cmp source for vim-vsnip + { name = 'calc'}, -- source for math calculation + }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + formatting = { + fields = {'menu', 'abbr', 'kind'}, + format = function(entry, item) + local menu_icon ={ + nvim_lsp = 'λ', + vsnip = '⋗', + buffer = 'Ω', + path = '🖫', + } + item.menu = menu_icon[entry.source.name] + return item + end, + }, +}) + +-- Treesitter Plugin Setup +require('nvim-treesitter.configs').setup { + ensure_installed = { "lua", "rust", "toml" }, + auto_install = true, + highlight = { + enable = true, + additional_vim_regex_highlighting=false, + }, + ident = { enable = true }, + rainbow = { + enable = true, + extended_mode = true, + max_file_lines = nil, + } +} + +-- disable netrw at the very start of your init.lua +vim.g.loaded_netrw = 1 +vim.g.loaded_netrwPlugin = 1 + +-- optionally enable 24-bit colour +vim.opt.termguicolors = true + +-- empty setup using defaults +require("nvim-tree").setup() + +-- OR setup with some options +require("nvim-tree").setup({ + sort = { + sorter = "case_sensitive", + }, + view = { + width = 30, + }, + renderer = { + group_empty = true, + }, + filters = { + dotfiles = true, + }, +}) diff --git a/lua/keys.lua b/lua/keys.lua new file mode 100644 index 0000000..faa65a9 --- /dev/null +++ b/lua/keys.lua @@ -0,0 +1,17 @@ +-- Vimspector +vim.cmd([[ +nmap call vimspector#Launch() +nmap call vimspector#StepOver() +nmap call vimspector#Reset() +nmap call vimspector#StepOver()") +nmap call vimspector#StepOut()") +nmap call vimspector#StepInto()") +]]) +map('n', "Db", ":call vimspector#ToggleBreakpoint()") +map('n', "Dw", ":call vimspector#AddWatch()") +map('n', "De", ":call vimspector#Evaluate()") + +-- FloaTerm configuration +map('n', "ft", ":FloatermNew --name=myfloat --height=0.8 --width=0.7 --autoclose=2 fish ") +map('n', "t", ":FloatermToggle myfloat") +map('t', "", ":q") diff --git a/lua/opts.lua b/lua/opts.lua new file mode 100644 index 0000000..eabc6c1 --- /dev/null +++ b/lua/opts.lua @@ -0,0 +1,31 @@ +--Set completeopt to have a better completion experience +-- :help completeopt +-- menuone: popup even when there's only one match +-- noinsert: Do not insert text until a selection is made +-- noselect: Do not select, force to select one from the menu +-- shortness: avoid showing extra messages when using completion +-- updatetime: set updatetime for CursorHold +vim.opt.completeopt = {'menuone', 'noselect', 'noinsert'} +vim.opt.shortmess = vim.opt.shortmess + { c = true} +vim.api.nvim_set_option('updatetime', 300) + +-- Fixed column for diagnostics to appear +-- Show autodiagnostic popup on cursor hover_range +-- Goto previous / next diagnostic warning / error +-- Show inlay_hints more frequently +vim.cmd([[ +set signcolumn=yes +autocmd CursorHold * lua vim.diagnostic.open_float(nil, { focusable = false }) +]]) + +-- Treesitter folding +vim.wo.foldmethod = 'expr' +vim.wo.foldexpr = 'nvim_treesitter#foldexpr()' + +-- Vimspector options +vim.cmd([[ +let g:vimspector_sidebar_width = 85 +let g:vimspector_bottombar_height = 15 +let g:vimspector_terminal_maxwidth = 70 +]]) + diff --git a/lua/plug.lua b/lua/plug.lua new file mode 100644 index 0000000..7e707d6 --- /dev/null +++ b/lua/plug.lua @@ -0,0 +1,35 @@ +return require('packer').startup(function() + -- other plugins... + + use 'williamboman/mason.nvim' + use 'williamboman/mason-lspconfig.nvim' + use 'neovim/nvim-lspconfig' + use 'simrat39/rust-tools.nvim' + + -- Completion framework: + use 'hrsh7th/nvim-cmp' + + -- LSP completion source: + use 'hrsh7th/cmp-nvim-lsp' + + -- Useful completion sources: + use 'hrsh7th/cmp-nvim-lua' + use 'hrsh7th/cmp-nvim-lsp-signature-help' + use 'hrsh7th/cmp-vsnip' + use 'hrsh7th/cmp-path' + use 'hrsh7th/cmp-buffer' + use 'hrsh7th/vim-vsnip' + + -- Treesitter + use 'nvim-treesitter/nvim-treesitter' + -- Viminspector (for debugging) + use 'puremourning/vimspector' + + use 'voldikss/vim-floaterm' + use 'phaazon/hop.nvim' + use { 'nvim-tree/nvim-tree.lua', + requires = { + 'nvim-tree/nvim-web-devicons', -- optional + }, + } +end)