Skip to content

Commit

Permalink
internal: adpat moonc removing offset in diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash authored and lijunchen committed Sep 5, 2024
1 parent 5fc1e8e commit 71e6e30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
6 changes: 3 additions & 3 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
50 changes: 34 additions & 16 deletions crates/moonutil/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 71e6e30

Please sign in to comment.