From 659ef00c71fd17efd2d8ffe5bbe5bd9f1698e73d Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 6 Oct 2024 17:43:18 +0200 Subject: Drag my neovim config kicking and screaming into the future (LSP) --- nvim/default.nix | 81 ++++++++++++++++++++----------------------------- nvim/init.lua | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nvim/init.vim | 38 +---------------------- 3 files changed, 125 insertions(+), 85 deletions(-) create mode 100644 nvim/init.lua (limited to 'nvim') diff --git a/nvim/default.nix b/nvim/default.nix index 87a4693..4e108f0 100644 --- a/nvim/default.nix +++ b/nvim/default.nix @@ -2,69 +2,54 @@ pkgs = (import ../pkgs.nix).stable {}; - misspell = pkgs.buildGoPackage rec { - pname = "misspell"; - version = "0.3.4"; - goPackagePath = "github.com/client9/misspell"; - - src = pkgs.fetchFromGitHub { - owner = "client9"; - repo = "misspell"; - rev = "v${version}"; - sha256 = "1vwf33wsc4la25zk9nylpbp9px3svlmldkm0bha4hp56jws4q9cs"; - }; - - goDeps = ./misspellDeps.nix; + gopls = pkgs.buildGoModule rec { + pname = "gopls"; + version = "0.16.2"; + + src = "${pkgs.fetchFromGitHub { + owner = "golang"; + repo = "tools"; + rev = "gopls/v${version}"; + sha256 = "sha256-amy00VMUcmyjDoZ4d9/+YswfcZ+1/cGvFsA4sAmc1dA="; + }}/gopls"; + + subPackages = ["."]; + vendorHash = "sha256-ta94xPboFtSxFeuMtPX76XiC1O7osNl4oLk64wIyyz4="; + doCheck = false; }; env = pkgs.buildEnv { name = "nvim-env"; paths = [ - pkgs.shellcheck - misspell + gopls ]; }; - envPlugins = "${env}/share/vim-plugins"; + init = pkgs.writeText "nvim-init.lua" '' + vim.cmd('source ${pkgs.vimPlugins.vim-plug}/plug.vim') - init = pkgs.writeText "nvim-init" '' - source ${pkgs.vimPlugins.vim-plug}/plug.vim + local Plug = vim.fn['plug#'] - call plug#begin() - Plug '${pkgs.vimPlugins.deoplete-nvim}' - Plug '${pkgs.vimPlugins.nerdtree}', { 'on': 'NERDTreeToggle' } - Plug '${pkgs.vimPlugins.nerdtree-git-plugin}' - Plug '${pkgs.vimPlugins.vim-gitgutter}' - Plug '${pkgs.vimPlugins.neomake}' - Plug '${pkgs.vimPlugins.vim-go}', { 'for': 'go' } - Plug '${pkgs.vimPlugins.vim-nix}', { 'for': 'nix' } - Plug '${pkgs.vimPlugins.rust-vim}', { 'for': 'rust' } - call plug#end() + vim.call('plug#begin') - source ${./init.vim} - ''; + Plug('${pkgs.vimPlugins.nerdtree}', { ['on'] = 'NERDTreeToggle' }) + Plug('${pkgs.vimPlugins.nerdtree-git-plugin}') + Plug('${pkgs.vimPlugins.vim-gitgutter}') - nvimRaw = pkgs.writeScriptBin "nvim" '' - #!${pkgs.bash}/bin/bash - export PATH=${env}/bin:$PATH - exec ${pkgs.neovim}/bin/nvim -u ${init} "$@" - ''; + Plug('${pkgs.vimPlugins.nvim-lspconfig}') + Plug('${pkgs.vimPlugins.nvim-cmp}') + Plug('${pkgs.vimPlugins.cmp-nvim-lsp}') - rplugin = pkgs.stdenv.mkDerivation { - name = "nvim-rplugin"; - buildInputs = [ pkgs.git pkgs.tree nvimRaw ]; - builder = builtins.toFile "builder.sh" '' - source $stdenv/setup - mkdir -p "$out"/ - export NVIM_RPLUGIN_MANIFEST="$out"/rplugin.vim - nvim -i NONE -c ':UpdateRemotePlugins' -c ':exit' >/dev/null - ''; - }; + vim.call('plug#end') + + vim.cmd('source ${./init.vim}') + + ${builtins.readFile ./init.lua} + ''; nvim = pkgs.writeScriptBin "nvim" '' #!${pkgs.bash}/bin/bash - export NVIM_RPLUGIN_MANIFEST=${rplugin}/rplugin.vim - exec ${nvimRaw}/bin/nvim "$@" + export PATH=${env}/bin:$PATH + exec ${pkgs.neovim}/bin/nvim -u ${init} "$@" ''; - } diff --git a/nvim/init.lua b/nvim/init.lua new file mode 100644 index 0000000..9f4e393 --- /dev/null +++ b/nvim/init.lua @@ -0,0 +1,91 @@ +-------------------------------------------------------------------------------- +-- LSP configuration. nvim-cmp related stuff taken from: +-- https://github.com/neovim/nvim-lspconfig/wiki/Autocompletion/217feffc675a17d8ab95259ed9d4c6d62e1cd2e1 +-------------------------------------------------------------------------------- + +local lspconfig = require("lspconfig") + +local capabilities = require("cmp_nvim_lsp").default_capabilities() + +-- https://github.com/hrsh7th/nvim-cmp/issues/373#issuecomment-1434284568 +capabilities.textDocument.completion.completionItem.snippetSupport = false + +lspconfig.gopls.setup { + capabilities = capabilities, +} + +local cmp = require("cmp") +cmp.setup { + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), -- Up + [''] = cmp.mapping.scroll_docs(4), -- Down + -- C-b (back) C-f (forward) for snippet placeholder navigation. + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + else + fallback() + end + end, { 'i', 's' }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, { 'i', 's' }), + }), + sources = { + { name = 'nvim_lsp' }, + }, +} + +-- Format files using LSP on save +vim.api.nvim_create_autocmd('BufWritePre', { + callback = function(args) + vim.lsp.buf.format() + end, +}) + +-- Open LSP Diagnostics in quickfix list on save +vim.api.nvim_create_autocmd('BufWritePost', { + callback = function(args) + vim.diagnostic.setqflist() + end, +}) + +-------------------------------------------------------------------------------- +-- Run 'organizeImports' on save for go files. Apparently organizing imports is +-- considered a separate task than formatting, so it's implemented separately +-- (in a much more cumbersome way, for some reason). +-- https://github.com/golang/tools/blob/0b989c812dcb662451db0b70a8267fe7e211886e/gopls/doc/vim.md#neovim-imports +-- https://github.com/golang/go/issues/33587 +-------------------------------------------------------------------------------- + +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*.go", + callback = function() + local params = vim.lsp.util.make_range_params() + params.context = {only = {"source.organizeImports"}} + -- buf_request_sync defaults to a 1000ms timeout. Depending on your + -- machine and codebase, you may want longer. Add an additional + -- argument after params if you find that you have to write the file + -- twice for changes to be saved. + -- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000) + local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params) + for cid, res in pairs(result or {}) do + for _, r in pairs(res.result or {}) do + if r.edit then + local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16" + vim.lsp.util.apply_workspace_edit(r.edit, enc) + end + end + end + vim.lsp.buf.format({async = false}) + end +}) diff --git a/nvim/init.vim b/nvim/init.vim index 08ce5eb..213099f 100644 --- a/nvim/init.vim +++ b/nvim/init.vim @@ -1,16 +1,6 @@ set noswapfile -" Deoplete ################################################################# - -let g:deoplete#enable_at_startup = 1 -" use tab to cycle -inoremap pumvisible() ? "\" : "\" -" close preview when leaving insert -autocmd InsertLeave,CompleteDone * if pumvisible() == 0 | pclose | endif -" use omni completion for go, provided by vim-go -call deoplete#custom#option('omni_patterns', { 'go': '[^. *\t]\.\w*' }) - " NERDTree ################################################################# let NERDTreeMouseMode=3 @@ -34,39 +24,13 @@ map :NERDTreeToggle " always enter term buffer in insert mode autocmd BufEnter * if &buftype == 'terminal' | :startinsert | endif -" vim-go ################################################################### - -let g:go_fmt_autosave = 1 -let g:go_fmt_command="goimports" - " rust.vim ################################################################### let g:rustfmt_autosave = 1 " Caddyfile #################################################################### -" -au BufNewFile,BufRead Caddyfile,*.Caddyfile,Caddyfile.* set ft=caddyfile - -" neomake ################################################################## -autocmd! BufWritePost * Neomake -"let g:neomake_verbose=3 -"let g:neomake_logfile='/tmp/neomake.log' - -" Always run clippy on rust projects -autocmd! BufWritePost *.rs Neomake! clippy - -" the sidebar sign placement wasn't playing nice with gitgutter, so use the -" location list instead. But location list is kinda dumb cause it pops open -" multiple times and at weird times, sooo.... fuck it -"let g:neomake_open_list=2 -let g:neomake_open_list=0 -let g:neomake_place_signs=0 - -let g:neomake_markdown_enabled_makers = ['misspell'] -let g:neomake_markdown_misspell_maker = { - \ 'errorformat': '%f:%l:%c:%m', - \ } +au BufNewFile,BufRead Caddyfile,*.Caddyfile,Caddyfile.* set ft=caddyfile " mine ##################################################################### -- cgit v1.2.3