:LspGotoTypeDef|Go to the type definition of the keyword under cursor
:LspGotoImpl|Go to the implementation of the keyword under cursor
:LspShowSignature|Display the signature of the keyword under cursor
-:LspShowDiagnostics|Display the diagnostics messages from the LSP server for the current buffer
+:LspDiagShow|Display the diagnostics messages from the LSP server for the current buffer
+:LspDiagFirst|Display the first diagnostic message for the current buffer
+:LspDiagNext|Display the next diagnostic message after the current line
+:LspDiagPrev|Display the previous diagnostic message before the current line
+:LspDiagCurrent|Display the diagnostic message for the current line
:LspShowReferences|Display the list of references to the keyword under cursor in a new quickfix list.
:LspHighlight|Highlight all the matches for the keyword under cursor
:LspHighlightClear|Clear all the matches highlighted by :LspHighlight
enddef
# Display the diagnostic messages from the LSP server for the current buffer
+# in a quickfix list
def lsp#showDiagnostics(): void
var ftype = &filetype
if ftype == ''
:copen
enddef
+# Show the diagnostic message for the current line
+def lsp#showCurrentDiag()
+ var ftype = &filetype
+ if ftype == ''
+ return
+ endif
+
+ var lspserver: dict<any> = s:lspGetServer(ftype)
+ if lspserver->empty()
+ ErrMsg('Error: LSP server for "' .. ftype .. '" filetype is not found')
+ return
+ endif
+ if !lspserver.running
+ ErrMsg('Error: LSP server for "' .. ftype .. '" filetype is not running')
+ return
+ endif
+
+ var bnr: number = bufnr()
+ var lnum: number = line('.')
+ var diag: dict<any> = lspserver.getDiagByLine(bnr, lnum)
+ if diag->empty()
+ WarnMsg('No diagnostic messages found for current line')
+ else
+ echo diag.message
+ endif
+enddef
+
+# sort the diaganostics messages for a buffer by line number
+def s:getSortedDiagLines(lspserver: dict<any>, bnr: number): list<number>
+ var lnums: list<number> =
+ lspserver.diagsMap[bnr]->keys()->map((_, v) => str2nr(v))
+ return lnums->sort((a, b) => a - b)
+enddef
+
+def lsp#jumpToDiag(which: string): void
+ var ftype = &filetype
+ if ftype == ''
+ return
+ endif
+
+ var lspserver: dict<any> = s:lspGetServer(ftype)
+ if lspserver->empty()
+ ErrMsg('Error: LSP server for "' .. ftype .. '" filetype is not found')
+ return
+ endif
+ if !lspserver.running
+ ErrMsg('Error: LSP server for "' .. ftype .. '" filetype is not running')
+ return
+ endif
+
+ var fname: string = expand('%:p')
+ if fname == ''
+ return
+ endif
+ var bnr: number = bufnr()
+
+ if !lspserver.diagsMap->has_key(bnr) || lspserver.diagsMap[bnr]->empty()
+ WarnMsg('No diagnostic messages found for ' .. fname)
+ return
+ endif
+
+ # sort the diagnostics by line number
+ var sortedDiags: list<number> = s:getSortedDiagLines(lspserver, bnr)
+
+ if which == 'first'
+ cursor(sortedDiags[0], 1)
+ return
+ endif
+
+ # Find the entry just before the current line (binary search)
+ var curlnum: number = line('.')
+ for lnum in (which == 'next') ? sortedDiags : reverse(sortedDiags)
+ if (which == 'next' && lnum > curlnum)
+ || (which == 'prev' && lnum < curlnum)
+ call cursor(lnum, 1)
+ return
+ endif
+ endfor
+
+ WarnMsg('Error: No more diagnostics found')
+enddef
+
# Insert mode completion handler
def lsp#completeFunc(findstart: number, base: string): any
var ftype: string = &filetype
for item in lspserver.completeItems
res->add(item)
endfor
- return res
+ return res->empty() ? v:none : res
endif
enddef
Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
For Vim version 8.2.2082 and above
-Last change: Dec 30, 2020
+Last change: Jan 10, 2021
==============================================================================
*lsp-license*
:LspGotoTypeDef Go to the type definition of the keyword under cursor
:LspGotoImpl Go to the implementation of the keyword under cursor
:LspShowSignature Display the signature of the keyword under cursor
-:LspShowDiagnostics Display the diagnostics messages from the LSP server for
+:LspDiagShow Display the diagnostics messages from the LSP server for
the current buffer
+:LspDiagFirst Display the first diagnostic message for the current
+ buffer
+:LspDiagNext Display the next diagnostic message for the current
+ buffer after the current line
+:LspDiagPrev Display the previous diagnostic message for the current
+ buffer before the current line
+:LspDiagCurrent Display the diagnostic message for the current line
:LspShowReferences Display the list of references to the keyword under
cursor in a new quickfix list.
:LspHighlight Highlight all the matches for the keyword under cursor
command! -nargs=0 -bar LspGotoTypeDef call lsp#gotoTypedef()
command! -nargs=0 -bar LspGotoImpl call lsp#gotoImplementation()
command! -nargs=0 -bar LspShowSignature call lsp#showSignature()
-command! -nargs=0 -bar LspShowDiagnostics call lsp#showDiagnostics()
+command! -nargs=0 -bar LspDiagShow call lsp#showDiagnostics()
+command! -nargs=0 -bar LspDiagCurrent call lsp#showCurrentDiag()
+command! -nargs=0 -bar LspDiagFirst call lsp#jumpToDiag('first')
+command! -nargs=0 -bar LspDiagNext call lsp#jumpToDiag('next')
+command! -nargs=0 -bar LspDiagPrev call lsp#jumpToDiag('prev')
command! -nargs=0 -bar LspShowReferences call lsp#showReferences()
command! -nargs=0 -bar LspHighlight call lsp#docHighlight()
command! -nargs=0 -bar LspHighlightClear call lsp#docHighlightClear()