]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add a function to get all the diagnostics in a buffer
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 17 Jun 2023 22:10:07 +0000 (15:10 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 17 Jun 2023 22:13:55 +0000 (15:13 -0700)
autoload/lsp/diag.vim
doc/lsp.txt
test/clangd_tests.vim

index 97556d7c88c4c5e371b459628ed2826b198e52d3..af738734b0e8c866610b131b0c8c7a3e0f2ed4af 100644 (file)
@@ -246,7 +246,7 @@ def SendAleDiags(bnr: number, timerid: number)
     return
   endif
 
-  # Conver to Ale's diagnostics format (:h ale-loclist-format)
+  # Convert to Ale's diagnostics format (:h ale-loclist-format)
   ale#other_source#ShowResults(bnr, 'lsp', diagsMap[bnr].sortedDiagnostics->mapnew((_, v) => {
      return {text: v.message,
              lnum: v.range.start.line + 1,
@@ -729,4 +729,16 @@ export def DiagsHighlightEnable()
   endfor
 enddef
 
+# Return the sorted diagnostics for buffer "bnr".  Default is the current
+# buffer.  A copy of the diagnostics is returned so that the caller can modify
+# the diagnostics.
+export def GetDiagsForBuf(bnr: number = bufnr()): list<dict<any>>
+  if !diagsMap->has_key(bnr) ||
+      diagsMap[bnr].sortedDiagnostics->empty()
+    return []
+  endif
+
+  return diagsMap[bnr].sortedDiagnostics->deepcopy()
+enddef
+
 # vim: tabstop=8 shiftwidth=2 softtabstop=2
index 55d0995b695d8512b35295d1d75a3d744097e6a2..7bee0a06eb532ff02ef013c51a92f60e717d875d 100644 (file)
@@ -1204,6 +1204,12 @@ the current line in a popup window.  To display the message in the status
 message area instead, you can set the 'showDiagInPopup' option to false.  By
 default this is set to true.
 
+The lsp#diag#GetDiagsForBuf() function can be used to get all the LSP
+diagnostics in a buffer.  This function optionally accepts a buffer number.
+If the buffer number argument is not specified, then the current buffer is
+used.  This function returns a |List| of diagnostics sorted by their line and
+column number.  Each diagnostic is a |Dict| returned by the language server.
+
 ==============================================================================
 8. Tag Function                                        *lsp-tagfunc*
 
index cc375afc1dd622454ac2cbb56569ec014a80e178..3c88ff0a9bf306a13d192a9b9d6af71a5eafbf48 100644 (file)
@@ -383,6 +383,9 @@ def g:Test_DiagLocListAutoUpdate()
   var bnr = bufnr()
   g:WaitForServerFileLoad(1)
   :redraw!
+  var d = lsp#diag#GetDiagsForBuf()[0]
+  assert_equal({start: {line: 0, character: 5}, end: {line: 0, character: 6}},
+              d.range)
 
   :LspDiagShow
   assert_equal(1, line('$'))
@@ -390,12 +393,18 @@ def g:Test_DiagLocListAutoUpdate()
   setline(2, 'int j:')
   redraw!
   g:WaitForDiags(2)
+  var l = lsp#diag#GetDiagsForBuf()
+  assert_equal({start: {line: 0, character: 5}, end: {line: 0, character: 6}},
+              l[0].range)
+  assert_equal({start: {line: 1, character: 5}, end: {line: 1, character: 6}},
+              l[1].range)
   wincmd w
   assert_equal(2, line('$'))
   wincmd w
   deletebufline('', 1, '$')
   redraw!
   g:WaitForDiags(0)
+  assert_equal([], lsp#diag#GetDiagsForBuf())
   wincmd w
   assert_equal([''], getline(1, '$'))
   :lclose