From 5a6a2573e1972ad4af5d0d336dc75ae3cf4a10d2 Mon Sep 17 00:00:00 2001 From: berggeist <24396556+berggeist@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:19:46 +0200 Subject: [PATCH] Adds option to align diagnostics virtual text --- autoload/lsp/diag.vim | 45 ++++++++++++++++++++++++++++++++++++++-- autoload/lsp/options.vim | 3 +++ doc/lsp.txt | 5 +++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/autoload/lsp/diag.vim b/autoload/lsp/diag.vim index 1d28a40..dd1b422 100644 --- a/autoload/lsp/diag.vim +++ b/autoload/lsp/diag.vim @@ -116,6 +116,19 @@ def DiagSevToInlineHLName(severity: number): string return typeMap[severity - 1] enddef +def DiagSevToSymbolText(severity: number): string + var typeMap: list = [ + opt.lspOptions.diagSignErrorText, + opt.lspOptions.diagSignWarningText, + opt.lspOptions.diagSignInfoText, + opt.lspOptions.diagSignHintText + ] + if severity > 4 + return opt.lspOptions.diagSignHintText + endif + return typeMap[severity - 1] +enddef + # Refresh the placed diagnostics in buffer 'bnr' # This inline signs, inline props, and virtual text diagnostics def DiagsRefresh(bnr: number) @@ -141,6 +154,24 @@ def DiagsRefresh(bnr: number) return endif + # Initialize default/fallback properties for diagnostic virtual text: + var diag_align: string = 'above' + var diag_wrap: string = 'truncate' + var diag_symbol: string = '┌─' + var diag_padding: number = 0 + + if opt.lspOptions.diagVirtualTextAlign ==? 'below' + diag_align = 'below' + diag_wrap = 'truncate' + diag_symbol = '└─' + diag_padding = 0 + elseif opt.lspOptions.diagVirtualTextAlign ==? 'after' + diag_align = 'after' + diag_wrap = 'wrap' + diag_symbol = 'E>' + diag_padding = 3 + endif + var signs: list> = [] var diags: list> = diagsMap[bnr].sortedDiagnostics for diag in diags @@ -162,14 +193,24 @@ def DiagsRefresh(bnr: number) endif if opt.lspOptions.showDiagWithVirtualText + var padding = diag.range.start.character if padding > 0 padding = strdisplaywidth(getline(diag.range.start.line + 1)[ : diag.range.start.character - 1]) endif + + var symbol = diag_symbol + + if diag_align ==? 'after' + padding = diag_padding + symbol = DiagSevToSymbolText(diag.severity) + endif + prop_add(lnum, 0, {bufnr: bnr, type: 'LspDiagVirtualText', - text: $'┌─ {diag.message}', - text_align: 'above', + text: $'{symbol} {diag.message}', + text_align: diag_align, + text_wrap: diag_wrap, text_padding_left: padding}) endif catch /E966\|E964/ # Invalid lnum | Invalid col diff --git a/autoload/lsp/options.vim b/autoload/lsp/options.vim index 3a6e753..ef708ee 100644 --- a/autoload/lsp/options.vim +++ b/autoload/lsp/options.vim @@ -37,6 +37,9 @@ export var lspOptions: dict = { # If a snippet plugin is going to apply the text edits, then set this to # false to avoid applying the text edits twice. completionTextEdit: true, + # Alignment of virtual diagnostic text, when showDiagWithVirtualText is true + # Allowed values: 'above' | 'below' | 'after' (default is 'above') + diagVirtualTextAlign: 'above', # instead of the signature noDiagHoverOnLine: true, # Suppress adding a new line on completion selection with diff --git a/doc/lsp.txt b/doc/lsp.txt index c472668..0446b1d 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -416,6 +416,11 @@ diagSignInfoText |String| option. Change diag sign text for info *lsp-opt-diagSignWarningText* diagSignWarningText |String| option. Change diag sign text for warnings By default 'W>', + *lsp-opt-diagVirtualTextAlign* +diagVirtualTextAlign |String| option. Alignment of diagnostics messages + if |lsp-opt-showDiagWithVirtualText| is set to true. + Allowed values are 'above', 'below' or 'after' + By default this is set to 'above', *lsp-opt-echoSignature* echoSignature |Boolean| option. In insert mode, echo the current symbol signature instead of showing it in a popup. -- 2.50.0