Skip to content

Commit

Permalink
Add support for diagnostics display funcref for more flexible integra…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
martskins committed Oct 9, 2020
1 parent 29f8eb2 commit a18b55c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
46 changes: 46 additions & 0 deletions doc/LanguageClient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -629,12 +629,58 @@ The default value for this config, or the absence of this config, enables extens

Default: v:null

<<<<<<< HEAD
2.41 g:LanguageClient_codeLensHighlightGroup *g:LanguageClient_codeLensHighlightGroup*

Highlight group to be used for code lens.

Default: 'Comment'

2.42 g:LanguageClient_diagnosticsDisplayFuncref *g:LanguageClient_diagnosticsDisplayFuncref*

If set, LanguageClient-neovim will call this function instead of setting the diagnostics signs. This
is useful to delegate the display of diagnostics to other engines. The function is called with two
arguments, the first one is the file name of which the diagnostics correspond to, and the seconds one
is the list of diagnostics for said file. Those diagnostics are as specified in the LSP specification.

For example, if you wanted to use `dense-analysis/ale` to display diagnostics instead of this plugin
you could use something like this:

```
function! g:DisplayDiagnostics(filename, diagnostics) abort
let s:diagnostics = []

for d in a:diagnostics
let s:severity = 'I'
if d.severity == 1
let s:severity = 'E'
elseif d.severity == 2
let s:severity = 'W'
endif

call add(s:diagnostics, {
\ "filename": a:filename,
\ "text": d.message,
\ "lnum": d.range.start.line + 1,
\ "end_lnum": d.range.end.line + 1,
\ "col": d.range.end.character,
\ "end_col": d.range.end.character,
\ "type": s:severity,
\ })
endfor

call ale#other_source#ShowResults(bufnr('%'), 'LanguageClientNeovim', s:diagnostics)
endfunction

let g:LanguageClient_diagnosticsDisplayFuncref = 'g:DisplayDiagnostics'
```

Keep in mind that to complete the integration between `ale` and `LanguageClient-neovim` you need to
add `LanguageClientNeovim` (or the name of the linter you used in the call to ShowResults) to the list
of linters to be used in `ale`.

Default: v:null

==============================================================================
3. Commands *LanguageClientCommands*

Expand Down
13 changes: 13 additions & 0 deletions src/language_server_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl LanguageClient {

#[allow(clippy::type_complexity)]
let (
diagnostics_display_funcref,
diagnostics_signs_max,
diagnostics_max_severity,
diagnostics_ignore_sources,
Expand All @@ -180,6 +181,7 @@ impl LanguageClient {
enable_extensions,
code_lens_hl_group,
): (
Option<String>,
Option<usize>,
String,
Vec<String>,
Expand All @@ -196,6 +198,7 @@ impl LanguageClient {
String,
) = self.vim()?.eval(
[
"get(g:, 'LanguageClient_diagnosticsDisplayFuncref', v:null)",
"get(g:, 'LanguageClient_diagnosticsSignsMax', v:null)",
"get(g:, 'LanguageClient_diagnosticsMaxSeverity', 'Hint')",
"get(g:, 'LanguageClient_diagnosticsIgnoreSources', [])",
Expand Down Expand Up @@ -328,6 +331,8 @@ impl LanguageClient {
state.preferred_markup_kind = preferred_markup_kind;
state.enable_extensions = enable_extensions;
state.code_lens_hl_group = code_lens_hl_group;
state.diagnostics_display_funcref = diagnostics_display_funcref;

Ok(())
})?;

Expand Down Expand Up @@ -2470,6 +2475,14 @@ impl LanguageClient {
}
}

// if a diagnostics display funcref has been configured then call that function and return
if let Some(funcref) = self.get(|state| state.diagnostics_display_funcref.clone())? {
self.vim()?
.rpcclient
.notify(funcref, json!([filename, diagnostics]))?;
return Ok(());
}

let current_filename: String = self.vim()?.get_filename(&Value::Null)?;
if filename != current_filename.canonicalize() {
return Ok(());
Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub struct State {
pub hide_virtual_texts_on_insert: bool,
pub echo_project_root: bool,
pub enable_extensions: Option<HashMap<String, bool>>,
pub diagnostics_display_funcref: Option<String>,

pub server_stderr: Option<String>,
pub logger: Logger,
Expand Down Expand Up @@ -296,6 +297,7 @@ impl State {
preferred_markup_kind: None,
enable_extensions: None,
code_lens_hl_group: "Comment".into(),
diagnostics_display_funcref: None,

logger,
})
Expand Down

0 comments on commit a18b55c

Please sign in to comment.