From be2221b5e7f9ee25e1fb88bdc48a4678a182cb97 Mon Sep 17 00:00:00 2001 From: Andreas Louv Date: Mon, 3 Apr 2023 01:05:26 +0200 Subject: [PATCH] Add ! to ":LspDiagCurrent" to only show the diag if it's under the cursor This is nice to tie together with a "CursorMoved" autocmd: augroup LspCustom au! au CursorMoved * silent! LspDiagCurrent! augroup END --- autoload/lsp/diag.vim | 17 ++++++++++++----- autoload/lsp/lsp.vim | 4 ++-- plugin/lsp.vim | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/autoload/lsp/diag.vim b/autoload/lsp/diag.vim index 5097d2f..cfca785 100644 --- a/autoload/lsp/diag.vim +++ b/autoload/lsp/diag.vim @@ -387,11 +387,11 @@ def DisplayDiag(diag: dict) enddef # Show the diagnostic message for the current line -export def ShowCurrentDiag(lspserver: dict) +export def ShowCurrentDiag(lspserver: dict, atPos: bool) var bnr: number = bufnr() var lnum: number = line('.') var col: number = charcol('.') - var diag: dict = lspserver.getDiagByPos(bnr, lnum, col) + var diag: dict = 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, bnr: number, lnum: number, col: number): dict +export def GetDiagByPos(lspserver: dict, bnr: number, + lnum: number, col: number, + atPos: bool = false): dict 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 diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 39d4ffe..21423b1 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -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 = 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. diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 18b04b4..7648257 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -78,7 +78,7 @@ augroup END # LSP commands command! -nargs=? -bar -range LspCodeAction lsp.CodeAction(, , ) -command! -nargs=0 -bar LspDiagCurrent lsp.LspShowCurrentDiag() +command! -nargs=0 -bar -bang LspDiagCurrent lsp.LspShowCurrentDiag(false) command! -nargs=0 -bar LspDiagFirst lsp.JumpToDiag('first') command! -nargs=0 -bar LspDiagHighlightDisable lsp.DiagHighlightDisable() command! -nargs=0 -bar LspDiagHighlightEnable lsp.DiagHighlightEnable() -- 2.48.1