From 83104eb86479e2c82cadba1b9b9493cba27ca2b3 Mon Sep 17 00:00:00 2001 From: Andreas Louv Date: Wed, 29 Mar 2023 20:36:26 +0200 Subject: [PATCH] Add support for inline diagnostic highlight --- autoload/lsp/diag.vim | 49 ++++++++++++++++++++++++++++++++++++++++ autoload/lsp/options.vim | 10 ++++++++ doc/lsp.txt | 10 ++++++++ 3 files changed, 69 insertions(+) diff --git a/autoload/lsp/diag.vim b/autoload/lsp/diag.vim index 2f868ed..7a56c5f 100644 --- a/autoload/lsp/diag.vim +++ b/autoload/lsp/diag.vim @@ -19,6 +19,25 @@ export def InitOnce() {name: 'LspDiagHint', text: 'H>', texthl: 'Question', linehl: lineHL}]) + if opt.lspOptions.highlightDiagInline + if !hlexists('LspDiagInlineError') + hlset([{name: 'LspDiagInlineError', linksto: opt.lspOptions.diagInlineErrorHL}]) + endif + if !hlexists('LspDiagInlineWarning') + hlset([{name: 'LspDiagInlineWarning', linksto: opt.lspOptions.diagInlineWarningHL}]) + endif + if !hlexists('LspDiagInlineInfo') + hlset([{name: 'LspDiagInlineInfo', linksto: opt.lspOptions.diagInlineInfoHL}]) + endif + if !hlexists('LspDiagInlineHint') + hlset([{name: 'LspDiagInlineHint', linksto: opt.lspOptions.diagInlineHintHL}]) + endif + prop_type_add('LspDiagInlineError', { highlight: 'LspDiagInlineError', override: true }) + prop_type_add('LspDiagInlineWarning', { highlight: 'LspDiagInlineWarning', override: true }) + prop_type_add('LspDiagInlineInfo', { highlight: 'LspDiagInlineInfo', override: true }) + prop_type_add('LspDiagInlineHint', { highlight: 'LspDiagInlineHint', override: true }) + endif + if opt.lspOptions.showDiagWithVirtualText if !hlexists('LspDiagVirtualText') hlset([{name: 'LspDiagVirtualText', @@ -45,6 +64,19 @@ def DiagSevToSignName(severity: number): string return typeMap[severity - 1] enddef +def DiagSevToInlineHLName(severity: number): string + var typeMap: list = [ + 'LspDiagInlineError', + 'LspDiagInlineWarning', + 'LspDiagInlineInfo', + 'LspDiagInlineHint' + ] + if severity > 4 + return 'LspDiagInlineHint' + endif + return typeMap[severity - 1] +enddef + # Refresh the signs placed in buffer 'bnr' on lines with a diagnostic message. def DiagsRefreshSigns(lspserver: dict, bnr: number) bnr->bufload() @@ -56,6 +88,14 @@ def DiagsRefreshSigns(lspserver: dict, bnr: number) prop_remove({type: 'LspDiagVirtualText', bufnr: bnr, all: true}) endif + if opt.lspOptions.highlightDiagInline + # Remove all the existing virtual text + prop_remove({type: 'LspDiagInlineError', bufnr: bnr, all: true}) + prop_remove({type: 'LspDiagInlineWarning', bufnr: bnr, all: true}) + prop_remove({type: 'LspDiagInlineInfo', bufnr: bnr, all: true}) + prop_remove({type: 'LspDiagInlineHint', bufnr: bnr, all: true}) + endif + if !lspserver.diagsMap->has_key(bnr) || lspserver.diagsMap[bnr].sortedDiagnostics->empty() return @@ -71,6 +111,15 @@ def DiagsRefreshSigns(lspserver: dict, bnr: number) lnum: lnum, name: DiagSevToSignName(diag.severity)}) + if opt.lspOptions.highlightDiagInline + prop_add(diag.range.start.line + 1, + util.GetLineByteFromPos(bnr, diag.range.start) + 1, + {end_lnum: diag.range.end.line + 1, + end_col: util.GetLineByteFromPos(bnr, diag.range.end) + 1, + bufnr: bnr, + type: DiagSevToInlineHLName(diag.severity)}) + endif + if opt.lspOptions.showDiagWithVirtualText prop_add(lnum, 0, {bufnr: bnr, type: 'LspDiagVirtualText', diff --git a/autoload/lsp/options.vim b/autoload/lsp/options.vim index bda616f..09a76be 100644 --- a/autoload/lsp/options.vim +++ b/autoload/lsp/options.vim @@ -40,6 +40,16 @@ export var lspOptions: dict = { showDiagWithVirtualText: false, # The highlight group used for a diagnostics virtual text diagVirtualTextHL: 'LineNr', + # Highlight diagnostics inline + highlightDiagInline: false, + # The highlight group used inline Error diagnostics + diagInlineErrorHL: 'SpellBad', + # The highlight group used inline Warning diagnostics + diagInlineWarningHL: 'SpellCap', + # The highlight group used inline Info diagnostics + diagInlineInfoHL: 'SpellRare', + # The highlight group used inline Hint diagnostics + diagInlineHintHL: 'SpellLocal', # Don't print message when a configured language server is missing. ignoreMissingServer: false, # Use a floating menu to show the code action menu instead of asking for input diff --git a/doc/lsp.txt b/doc/lsp.txt index 639ae12..09b19f6 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -351,6 +351,16 @@ usePopupInCodeAction |Boolean| option. When using the |:LspCodeAction| command to display the code action for the current line, use a popup menu instead of echoing. By default this is set to false. +highlightDiagInline |Boolean| option. Highlight the diagnostics inline + By default this is set to false. +diagInlineErrorHL |String| option. The highlight group used for + inline error highlight. By default uses "SpellBad" +diagInlineWarningHL |String| option. The highlight group used for + inline warning highlight. By default uses "SpellCap" +diagInlineInfoHL |String| option. The highlight group used for + inline info highlight. By default uses "SpellRare" +diagInlineHintHL |String| option. The highlight group used for + inline hint highlight. By default uses "SpellLocal" For example, to disable the automatic placement of signs for the LSP diagnostic messages, you can add the following line to your .vimrc file: > -- 2.48.1