Skip to content

Commit a18b55c

Browse files
committed
Add support for diagnostics display funcref for more flexible integration
1 parent 29f8eb2 commit a18b55c

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

doc/LanguageClient.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,58 @@ The default value for this config, or the absence of this config, enables extens
629629

630630
Default: v:null
631631

632+
<<<<<<< HEAD
632633
2.41 g:LanguageClient_codeLensHighlightGroup *g:LanguageClient_codeLensHighlightGroup*
633634

634635
Highlight group to be used for code lens.
635636

636637
Default: 'Comment'
637638

639+
2.42 g:LanguageClient_diagnosticsDisplayFuncref *g:LanguageClient_diagnosticsDisplayFuncref*
640+
641+
If set, LanguageClient-neovim will call this function instead of setting the diagnostics signs. This
642+
is useful to delegate the display of diagnostics to other engines. The function is called with two
643+
arguments, the first one is the file name of which the diagnostics correspond to, and the seconds one
644+
is the list of diagnostics for said file. Those diagnostics are as specified in the LSP specification.
645+
646+
For example, if you wanted to use `dense-analysis/ale` to display diagnostics instead of this plugin
647+
you could use something like this:
648+
649+
```
650+
function! g:DisplayDiagnostics(filename, diagnostics) abort
651+
let s:diagnostics = []
652+
653+
for d in a:diagnostics
654+
let s:severity = 'I'
655+
if d.severity == 1
656+
let s:severity = 'E'
657+
elseif d.severity == 2
658+
let s:severity = 'W'
659+
endif
660+
661+
call add(s:diagnostics, {
662+
\ "filename": a:filename,
663+
\ "text": d.message,
664+
\ "lnum": d.range.start.line + 1,
665+
\ "end_lnum": d.range.end.line + 1,
666+
\ "col": d.range.end.character,
667+
\ "end_col": d.range.end.character,
668+
\ "type": s:severity,
669+
\ })
670+
endfor
671+
672+
call ale#other_source#ShowResults(bufnr('%'), 'LanguageClientNeovim', s:diagnostics)
673+
endfunction
674+
675+
let g:LanguageClient_diagnosticsDisplayFuncref = 'g:DisplayDiagnostics'
676+
```
677+
678+
Keep in mind that to complete the integration between `ale` and `LanguageClient-neovim` you need to
679+
add `LanguageClientNeovim` (or the name of the linter you used in the call to ShowResults) to the list
680+
of linters to be used in `ale`.
681+
682+
Default: v:null
683+
638684
==============================================================================
639685
3. Commands *LanguageClientCommands*
640686

src/language_server_protocol.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl LanguageClient {
165165

166166
#[allow(clippy::type_complexity)]
167167
let (
168+
diagnostics_display_funcref,
168169
diagnostics_signs_max,
169170
diagnostics_max_severity,
170171
diagnostics_ignore_sources,
@@ -180,6 +181,7 @@ impl LanguageClient {
180181
enable_extensions,
181182
code_lens_hl_group,
182183
): (
184+
Option<String>,
183185
Option<usize>,
184186
String,
185187
Vec<String>,
@@ -196,6 +198,7 @@ impl LanguageClient {
196198
String,
197199
) = self.vim()?.eval(
198200
[
201+
"get(g:, 'LanguageClient_diagnosticsDisplayFuncref', v:null)",
199202
"get(g:, 'LanguageClient_diagnosticsSignsMax', v:null)",
200203
"get(g:, 'LanguageClient_diagnosticsMaxSeverity', 'Hint')",
201204
"get(g:, 'LanguageClient_diagnosticsIgnoreSources', [])",
@@ -328,6 +331,8 @@ impl LanguageClient {
328331
state.preferred_markup_kind = preferred_markup_kind;
329332
state.enable_extensions = enable_extensions;
330333
state.code_lens_hl_group = code_lens_hl_group;
334+
state.diagnostics_display_funcref = diagnostics_display_funcref;
335+
331336
Ok(())
332337
})?;
333338

@@ -2470,6 +2475,14 @@ impl LanguageClient {
24702475
}
24712476
}
24722477

2478+
// if a diagnostics display funcref has been configured then call that function and return
2479+
if let Some(funcref) = self.get(|state| state.diagnostics_display_funcref.clone())? {
2480+
self.vim()?
2481+
.rpcclient
2482+
.notify(funcref, json!([filename, diagnostics]))?;
2483+
return Ok(());
2484+
}
2485+
24732486
let current_filename: String = self.vim()?.get_filename(&Value::Null)?;
24742487
if filename != current_filename.canonicalize() {
24752488
return Ok(());

src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub struct State {
211211
pub hide_virtual_texts_on_insert: bool,
212212
pub echo_project_root: bool,
213213
pub enable_extensions: Option<HashMap<String, bool>>,
214+
pub diagnostics_display_funcref: Option<String>,
214215

215216
pub server_stderr: Option<String>,
216217
pub logger: Logger,
@@ -296,6 +297,7 @@ impl State {
296297
preferred_markup_kind: None,
297298
enable_extensions: None,
298299
code_lens_hl_group: "Comment".into(),
300+
diagnostics_display_funcref: None,
299301

300302
logger,
301303
})

0 commit comments

Comments
 (0)