-
Notifications
You must be signed in to change notification settings - Fork 528
/
location_marks.rs
115 lines (97 loc) · 4.02 KB
/
location_marks.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::cmp::max;
use std::sync::Arc;
use cairo_lang_filesystem::span::{FileSummary, TextPosition, TextSpan, TextWidth};
use itertools::repeat_n;
use crate::DiagnosticLocation;
#[cfg(test)]
#[path = "location_marks_test.rs"]
mod test;
/// Given a single line diagnostic location, returns a string with the location marks.
pub fn get_single_line_location_marks(
db: &dyn cairo_lang_filesystem::db::FilesGroup,
location: &DiagnosticLocation,
) -> String {
// TODO(ilya, 10/10/2023): Handle locations which spread over a few lines.
let content = db.file_content(location.file_id).expect("File missing from DB.");
let summary = db.file_summary(location.file_id).expect("File missing from DB.");
let span = &location.span;
let TextPosition { line: first_line_idx, col } = span
.start
.position_in_file(db, location.file_id)
.expect("Failed to find location in file.");
let first_line_start = summary.line_offsets[first_line_idx];
let first_line_end = match summary.line_offsets.get(first_line_idx + 1) {
Some(offset) => offset.sub_width(TextWidth::from_char('\n')),
None => summary.last_offset,
};
let first_line_span = TextSpan { start: first_line_start, end: first_line_end };
let mut res = first_line_span.take(&content).to_string();
res.push('\n');
res.extend(repeat_n(' ', col));
let subspan_in_first_line =
TextSpan { start: span.start, end: std::cmp::min(first_line_end, span.end) };
let marker_length = subspan_in_first_line.n_chars(&content);
println!("marker_length: {}", marker_length);
// marker_length can be 0 if the span is empty.
res.extend(repeat_n('^', max(marker_length, 1)));
res
}
/// Given a multiple lines diagnostic location, returns a string with the location marks.
pub fn get_multiple_lines_location_marks(
db: &dyn cairo_lang_filesystem::db::FilesGroup,
location: &DiagnosticLocation,
skip_middle_lines: bool,
) -> String {
let content = db.file_content(location.file_id).expect("File missing from DB.");
let summary = db.file_summary(location.file_id).expect("File missing from DB.");
let span = &location.span;
let TextPosition { line: first_line_idx, col } = span
.start
.position_in_file(db, location.file_id)
.expect("Failed to find location in file.");
let mut res = get_line_content(summary.clone(), first_line_idx, content.clone(), true);
res += " _";
res.extend(repeat_n('_', col));
res += "^\n";
let TextPosition { line: last_line_idx, col: _ } =
span.end.position_in_file(db, location.file_id).expect("Failed to find location in file.");
if !skip_middle_lines {
for row_index in first_line_idx + 1..=last_line_idx - 1 {
res += &get_line_content(summary.clone(), row_index, content.clone(), false);
}
} else {
res += "| ...\n";
}
res += &get_line_content(summary.clone(), last_line_idx, content.clone(), false);
let last_line_start = summary.line_offsets[last_line_idx];
let last_line_end = match summary.line_offsets.get(last_line_idx + 1) {
Some(offset) => offset.sub_width(TextWidth::from_char('\n')),
None => summary.last_offset,
};
let line_span = TextSpan { start: last_line_start, end: last_line_end };
let last_line_len = line_span.n_chars(&content);
res += "|";
res.extend(repeat_n('_', last_line_len));
res.push('^');
res
}
fn get_line_content(
summary: Arc<FileSummary>,
row_index: usize,
content: Arc<str>,
first_line: bool,
) -> String {
let line_start = summary.line_offsets[row_index];
let line_end = match summary.line_offsets.get(row_index + 1) {
Some(offset) => offset.sub_width(TextWidth::from_char('\n')),
None => summary.last_offset,
};
let line_span = TextSpan { start: line_start, end: line_end };
let mut res = match first_line {
true => " ".to_string(),
false => "| ".to_string(),
};
res += line_span.take(&content);
res.push('\n');
res
}