]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add support for inline diagnostic highlight
authorAndreas Louv <andreas@louv.dk>
Wed, 29 Mar 2023 18:36:26 +0000 (20:36 +0200)
committerAndreas Louv <andreas@louv.dk>
Wed, 29 Mar 2023 20:31:00 +0000 (22:31 +0200)
autoload/lsp/diag.vim
autoload/lsp/options.vim
doc/lsp.txt

index 2f868ed8147ea30a523b02dd52572947a4411f85..7a56c5ffecb7971301b7be15801aec08ef176f47 100644 (file)
@@ -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<string> = [
+    '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<any>, bnr: number)
   bnr->bufload()
@@ -56,6 +88,14 @@ def DiagsRefreshSigns(lspserver: dict<any>, 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<any>, 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',
index bda616f2e5b375c5a743fc8401caecf720f40c1b..09a76bef33da6adafa073efdcd965019196b2d68 100644 (file)
@@ -40,6 +40,16 @@ export var lspOptions: dict<any> = {
   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
index 639ae12e064b0bd371a68e54d7fb19aed75e5731..09b19f66c2fbd211694b7659e950417064dec63e 100644 (file)
@@ -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: >