diff --git a/kclvm/error/src/diagnostic.rs b/kclvm/error/src/diagnostic.rs index 6c8034c2c..d2a203d6b 100644 --- a/kclvm/error/src/diagnostic.rs +++ b/kclvm/error/src/diagnostic.rs @@ -5,23 +5,11 @@ use std::hash::Hash; use crate::{ErrorKind, WarningKind}; /// Diagnostic structure. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Diagnostic { pub level: Level, pub messages: Vec, pub code: Option, - pub data: Option, -} - -impl Hash for Diagnostic { - fn hash(&self, state: &mut H) { - self.level.hash(state); - for message in &self.messages { - message.hash(state); - } - self.code.hash(state); - // The `data` field is not included in the hash calculation due to complexity. - } } /// Position describes an arbitrary source position including the filename, @@ -120,10 +108,6 @@ impl Diagnostic { code: Option, suggestions: Option>, ) -> Self { - let data = suggestions.clone().map(|suggs| { - serde_json::json!({ "suggested_replacements": suggs }) - }); - // println!("Data received: {:?}", data); Diagnostic { level, messages: vec![Message { @@ -131,10 +115,9 @@ impl Diagnostic { style: Style::LineAndColumn, message: message.to_string(), note: note.map(String::from), - suggested_replacement: None, // Assuming your Message struct has such a field + suggested_replacement: suggestions.and_then(|v| v.into_iter().next()), }], code, - data, // Now includes suggestions if provided } } diff --git a/kclvm/error/src/lib.rs b/kclvm/error/src/lib.rs index 1109b6330..f805c3fcd 100644 --- a/kclvm/error/src/lib.rs +++ b/kclvm/error/src/lib.rs @@ -179,7 +179,6 @@ impl Handler { level: Level::Error, messages: msgs.to_owned(), code: Some(DiagnosticId::Error(err)), - data: None, }; self.add_diagnostic(diag); @@ -198,7 +197,6 @@ impl Handler { suggested_replacement: None, }], code: Some(DiagnosticId::Suggestions), - data: None, }); }); @@ -224,7 +222,6 @@ impl Handler { level: Level::Warning, messages: msgs.to_owned(), code: Some(DiagnosticId::Warning(warning)), - data: None, }; self.add_diagnostic(diag); diff --git a/kclvm/tools/src/LSP/src/quick_fix.rs b/kclvm/tools/src/LSP/src/quick_fix.rs index 821345956..a46ba5408 100644 --- a/kclvm/tools/src/LSP/src/quick_fix.rs +++ b/kclvm/tools/src/LSP/src/quick_fix.rs @@ -14,7 +14,7 @@ pub(crate) fn quick_fix(uri: &Url, diags: &Vec) -> Vec match error { ErrorKind::CompileError => { - let replacement_text = extract_suggested_replacements(&diag.data).unwrap_or_else(|| "".to_string()); + let replacement_text = extract_suggested_replacement(&diag.data).unwrap_or_else(|| "".to_string()); let mut changes = HashMap::new(); changes.insert( uri.clone(), @@ -87,11 +87,11 @@ pub(crate) fn quick_fix(uri: &Url, diags: &Vec) -> Vec) -> Option { +fn extract_suggested_replacement(data: &Option) -> Option { data.as_ref().and_then(|data| { match data { Value::Object(obj) => { - obj.get("suggested_replacements").and_then(|val| { + obj.get("suggested_replacement").and_then(|val| { match val { Value::String(s) => Some(s.clone()), Value::Array(arr) if !arr.is_empty() => arr.iter().filter_map(|v| v.as_str()).next().map(String::from), diff --git a/kclvm/tools/src/LSP/src/to_lsp.rs b/kclvm/tools/src/LSP/src/to_lsp.rs index d13778ff8..168b0e165 100644 --- a/kclvm/tools/src/LSP/src/to_lsp.rs +++ b/kclvm/tools/src/LSP/src/to_lsp.rs @@ -5,6 +5,7 @@ use kclvm_error::Message; use kclvm_error::Position as KCLPos; use lsp_types::*; use ra_ap_vfs::FileId; +use serde_json::json; use crate::state::LanguageServerSnapshot; use std::{ @@ -43,6 +44,10 @@ fn kcl_msg_to_lsp_diags( let start_position = lsp_pos(&range.0); let end_position = lsp_pos(&range.1); + let data = msg.suggested_replacement.as_ref().map(|s| { + json!({ "suggested_replacement": [s] }) + }); + let related_information = if related_msg.is_empty() { None } else { @@ -72,7 +77,7 @@ fn kcl_msg_to_lsp_diags( message: msg.message.clone(), related_information, tags: None, - data: None, + data: data, } } @@ -97,8 +102,6 @@ pub fn kcl_diag_to_lsp_diags(diag: &KCLDiagnostic, file_name: &str) -> Vec Vec