]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add ! to ":LspDiagCurrent" to only show the diag if it's under the cursor
authorAndreas Louv <andreas@louv.dk>
Sun, 2 Apr 2023 23:05:26 +0000 (01:05 +0200)
committerAndreas Louv <andreas@louv.dk>
Sun, 2 Apr 2023 23:40:53 +0000 (01:40 +0200)
This is nice to tie together with a "CursorMoved" autocmd:

augroup LspCustom
  au!
  au CursorMoved * silent! LspDiagCurrent!
augroup END

autoload/lsp/diag.vim
autoload/lsp/lsp.vim
plugin/lsp.vim

index 5097d2f21ee43012ad1c1e2e95b17f7d1944aa12..cfca785a3f09a72f2063d21df547f104a9d629a9 100644 (file)
@@ -387,11 +387,11 @@ def DisplayDiag(diag: dict<any>)
 enddef
 
 # Show the diagnostic message for the current line
-export def ShowCurrentDiag(lspserver: dict<any>)
+export def ShowCurrentDiag(lspserver: dict<any>, atPos: bool)
   var bnr: number = bufnr()
   var lnum: number = line('.')
   var col: number = charcol('.')
-  var diag: dict<any> = lspserver.getDiagByPos(bnr, lnum, col)
+  var diag: dict<any> = lspserver.getDiagByPos(bnr, lnum, col, atPos)
   if diag->empty()
     util.WarnMsg('No diagnostic messages found for current line')
   else
@@ -421,16 +421,23 @@ enddef
 
 # Get the diagnostic from the LSP server for a particular line and character
 # offset in a file
-export def GetDiagByPos(lspserver: dict<any>, bnr: number, lnum: number, col: number): dict<any>
+export def GetDiagByPos(lspserver: dict<any>, bnr: number,
+                       lnum: number, col: number,
+                       atPos: bool = false): dict<any>
   var diags_in_line = GetDiagsByLine(lspserver, bnr, lnum)
 
   for diag in diags_in_line
-    if col <= diag.range.start.character + 1
+    if atPos
+      if col >= diag.range.start.character + 1 && col < diag.range.end.character + 1
+        return diag
+      endif
+    elseif col <= diag.range.start.character + 1
       return diag
     endif
   endfor
 
-  if diags_in_line->len() > 0
+  # No diagnostic to the right of the position, return the last one instead
+  if !atPos && diags_in_line->len() > 0
     return diags_in_line[-1]
   endif
 
index 39d4ffe61a969c03b5f0654e634eb32cbb85fd8e..21423b19b7847aecc64f3066872a7886d9604f03 100644 (file)
@@ -555,13 +555,13 @@ export def ShowDiagnostics(): void
 enddef
 
 # Show the diagnostic message for the current line
-export def LspShowCurrentDiag()
+export def LspShowCurrentDiag(atPos: bool)
   var lspserver: dict<any> = buf.CurbufGetServerChecked()
   if lspserver->empty()
     return
   endif
 
-  diag.ShowCurrentDiag(lspserver)
+  diag.ShowCurrentDiag(lspserver, atPos)
 enddef
 
 # Display the diagnostics for the current line in the status line.
index 18b04b4cc54a53eaf9e7694f3415e551a8c4d1c6..7648257b78851ca1a2e29dda969312cdf1250d33 100644 (file)
@@ -78,7 +78,7 @@ augroup END
 
 # LSP commands
 command! -nargs=? -bar -range LspCodeAction lsp.CodeAction(<line1>, <line2>, <q-args>)
-command! -nargs=0 -bar LspDiagCurrent lsp.LspShowCurrentDiag()
+command! -nargs=0 -bar -bang LspDiagCurrent lsp.LspShowCurrentDiag(<bang>false)
 command! -nargs=0 -bar LspDiagFirst lsp.JumpToDiag('first')
 command! -nargs=0 -bar LspDiagHighlightDisable lsp.DiagHighlightDisable()
 command! -nargs=0 -bar LspDiagHighlightEnable lsp.DiagHighlightEnable()