From: Yegappan Lakshmanan Date: Sat, 17 Jun 2023 22:10:07 +0000 (-0700) Subject: Add a function to get all the diagnostics in a buffer X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=8bbcc9790a0cc6f094b972eb2660a489be5fc3f7;p=vim-lsp.git Add a function to get all the diagnostics in a buffer --- diff --git a/autoload/lsp/diag.vim b/autoload/lsp/diag.vim index 97556d7..af73873 100644 --- a/autoload/lsp/diag.vim +++ b/autoload/lsp/diag.vim @@ -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> + if !diagsMap->has_key(bnr) || + diagsMap[bnr].sortedDiagnostics->empty() + return [] + endif + + return diagsMap[bnr].sortedDiagnostics->deepcopy() +enddef + # vim: tabstop=8 shiftwidth=2 softtabstop=2 diff --git a/doc/lsp.txt b/doc/lsp.txt index 55d0995..7bee0a0 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -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* diff --git a/test/clangd_tests.vim b/test/clangd_tests.vim index cc375af..3c88ff0 100644 --- a/test/clangd_tests.vim +++ b/test/clangd_tests.vim @@ -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