From d7db9e42ce6360de902b57c72f1e15bbc927c846 Mon Sep 17 00:00:00 2001 From: kohnish Date: Sun, 16 Jan 2022 19:33:54 +0100 Subject: [PATCH] Optional vim-lsc like behaviours - Option to show diagnostics on status line - Option to not suppress the diag hover appearing on mouse hover - Make outline configurable - Option to suppress adding a new line on completion selection --- autoload/diag.vim | 19 +++++++++++++++++++ autoload/lsp.vim | 27 ++++++++++++++++++++++++--- autoload/lspoptions.vim | 10 ++++++++++ autoload/outline.vim | 11 ++++++++++- doc/lsp.txt | 23 +++++++++++++++++------ plugin/lsp.vim | 38 ++++++++++++++++++++++++-------------- 6 files changed, 104 insertions(+), 24 deletions(-) diff --git a/autoload/diag.vim b/autoload/diag.vim index 687c486..b10ce4d 100644 --- a/autoload/diag.vim +++ b/autoload/diag.vim @@ -190,6 +190,25 @@ export def ShowCurrentDiag(lspserver: dict) endif enddef +# Show the diagnostic message for the current line without linebreak +export def ShowCurrentDiagInStatusLine(lspserver: dict) + var bnr: number = bufnr() + var lnum: number = line('.') + var diag: dict = lspserver.getDiagByLine(bnr, lnum) + if !diag->empty() + # 15 is a enough length not to cause line break + var max_width = &columns - 15 + var code = "" + if has_key(diag, 'code') + code = "[" .. diag.code .. "] " + endif + var msgNoLineBreak = code .. substitute(substitute(diag.message, "\n", " ", ""), "\\n", " ", "") + echo msgNoLineBreak[ : max_width] + else + echo "" + endif +enddef + # Get the diagnostic from the LSP server for a particular line in a file export def GetDiagByLine(lspserver: dict, bnr: number, lnum: number): dict if lspserver.diagsMap->has_key(bnr) && diff --git a/autoload/lsp.vim b/autoload/lsp.vim index cdb6594..d6591f7 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -35,6 +35,7 @@ if has('patch-8.2.4019') diag.DiagsGetErrorCount = diag_import.DiagsGetErrorCount diag.ShowAllDiags = diag_import.ShowAllDiags diag.ShowCurrentDiag = diag_import.ShowCurrentDiag + diag.ShowCurrentDiagInStatusLine = diag_import.ShowCurrentDiagInStatusLine diag.LspDiagsJump = diag_import.LspDiagsJump diag.DiagRemoveFile = diag_import.DiagRemoveFile symbol.ShowSymbolMenu = symbolsearch_import.ShowSymbolMenu @@ -54,6 +55,7 @@ else DiagsGetErrorCount, ShowAllDiags, ShowCurrentDiag, + ShowCurrentDiagInStatusLine, LspDiagsJump} from './diag.vim' import ShowSymbolMenu from './symbolsearch.vim' import {OpenOutlineWindow, SkipOutlineRefresh} from './outline.vim' @@ -72,6 +74,7 @@ else diag.DiagsGetErrorCount = DiagsGetErrorCount diag.ShowAllDiags = ShowAllDiags diag.ShowCurrentDiag = ShowCurrentDiag + diag.ShowCurrentDiagInStatusLine = ShowCurrentDiagInStatusLine diag.LspDiagsJump = LspDiagsJump symbol.ShowSymbolMenu = ShowSymbolMenu outline.OpenOutlineWindow = OpenOutlineWindow @@ -307,8 +310,10 @@ def g:LspDiagExpr(): string # Display the diagnostic message only if the mouse is over the first two # columns - if v:beval_col >= 3 - return '' + if opt.lspOptions.noDiagHoverOnLine + if v:beval_col >= 3 + return '' + endif endif return diagInfo.message @@ -364,7 +369,9 @@ def lsp#addFile(bnr: number): void setbufvar(bnr, '&completeopt', 'menuone,popup,noinsert,noselect') setbufvar(bnr, '&completepopup', 'border:off') # in insert mode stops completion and inserts a - inoremap pumvisible() ? "\\" : "\" + if !opt.lspOptions.noNewlineInCompletion + inoremap pumvisible() ? "\\" : "\" + endif else if s:lspOmniComplEnabled(ftype) setbufvar(bnr, '&omnifunc', 'lsp#omniFunc') @@ -548,6 +555,20 @@ def lsp#showCurrentDiag() diag.ShowCurrentDiag(lspserver) enddef +def lsp#showCurrentDiagInStatusLine() + var ftype = &filetype + if ftype == '' || @% == '' + return + endif + + var lspserver: dict = s:lspGetServer(ftype) + if lspserver->empty() || !lspserver.running + return + endif + + diag.ShowCurrentDiagInStatusLine(lspserver) +enddef + # get the count of error in the current buffer def lsp#errorCount(): dict var res = {'Error': 0, 'Warn': 0, 'Info': 0, 'Hint': 0} diff --git a/autoload/lspoptions.vim b/autoload/lspoptions.vim index 9b93bae..bada2f8 100644 --- a/autoload/lspoptions.vim +++ b/autoload/lspoptions.vim @@ -19,6 +19,16 @@ export var lspOptions: dict = { hoverInPreview: false, # Focus on the location list window after LspShowReferences keepFocusInReferences: false, + # Suppress adding a new line on completion selection with + noNewlineInCompletion: false, + # Outline window size + outlineWinSize: 20, + # Open outline window on right side + outlineOnRight: false, + # Suppress diagnostic hover from appearing when the mouse is over the line instead of the signature + noDiagHoverOnLine: true, + # Show a diagnostic message on a status line + showDiagOnStatusLine: false, } # set LSP options from user provided options diff --git a/autoload/outline.vim b/autoload/outline.vim index 53021ee..30a591d 100644 --- a/autoload/outline.vim +++ b/autoload/outline.vim @@ -1,12 +1,17 @@ vim9script var util = {} +var opt = {} if has('patch-8.2.4019') import './util.vim' as util_import + import './lspoptions.vim' as opt_import util.GetLineByteFromPos = util_import.GetLineByteFromPos + opt.lspOptions = opt_import.lspOptions else import GetLineByteFromPos from './util.vim' + import lspOptions from './lspoptions.vim' util.GetLineByteFromPos = GetLineByteFromPos + opt.lspOptions = lspOptions endif # jump to a symbol selected in the outline window @@ -234,7 +239,11 @@ export def OpenOutlineWindow() var prevWinID: number = win_getid() - :topleft :20vnew LSP-Outline + if opt.lspOptions.outlineOnRight + execute ':botright :' .. opt.lspOptions.outlineWinSize .. 'vnew LSP-Outline' + else + execute ':topleft :' .. opt.lspOptions.outlineWinSize .. 'vnew LSP-Outline' + endif :setlocal modifiable :setlocal noreadonly :silent! :%d _ diff --git a/doc/lsp.txt b/doc/lsp.txt index e147d59..de26c20 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -188,21 +188,32 @@ Some of the LSP plugin features can be enabled or disabled by using the lsp#setOptions() function. This function accepts a dictionary argument with the following optional items: - autoComplete In insert mode, automatically complete the current +autoComplete In insert mode, automatically complete the current symbol. Otherwise use omni-completion. By default this is set to true. - autoHighlight In normal mode, automatically highlight all the +autoHighlight In normal mode, automatically highlight all the occurrences of the symbol under the cursor. By default this is set to false. - showSignature In insert mode, automatically show the current symbol +showSignature In insert mode, automatically show the current symbol signature in a popup. By default this is set to true. - echoSignature In insert mode, echo the current symbol signature +echoSignature In insert mode, echo the current symbol signature instead of showing it in a popup. By default this is set to false. - autoHighlightDiags Automatically place signs on the lines with a +autoHighlightDiags Automatically place signs on the lines with a diagnostic message from the LSP server. By default this is set to true. - keepFocusInReferences Focus on the location list window after LspShowReferences +keepFocusInReferences Focus on the location list window after LspShowReferences. + By default this is set to false. +noNewlineInCompletion Suppress adding a new line on completion selection with . + By default this is set to false. +outlineWinSize Outline window size, by default this is 20. +outlineOnRight Open the outline window on the right side, by default + this is false. +noDiagHoverOnLine Suppress diagnostic hover from appearing when + the mouse is over the line instead of the signature. + By default this is set to true +showDiagOnStatusLine Show a diagnostic message on a status line. + By default this is set to false For example, to disable the automatic placement of signs for the LSP diagnostic messages, you can add the following line to your .vimrc file: diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 8e85806..f039632 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -1,23 +1,33 @@ -" LSP plugin for vim9 - -" Needs Vim 8.2.2342 and higher if v:version < 802 || !has('patch-8.2.2342') finish endif +vim9script +# LSP plugin for vim9 + +var opt = {} +if has('patch-8.2.4019') + import '../autoload/lspoptions.vim' as opt_import + opt.lspOptions = opt_import.lspOptions +else + import lspOptions from '../autoload/lspoptions.vim' + opt.lspOptions = lspOptions +endif + augroup LSPAutoCmds au! - autocmd BufNewFile,BufReadPost * - \ call lsp#addFile(expand('') + 0) - autocmd BufWipeOut * - \ call lsp#removeFile(expand('') + 0) + autocmd BufNewFile,BufReadPost * lsp#addFile(bufnr('%')) + autocmd BufWipeOut * lsp#removeFile(bufnr('%')) + if opt.lspOptions.showDiagOnStatusLine + autocmd CursorMoved * lsp#showCurrentDiagInStatusLine() + endif augroup END -" TODO: Is it needed to shutdown all the LSP servers when exiting Vim? -" This takes some time. -" autocmd VimLeavePre * call lsp#stopAllServers() +# TODO: Is it needed to shutdown all the LSP servers when exiting Vim? +# This takes some time. +# autocmd VimLeavePre * call lsp#stopAllServers() -" LSP commands +# LSP commands command! -nargs=0 -bar LspShowServers call lsp#showServers() command! -nargs=1 -bar LspSetTrace call lsp#setTraceServer() command! -nargs=0 -bar LspGotoDefinition call lsp#gotoDefinition(v:false) @@ -52,7 +62,7 @@ command! -nargs=0 -bar LspWorkspaceListFolders call lsp#listWorkspaceFolders() command! -nargs=1 -bar -complete=dir LspWorkspaceAddFolder call lsp#addWorkspaceFolder() command! -nargs=1 -bar -complete=dir LspWorkspaceRemoveFolder call lsp#removeWorkspaceFolder() -" Add the GUI menu entries +# Add the GUI menu entries if has('gui_running') anoremenu L&sp.Goto.Definition :call lsp#gotoDefinition(v:false) anoremenu L&sp.Goto.Declaration :call lsp#gotoDeclaration(v:false) @@ -73,7 +83,7 @@ if has('gui_running') anoremenu L&sp.Highlight\ Symbol :call lsp#docHighlight() anoremenu L&sp.Highlight\ Clear :call lsp#docHighlightClear() - " Diagnostics + # Diagnostics anoremenu L&sp.Diagnostics.Current :call lsp#showCurrentDiag anoremenu L&sp.Diagnostics.Show\ All :call lsp#showDiagnostics() anoremenu L&sp.Diagnostics.First :call lsp#jumpToDiag('first') @@ -96,4 +106,4 @@ if has('gui_running') endif endif -" vim: shiftwidth=2 softtabstop=2 +# vim: shiftwidth=2 softtabstop=2 -- 2.48.1