From 87c282d8f1cb53cda9b5c3deda52501bb0104103 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Mon, 8 Apr 2024 17:54:06 +0200 Subject: [PATCH] Send along diagnostics when requesting code actions (#10281) This fixes #10177 by sending along the correct diagnostics when querying the language server for diagnostics for a given cursor location. Turns out that `gopls` takes the `range`, `source`, `message` of the diagnostics sent along to check whether it has any code actions for the given location. Release Notes: - Fixed "quickfix" code actions that were based on diagnostics not showing up in Go files. ([#10177](https://github.com/zed-industries/zed/issues/10177)). Co-authored-by: Conrad Co-authored-by: Marshall --- crates/language/src/diagnostic_set.rs | 9 +++++++-- crates/project/src/lsp_command.rs | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/language/src/diagnostic_set.rs b/crates/language/src/diagnostic_set.rs index 5d85fbc2903850f1e9436420ba8f5f15b65182a4..ea35bc184c8c1e8f0be276675c85a6a59e2bdd00 100644 --- a/crates/language/src/diagnostic_set.rs +++ b/crates/language/src/diagnostic_set.rs @@ -1,4 +1,4 @@ -use crate::Diagnostic; +use crate::{range_to_lsp, Diagnostic}; use collections::HashMap; use lsp::LanguageServerId; use std::{ @@ -51,7 +51,7 @@ pub struct Summary { count: usize, } -impl DiagnosticEntry { +impl DiagnosticEntry { /// Returns a raw LSP diagnostic ssed to provide diagnostic context to LSP /// codeAction request pub fn to_lsp_diagnostic_stub(&self) -> lsp::Diagnostic { @@ -61,9 +61,14 @@ impl DiagnosticEntry { .clone() .map(lsp::NumberOrString::String); + let range = range_to_lsp(self.range.clone()); + lsp::Diagnostic { code, + range, severity: Some(self.diagnostic.severity), + source: self.diagnostic.source.clone(), + message: self.diagnostic.message.clone(), ..Default::default() } } diff --git a/crates/project/src/lsp_command.rs b/crates/project/src/lsp_command.rs index 043503bf0d9ec4aca7ace0a67cba3a06b5b0cc2d..38df25378a0d5d82039b6c733e26cc2d75d7aada 100644 --- a/crates/project/src/lsp_command.rs +++ b/crates/project/src/lsp_command.rs @@ -1699,9 +1699,10 @@ impl LspCommand for GetCodeActions { ) -> lsp::CodeActionParams { let relevant_diagnostics = buffer .snapshot() - .diagnostics_in_range::<_, usize>(self.range.clone(), false) + .diagnostics_in_range::<_, language::PointUtf16>(self.range.clone(), false) .map(|entry| entry.to_lsp_diagnostic_stub()) - .collect(); + .collect::>(); + lsp::CodeActionParams { text_document: lsp::TextDocumentIdentifier::new( lsp::Url::from_file_path(path).unwrap(),