From 33b7483db53684cdd0a36f873662f8d6ff163201 Mon Sep 17 00:00:00 2001 From: Robin Jadoul Date: Sun, 17 Apr 2022 05:05:23 +0200 Subject: Send active diagnostics to LSP when requesting code actions. (#2005) * Send active diagnostics to LSP when requesting code actions. This allows for e.g. clangd to properly send the quickfix code actions corresponding to those diagnostics as options. The LSP spec v3.16.0 introduced an opaque `data` member that would allow the server to persist arbitrary data between the diagnostic and the code actions request, but this is not supported yet by this commit. * Reuse existing range_to_lsp_range functionality--- helix-term/src/commands/lsp.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'helix-term') diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 1db57ecf..0a12ae39 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -1,6 +1,6 @@ use helix_lsp::{ block_on, lsp, - util::{lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range}, + util::{diagnostic_to_lsp_diagnostic, lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range}, OffsetEncoding, }; @@ -192,15 +192,29 @@ pub fn code_action(cx: &mut Context) { let language_server = language_server!(cx.editor, doc); - let range = range_to_lsp_range( - doc.text(), - doc.selection(view.id).primary(), - language_server.offset_encoding(), - ); - - let future = language_server.code_actions(doc.identifier(), range); + let selection_range = doc.selection(view.id).primary(); let offset_encoding = language_server.offset_encoding(); + let range = range_to_lsp_range(doc.text(), selection_range, offset_encoding); + + let future = language_server.code_actions( + doc.identifier(), + range, + // Filter and convert overlapping diagnostics + lsp::CodeActionContext { + diagnostics: doc + .diagnostics() + .iter() + .filter(|&diag| { + selection_range + .overlaps(&helix_core::Range::new(diag.range.start, diag.range.end)) + }) + .map(|diag| diagnostic_to_lsp_diagnostic(doc.text(), diag, offset_encoding)) + .collect(), + only: None, + }, + ); + cx.callback( future, move |editor, compositor, response: Option| { -- cgit v1.2.3-70-g09d2