Skip to main content

ron_schema/
diagnostic.rs

1/*************************
2 * Author: Bradley Hunter
3 */
4
5use crate::span::{Span};
6
7
8
9/// Extracted source line with highlight positions for rendering error underlines.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct SourceLine {
12    /// 1-based line number.
13    pub line_number: usize,
14    /// The full source line with trailing newline trimmed.
15    pub line_text: String,
16    /// Column where the underline starts (0-based, for spacing).
17    pub highlight_start: usize,
18    /// Column where the underline ends (0-based).
19    pub highlight_end: usize,
20}
21
22/// Extracts a source line and highlight positions from the original source text for a given span.
23///
24/// For spans that cross multiple lines, the highlight extends to the end of the first line.
25#[must_use] 
26pub fn extract_source_line(source: &str, span: Span) -> SourceLine {
27    let mut line_start = span.start.offset;
28    while line_start > 0 && source.as_bytes()[line_start - 1] != b'\n' {
29        line_start -= 1;
30    }
31
32    let mut line_end = span.start.offset;
33    while line_end < source.len() && source.as_bytes()[line_end] != b'\n' {
34        line_end += 1;
35    }
36
37    let line_text = source[line_start..line_end].to_string();
38
39    let highlight_start = span.start.offset - line_start;
40    let highlight_end = if span.end.line == span.start.line {
41        span.end.offset - line_start
42    } else {
43        line_end - line_start
44    };
45
46    SourceLine { line_number: span.start.line, line_text, highlight_start, highlight_end }
47    
48}