diff --git a/crates/analyzer/src/lib.rs b/crates/analyzer/src/lib.rs index 7602f4c..0fa9f39 100644 --- a/crates/analyzer/src/lib.rs +++ b/crates/analyzer/src/lib.rs @@ -93,12 +93,12 @@ fn clean_lsp_url(path: &std::path::Path) -> Option { } fn lsp_pos_to_rust_map(pos: &Position) -> RustMap { - RustMap(pos.line as usize + 1, pos.character as usize) + RustMap(pos.line as usize, pos.character as usize) } fn mist_map_to_lsp_pos(map: &MistMap) -> Position { Position { - line: map.0.saturating_sub(1) as u32, + line: map.0 as u32, character: map.1 as u32, } } @@ -391,7 +391,7 @@ impl Backend { // Fallback: the source has a syntax error that prevents transpilation. // Use the last known working Rust content + mapping to inject the // marker at the nearest valid Rust position. - let mist_target = MistMap(line as usize + 1, character as usize); + let mist_target = MistMap(line as usize, character as usize); let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") { rust_path.set_file_name("mod.rs"); @@ -417,7 +417,7 @@ impl Backend { .map(|(rust, _)| *rust); let (modified_rust, rust_pos) = if let Some(next_rust) = rust_next { - let lsp_line = (next_rust.0 - 1) as u32; + let lsp_line = next_rust.0 as u32; let lsp_col = next_rust.1 as u32; let modified = inject_marker_at(&last_content, lsp_line, lsp_col, &marker)?; let pos = find_marker_position(&modified, &marker)?; @@ -1869,7 +1869,7 @@ async fn handle_ra_notifications( character: if mist_end_pos.line == mist_start_pos.line { mist_end_pos.character.max(mist_start_pos.character + 1) } else { - mist_end_pos.character.max(1) + mist_end_pos.character.max(0) }, }; diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index d2d3a3b..dc7e142 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -46,7 +46,7 @@ impl RustCodegen { indent: 0, crates: HashMap::new(), mapping: Mapping::new(mist_path), - position: RustMap(1, 0), + position: RustMap(0, 0), } } @@ -186,7 +186,7 @@ impl GenSpanTranslation for Spanned { fn gen_span(&self, cg: &mut RustCodegen) { cg.mapping .map - .insert((cg.position, MistMap(self.line, self.column))); + .insert((cg.position, MistMap(self.line, self.column - 1))); } } diff --git a/crates/mist-api/src/builder.mist b/crates/mist-api/src/builder.mist index 3445058..dae8dab 100644 --- a/crates/mist-api/src/builder.mist +++ b/crates/mist-api/src/builder.mist @@ -58,28 +58,36 @@ pub bool build(Vec mut args, PathBuf root) serde_json::from_str(&map_str).expect("Failed to parse map file") }); let mist_file = map.mist_path.strip_prefix(&root); - let (_, rev_mapper::MistMap(line, column)) = map.find(&rev_mapper::RustMap(span.line_end, span.column_start)).unwrap_or_default(); - let mist_msg = MistDiagnosticMessage { - message: format!("{}: {}", - msg.message.message, - span.label.clone().unwrap_or_default()), - file_name: if let Ok(mist_file) = mist_file { - mist_file.to_string_lossy().to_string() - } else { - map.mist_path.to_string_lossy().to_string() - }, - file_path: map.mist_path.clone(), - line: line, - column: column, - }; - match msg.message.level { - cargo_metadata::diagnostic::DiagnosticLevel::Error => { - diagnostics.push(MistDiagnostic::Error(mist_msg)) + let rust_lookup = rev_mapper::RustMap(span.line_end - 1, span.column_start - 1); + match map.find(&rust_lookup) { + Some((_, rev_mapper::MistMap(line, column))) => { + let mist_msg = MistDiagnosticMessage { + message: format!("{}: {}", + msg.message.message, + span.label.clone().unwrap_or_default()), + file_name: if let Ok(mist_file) = mist_file { + mist_file.to_string_lossy().to_string() + } else { + map.mist_path.to_string_lossy().to_string() + }, + file_path: map.mist_path.clone(), + line: line, + column: column, + }; + match msg.message.level { + cargo_metadata::diagnostic::DiagnosticLevel::Error => { + diagnostics.push(MistDiagnostic::Error(mist_msg)) + } + cargo_metadata::diagnostic::DiagnosticLevel::Warning => { + diagnostics.push(MistDiagnostic::Warning(mist_msg)) + } + _ => { + } + } } - cargo_metadata::diagnostic::DiagnosticLevel::Warning => { - diagnostics.push(MistDiagnostic::Warning(mist_msg)) - } - _ => { + None => { + diagnostics.push(MistDiagnostic::Rust(msg)); + break; } } } @@ -110,8 +118,8 @@ pub void print_diagnostics(Vec& diagnostics) let line = get_line(&mut files, &msg); println!("\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}", msg.file_name, - msg.line, - msg.column, + msg.line + 1, + msg.column + 1, msg.message, line.unwrap_or_default(),) } @@ -119,8 +127,8 @@ pub void print_diagnostics(Vec& diagnostics) let line = get_line(&mut files, &msg); println!("\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}", msg.file_name, - msg.line, - msg.column, + msg.line + 1, + msg.column + 1, msg.message, line.unwrap_or_default(),) } @@ -135,5 +143,5 @@ pub Option get_line(HashMap> mut& files, MistDiagno let lines = files.entry(src_path.clone()).or_insert_with(() => { fs::read_to_string(src_path).expect("Unable to read mist file").lines().into_iter().map(String::from).collect() }); - lines.get(msg.line - 1).map((v) => v.trim().to_string()) + lines.get(msg.line).map((v) => v.trim().to_string()) } diff --git a/crates/parser/src/rev_mapper.rs b/crates/parser/src/rev_mapper.rs index 2b75e2f..b15f62a 100644 --- a/crates/parser/src/rev_mapper.rs +++ b/crates/parser/src/rev_mapper.rs @@ -3,12 +3,12 @@ use std::{collections::HashSet, path::PathBuf}; use serde::{Deserialize, Serialize}; #[derive( - Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, + Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, )] pub struct MistMap(pub usize, pub usize); #[derive( - Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, + Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, )] pub struct RustMap(pub usize, pub usize);