From f192da86a7d1ecc89757cc8eecc51ecaf8aa96eb Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Sun, 21 Mar 2021 16:10:17 +0100 Subject: [PATCH] vim: Migrate from ALE and vim-go -> vim-lsp --- vim/after/ftplugin/go.vim | 54 +++--------- vimrc | 179 ++++++++++++++++++++++---------------- 2 files changed, 116 insertions(+), 117 deletions(-) diff --git a/vim/after/ftplugin/go.vim b/vim/after/ftplugin/go.vim index f5ae6c3..c01737a 100644 --- a/vim/after/ftplugin/go.vim +++ b/vim/after/ftplugin/go.vim @@ -1,28 +1,14 @@ -nmap gvd (go-def-vertical) -nmap gsd (go-def-split) -nmap gvD (go-def-type-vertical) -nmap gsD (go-def-type-split) - nmap gb :up:GoBuild nmap gr :up:GoRun -nmap gc :GoCallers -" TODO: if GoInfo returns nothing an [INFO] SUCCESS message is shown. -" But disabling g:go_echo_command_info impacts build and test workflow. -nmap gi :GoInfo -nmap gp :GoImplements -nmap ga :GoAlternate +nmap gt :up:GoTest +nmap gf :up:GoTestFunc + nmap gk :GoDocBrowser -nmap gd :GoDecls -nmap gD :GoDeclsDir -" mnemonic: match -nmap gm :GoSameIds nmap gv :vsplit:GoAlternate! nmap gV :vsplit:GoAlternate! nmap gs :split:GoAlternate! nmap gS :split:GoAlternate! -nmap gt :up:GoTest -nmap gf :up:GoTestFunc nmap gat :GoAddTags nmap grt :GoRemoveTags nmap gec :up:GoErrCheck @@ -34,41 +20,23 @@ nnoremap dq :GoDebugStop nnoremap dn :GoDebugNext nnoremap ds :GoDebugStep -" Avoid window positions jumping around when building and testing: -set cmdheight=2 - -" fzf configuration: -let g:fzf_action = { - \ 'ctrl-t': 'tab split', - \ 'ctrl-s': 'split', - \ 'ctrl-v': 'vsplit' } - -" Vim-Go configuration: -let g:go_decls_mode = 'fzf' +" Vim-Go configuration +let g:go_gopls_enabled = 0 let g:go_fmt_autosave = 0 -" Vim-Go seems to handle fix-on-save better than ALE: +" TODO: move to vim-lsp let g:go_imports_autosave = 1 let g:go_auto_sameids = 0 let g:go_auto_type_info = 0 -let g:go_info_mode = 'gopls' -" ALE handles auto type info: -let g:go_auto_type_info = 0 -let g:go_code_completion_enabled = 1 +let g:go_code_completion_enabled = 0 let g:go_echo_command_info = 0 let g:go_echo_go_info = 0 let g:go_highlight_diagnostic_errors = 0 let g:go_highlight_diagnostic_warnings = 0 -" g:go_statusline_duration doesn't seem to work accurately, but does help: + +" g:go_statusline_duration doesn't seem to work accurately, but does help +" reduce the time the build/test success messages are shown in the status +" line. let g:go_statusline_duration = 1000 -let g:go_gopls_analyses = { - \ 'composites': v:false, - \ 'unusedparams': v:true, - \ 'unusedresult': v:true, - \ 'shadow': v:true, - \ } -let g:go_fmt_options = { - \ 'goimports': '-local github.com/sensiblecodeio/cantabular', - \ } autocmd BufNewFile,BufRead *.go nmap g6 :GoAlternate! diff --git a/vimrc b/vimrc index ca4ae07..99b0d3e 100644 --- a/vimrc +++ b/vimrc @@ -160,6 +160,10 @@ inoremap pumvisible() ? "\" : "\" let g:vim_markdown_conceal = 1 " fzf configuration: +let g:fzf_action = { + \ 'ctrl-t': 'tab split', + \ 'ctrl-s': 'split', + \ 'ctrl-v': 'vsplit' } nmap t :Files nmap T :GFiles nmap b :Buffers @@ -168,7 +172,29 @@ nmap gg :Rg " Lightline configuration: packadd! lightline.vim function! GitHead() - return '[' . FugitiveHead() . ']' + let l:head = FugitiveHead() + if l:head == '' + return '' + endif + return '[' . l:head . ']' +endfunction + +function! LightlineLSPErrorText() + let l:info = lsp#get_buffer_diagnostics_counts() + if info.error == 0 + return '' + endif + let sign = get(g:lsp_diagnostics_signs_error, 'text', 'E') + return info.error . sign +endfunction + +function! LightlineLSPWarningText() + let l:info = lsp#get_buffer_diagnostics_counts() + if info.warning == 0 + return '' + endif + let sign = get(g:lsp_diagnostics_signs_warning, 'text', 'W') + return info.warning . sign endfunction set laststatus=2 @@ -177,13 +203,21 @@ let g:lightline = { \ 'active': { \ 'left': [ [ 'mode', 'paste' ], \ [ 'readonly', 'filename', 'modified' ], - \ [ 'gitbranch' ] ], + \ [ 'gitbranch' ], ['lsperror', 'lspwarn'] ], \ 'right': [ ['lineinfo'], ['percent'], ['filetype'], ['gobuild'] ], \ }, \ 'component_function': { \ 'gobuild': 'go#statusline#Show', \ 'gitbranch': 'GitHead', \ }, + \ 'component_expand': { + \ 'lsperror': 'LightlineLSPErrorText', + \ 'lspwarn': 'LightlineLSPWarningText', + \ }, + \ 'component_type': { + \ 'lsperror': 'error', + \ 'lspwarn': 'warning', + \ }, \ 'mode_map': { \ 'n' : 'N', \ 'i' : 'I', @@ -220,82 +254,81 @@ let g:echodoc#type='echo' let g:echodoc#enable_at_startup = 1 packadd! echodoc.vim -" ALE configuration: -nmap [L (ale_first) -nmap ]L (ale_last) -nmap [e (ale_previous_wrap_error) -nmap ]e (ale_next_wrap_error) -nmap [w (ale_previous_wrap) -nmap ]w (ale_next_wrap) +" vim-fugitive +" load before LSP to ensure it doesn't clobber signcolumn +packadd! vim-fugitive -nmap ad :ALEDetail -map g :ALEGoToDefinition -map f :ALEFindReferences -map fv :ALEFindReferences -vsplit -map fh :ALEFindReferences -split -map ft :ALEFindReferences -tab -nnoremap gD :ALEGoToTypeDefinition -nnoremap gd :ALEGoToDefinition -command GoToDefinition ALEGoToDefinition -command GoToTypeDefinition ALEGoToTypeDefinition -command FindReferences ALEFindReferences -command Rename ALERename +" vim-go +" load before LSP to ensure it doesn't clobber mappings +packadd! vim-go -let g:ale_sign_column_always = 1 -let g:ale_fix_on_save = 1 -let g:ale_lint_on_text_changed = 1 -let g:ale_lint_on_enter = 1 -let g:ale_lint_on_insert_leave = 1 -let g:ale_lint_on_save = 1 -let g:ale_echo_cursor = 1 -let g:ale_lint_delay = 100 -let g:ale_lsp_suggestions = 1 -let g:ale_set_balloons = 1 -" NOTE: Needs https://github.com/dense-analysis/ale/issues/3350 -let g:ale_rust_analyzer_config = { - \ 'diagnostics': { 'disabled': ['unresolved-import'] }, - \ 'cargo': { 'loadOutDirsFromCheck': v:true }, - \ 'procMacro': { 'enable': v:true }, - \ 'checkOnSave': { 'command': 'clippy', 'enable': v:true }, - \ } -let g:ale_go_goimports_options = '-local github.com/sensiblecodeio/cantabular' -let g:ale_go_gopls_init_options = { - \ 'ui.diagnostic.analyses': { - \ 'composites': v:false, - \ 'unusedparams': v:true, - \ 'unusedresult': v:true, - \ 'shadow': v:true, - \ }, - \ 'staticcheck': v:true} -" let g:ale_command_wrapper = 'nice -n5' -let g:ale_completion_enabled = 0 -let g:ale_close_preview_on_insert = 1 -let g:ale_hover_to_preview = 1 -let g:ale_history_log_output = 0 -let g:ale_linters = { - \ 'javascript': ['eslint'], - \ 'go': ['gopls'], - \ 'rust': ['analyzer'], - \ 'css': ['stylelint'], - \ 'python': ['pyls'], - \ 'ruby': ['rubocop'], - \ } -let g:ale_fixers = { - \ '*': ['remove_trailing_lines', 'trim_whitespace'], - \ 'javascript': ['prettier'], - \ 'rust': [], - \ 'go': [], - \ 'css': ['stylelint'], - \ 'python': ['autopep8'], - \ 'ruby': ['rubocop'], - \ } +" LSP configuration: +packadd! vim-lsp +packadd! vim-lsp-settings +packadd! deoplete-vim-lsp + +let g:lsp_settings = { + \ 'gopls': { + \ 'initialization_options': { + \ 'ui.diagnostic.analyses': { + \ 'composites': v:false, + \ 'unusedparams': v:true, + \ 'unusedresult': v:true, + \ 'shadow': v:true, + \ }, + \ 'staticcheck': v:true, + \ }}} +let g:lsp_diagnostics_float_cursor = 1 + +function! s:on_lsp_buffer_enabled() abort + setlocal omnifunc=lsp#complete + setlocal signcolumn=yes + + " Jump to definitions + nmap gd (lsp-definition) + nmap gvd :vertical :LspDefinition + nmap gsd :belowright :LspDefinition + nmap gD (lsp-type-definition) + nmap gvD :vertical :LspTypeDefinition + nmap gsD :belowright :LspTypeDefinition + " Map to leader keys too, easy typo: + nmap gd (lsp-definition) + nmap gD (lsp-type-definition) + + " Peek definitions, only on leader keys + nmap pd (lsp-peek-definition) + nmap pD (lsp-peek-type-definition) + + " Navigation + nmap ]e (lsp-next-error) + nmap [e (lsp-previous-error) + nmap ]d (lsp-next-diagnostic) + nmap [d (lsp-previous-diagnostic) + nmap ]r (lsp-next-reference) + nmap [r (lsp-previous-reference) + + " Misc actions + nmap ci (lsp-call-hierarchy-incoming) + nmap co (lsp-call-hierarchy-outgoing) + nmap a (lsp-code-action) + nmap i (lsp-implementation) + nmap r (lsp-references) + nmap d (lsp-hover) + + nmap e (lsp-rename) +endfunction + +augroup lsp_install + au! + autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled() + autocmd User lsp_diagnostics_updated call lightline#update() + " autocmd BufWritePre silent LspDocumentFormatSync + " autocmd CursorHold silent LspHover +augroup END " load internal plugins: runtime macros/matchit.vim -" load ALE only after configured: -packadd! ale - " load other plugins: packadd! tmux-complete.vim packadd! vim-commentary @@ -303,7 +336,5 @@ packadd! vim-commentary " https://github.com/junegunn/fzf/blob/master/plugin/fzf.vim " and also this separate plugin, which is loaded here: packadd! fzf.vim -packadd! vim-go packadd! vim-surround packadd! vim-yaml-folds -packadd! vim-fugitive