diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index 619bef43b..826a61ca9 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -5348,8 +5348,8 @@ fn test_moon_check_json_output() { check( &get_stdout_with_args_and_replace_dir(&dir, ["check", "--output-json", "-q"]), expect![[r#" - {"$message_type":"diagnostic","level":"warning","loc":{"path":"$ROOT/main/main.mbt","start":{"line":3,"col":3,"offset":25},"end":{"line":3,"col":10,"offset":32}},"message":"Warning (Alert alert_2): alert_2","error_code":2000} - "#]], + {"$message_type":"diagnostic","level":"warning","loc":{"path":"$ROOT/main/main.mbt","start":{"line":3,"col":3},"end":{"line":3,"col":10}},"message":"Warning (Alert alert_2): alert_2","error_code":2000} + "#]], ); check( &get_stderr_on_success_with_args_and_replace_dir( @@ -5372,7 +5372,7 @@ fn test_moon_check_json_output() { check( &get_stdout_with_args_and_replace_dir(&dir, ["check", "--output-json", "-q"]), expect![[r#" - {"$message_type":"diagnostic","level":"warning","loc":{"path":"$ROOT/main/main.mbt","start":{"line":3,"col":3,"offset":27},"end":{"line":3,"col":10,"offset":34}},"message":"Warning (Alert alert_2): alert_2","error_code":2000} + {"$message_type":"diagnostic","level":"warning","loc":{"path":"$ROOT/main/main.mbt","start":{"line":3,"col":3},"end":{"line":3,"col":10}},"message":"Warning (Alert alert_2): alert_2","error_code":2000} "#]], ); check( diff --git a/crates/moonutil/src/render.rs b/crates/moonutil/src/render.rs index abcf796cf..2748358f8 100644 --- a/crates/moonutil/src/render.rs +++ b/crates/moonutil/src/render.rs @@ -39,7 +39,29 @@ pub struct Location { pub struct Position { pub line: usize, pub col: usize, - pub offset: isize, +} + +impl Position { + pub fn calculate_offset(&self, content: &str) -> usize { + // line and col count from 1 + let mut current_line = 1; + let mut current_col = 1; + + for (char_idx, (_, c)) in content.char_indices().enumerate() { + if current_line == self.line && current_col == self.col { + return char_idx; + } + + if c == '\n' { + current_line += 1; + current_col = 1; + } else { + current_col += 1; + } + } + + content.chars().count() + } } impl MooncDiagnostic { @@ -74,21 +96,17 @@ impl MooncDiagnostic { } }; - let mut report_builder = ariadne::Report::build( - kind, - source_file_path, - diagnostic.location.start.offset as usize, - ) - .with_message(format!("[{}]", diagnostic.error_code).fg(color)) - .with_label( - ariadne::Label::new(( - source_file_path, - diagnostic.location.start.offset as usize - ..diagnostic.location.end.offset as usize, - )) - .with_message((&diagnostic.message).fg(color)) - .with_color(color), - ); + let start_offset = diagnostic.location.start.calculate_offset(&source_file); + let end_offset = diagnostic.location.end.calculate_offset(&source_file); + + let mut report_builder = + ariadne::Report::build(kind, source_file_path, start_offset) + .with_message(format!("[{}]", diagnostic.error_code).fg(color)) + .with_label( + ariadne::Label::new((source_file_path, start_offset..end_offset)) + .with_message((&diagnostic.message).fg(color)) + .with_color(color), + ); if !use_fancy { let config = ariadne::Config::default().with_color(false);