Skip to content

Commit 2dd003d

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

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

doc/LanguageClient.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,51 @@ Highlight group to be used for code lens.
635635

636636
Default: 'Comment'
637637

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

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)