]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Optional vim-lsc like behaviours
authorkohnish <kohnish@gmx.com>
Sun, 16 Jan 2022 18:33:54 +0000 (19:33 +0100)
committerkohnish <kohnish@gmx.com>
Sun, 16 Jan 2022 22:25:53 +0000 (23:25 +0100)
 - 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
autoload/lsp.vim
autoload/lspoptions.vim
autoload/outline.vim
doc/lsp.txt
plugin/lsp.vim

index 687c4865384bb99380899c8a50f741bdc85c7952..b10ce4d0159f0c0ac8fa3292950267f8f4b1fd88 100644 (file)
@@ -190,6 +190,25 @@ export def ShowCurrentDiag(lspserver: dict<any>)
   endif
 enddef
 
+# Show the diagnostic message for the current line without linebreak
+export def ShowCurrentDiagInStatusLine(lspserver: dict<any>)
+  var bnr: number = bufnr()
+  var lnum: number = line('.')
+  var diag: dict<any> = 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<any>, bnr: number, lnum: number): dict<any>
   if lspserver.diagsMap->has_key(bnr) &&
index cdb65941e31687d50bd14d9736fa27da61ba5451..d6591f768fe16085113bbc1b5163ecfd58728800 100644 (file)
@@ -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')
     # <Enter> in insert mode stops completion and inserts a <Enter>
-    inoremap <expr> <buffer> <CR> pumvisible() ? "\<C-Y>\<CR>" : "\<CR>"
+    if !opt.lspOptions.noNewlineInCompletion
+      inoremap <expr> <buffer> <CR> pumvisible() ? "\<C-Y>\<CR>" : "\<CR>"
+    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<any> = 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<number>
   var res = {'Error': 0, 'Warn': 0, 'Info': 0, 'Hint': 0}
index 9b93bae49c2db091240ad806bdd514c6224c9b7d..bada2f840930d5dd046605f9f8b7409d215708e2 100644 (file)
@@ -19,6 +19,16 @@ export var lspOptions: dict<any> = {
   hoverInPreview: false,
   # Focus on the location list window after LspShowReferences
   keepFocusInReferences: false,
+  # Suppress adding a new line on completion selection with <CR>
+  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
index 53021ee96994fd839a43116abd7949fb7b108086..30a591d2481fd5e82b2c1dc9f41faf56e0ee5905 100644 (file)
@@ -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 _
index e147d591110b752cea7857c731d51e1b3e30a8eb..de26c203f6e3d032b1bcce482513508332c48c29 100644 (file)
@@ -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 <CR>.
+                       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:
index 8e85806fdd9309b3a822d432ee0c8f9417ac840a..f039632bacf057203c040e12f6fe9ff940cf1b42 100644 (file)
@@ -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('<abuf>') + 0)
-  autocmd BufWipeOut *
-                         \ call lsp#removeFile(expand('<abuf>') + 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(<q-args>)
 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(<q-args>)
 command! -nargs=1 -bar -complete=dir LspWorkspaceRemoveFolder call lsp#removeWorkspaceFolder(<q-args>)
 
-" Add the GUI menu entries
+# Add the GUI menu entries
 if has('gui_running')
   anoremenu <silent> L&sp.Goto.Definition :call lsp#gotoDefinition(v:false)<CR>
   anoremenu <silent> L&sp.Goto.Declaration :call lsp#gotoDeclaration(v:false)<CR>
@@ -73,7 +83,7 @@ if has('gui_running')
   anoremenu <silent> L&sp.Highlight\ Symbol :call lsp#docHighlight()<CR>
   anoremenu <silent> L&sp.Highlight\ Clear :call lsp#docHighlightClear()<CR>
 
-  " Diagnostics
+  # Diagnostics
   anoremenu <silent> L&sp.Diagnostics.Current :call lsp#showCurrentDiag<CR>
   anoremenu <silent> L&sp.Diagnostics.Show\ All :call lsp#showDiagnostics()<CR>
   anoremenu <silent> L&sp.Diagnostics.First :call lsp#jumpToDiag('first')<CR>
@@ -96,4 +106,4 @@ if has('gui_running')
   endif
 endif
 
-" vim: shiftwidth=2 softtabstop=2
+# vim: shiftwidth=2 softtabstop=2