Skip to main content

veryl_analyzer/
multi_sources.rs

1#[derive(Debug, PartialEq, Eq)]
2pub struct Source {
3    pub path: String,
4    pub text: String,
5}
6
7#[derive(Debug, PartialEq, Eq)]
8pub struct MultiSources {
9    pub sources: Vec<Source>,
10}
11
12impl miette::SourceCode for MultiSources {
13    fn read_span<'a>(
14        &'a self,
15        span: &miette::SourceSpan,
16        context_lines_before: usize,
17        context_lines_after: usize,
18    ) -> Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
19        let mut start = 0;
20        let mut code = None;
21        let mut header = None;
22        for source in &self.sources {
23            if span.offset() < start + source.text.len() {
24                code = Some(&source.text);
25                header = Some(&source.path);
26                break;
27            }
28            start += source.text.len();
29        }
30
31        let code = code.unwrap();
32        let header = header.unwrap();
33
34        let local_span = &(span.offset() - start, span.len()).into();
35        let local = code.read_span(local_span, context_lines_before, context_lines_after)?;
36
37        let local_span = local.span();
38        let span = (local_span.offset() + start, local_span.len()).into();
39
40        Ok(Box::new(miette::MietteSpanContents::new_named(
41            header.to_owned(),
42            local.data(),
43            span,
44            local.line(),
45            local.column(),
46            local.line_count(),
47        )))
48    }
49}