Skip to content

Commit

Permalink
removed data field from KCL diagnostics
Browse files Browse the repository at this point in the history
Signed-off-by: Shashank Mittal <[email protected]>
  • Loading branch information
shashank-iitbhu committed Feb 13, 2024
1 parent 82d140d commit c5ce405
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 34 deletions.
21 changes: 2 additions & 19 deletions kclvm/error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message>,
pub code: Option<DiagnosticId>,
pub data: Option<serde_json::Value>,
}

impl Hash for Diagnostic {
fn hash<H: std::hash::Hasher>(&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,
Expand Down Expand Up @@ -120,21 +108,16 @@ impl Diagnostic {
code: Option<DiagnosticId>,
suggestions: Option<Vec<String>>,
) -> Self {
let data = suggestions.clone().map(|suggs| {
serde_json::json!({ "suggested_replacements": suggs })
});
// println!("Data received: {:?}", data);
Diagnostic {
level,
messages: vec![Message {
range,
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
}
}

Expand Down
3 changes: 0 additions & 3 deletions kclvm/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ impl Handler {
level: Level::Error,
messages: msgs.to_owned(),
code: Some(DiagnosticId::Error(err)),
data: None,
};
self.add_diagnostic(diag);

Expand All @@ -198,7 +197,6 @@ impl Handler {
suggested_replacement: None,
}],
code: Some(DiagnosticId::Suggestions),
data: None,
});
});

Expand All @@ -224,7 +222,6 @@ impl Handler {
level: Level::Warning,
messages: msgs.to_owned(),
code: Some(DiagnosticId::Warning(warning)),
data: None,
};
self.add_diagnostic(diag);

Expand Down
6 changes: 3 additions & 3 deletions kclvm/tools/src/LSP/src/quick_fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn quick_fix(uri: &Url, diags: &Vec<Diagnostic>) -> Vec<lsp_types::Co
match id {
DiagnosticId::Error(error) => 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(),
Expand Down Expand Up @@ -87,11 +87,11 @@ pub(crate) fn quick_fix(uri: &Url, diags: &Vec<Diagnostic>) -> Vec<lsp_types::Co
code_actions
}

fn extract_suggested_replacements(data: &Option<Value>) -> Option<String> {
fn extract_suggested_replacement(data: &Option<Value>) -> Option<String> {
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),
Expand Down
16 changes: 7 additions & 9 deletions kclvm/tools/src/LSP/src/to_lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -72,7 +77,7 @@ fn kcl_msg_to_lsp_diags(
message: msg.message.clone(),
related_information,
tags: None,
data: None,
data: data,
}
}

Expand All @@ -97,8 +102,6 @@ pub fn kcl_diag_to_lsp_diags(diag: &KCLDiagnostic, file_name: &str) -> Vec<Diagn
} else {
None
};

let data = diag.data.clone();

let lsp_diag = kcl_msg_to_lsp_diags(
msg,
Expand All @@ -107,12 +110,7 @@ pub fn kcl_diag_to_lsp_diags(diag: &KCLDiagnostic, file_name: &str) -> Vec<Diagn
code,
);

let lsp_diag_with_data = Diagnostic {
data,
..lsp_diag
};

diags.push(lsp_diag_with_data);
diags.push(lsp_diag);
}
}
diags
Expand Down

0 comments on commit c5ce405

Please sign in to comment.