neo_solidity/frontend/
frontend_diagnostics.rs1fn format_diagnostics(source: &str, diagnostics: &[Diagnostic]) -> String {
2 diagnostics
3 .iter()
4 .map(|diag| {
5 if let solang_parser::pt::Loc::File(_, start, _) = diag.loc {
6 let (line, column) = offset_to_line_column(source, start);
7 format!("{}:{}: {}", line, column, diag.message)
8 } else {
9 diag.message.clone()
10 }
11 })
12 .collect::<Vec<_>>()
13 .join("\n")
14}
15
16fn offset_to_line_column(source: &str, offset: usize) -> (usize, usize) {
17 let mut line = 1usize;
18 let mut column = 1usize;
19 let mut current = 0usize;
20
21 for ch in source.chars() {
22 if current >= offset {
23 break;
24 }
25
26 if ch == '\n' {
27 line += 1;
28 column = 1;
29 } else {
30 column += 1;
31 }
32
33 current += ch.len_utf8();
34 }
35
36 (line, column)
37}
38