|
1 | 1 | use super::annotation::Annotation;
|
2 |
| -use super::line::{DisplayLine, DisplayMark, DisplayMarkType, DisplayRawLine, DisplaySourceLine}; |
3 |
| -use crate::{Slice, Snippet, SourceAnnotation}; |
| 2 | +use super::line::{ |
| 3 | + DisplayContentElement, DisplayLine, DisplayMark, DisplayMarkType, DisplayRawLine, |
| 4 | + DisplaySourceLine, |
| 5 | +}; |
| 6 | +use crate::{InlineAnnotation, Slice, Snippet, SourceAnnotation}; |
4 | 7 |
|
5 | 8 | #[derive(Debug, Clone)]
|
6 | 9 | pub struct DisplayList<'d> {
|
@@ -55,15 +58,17 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
|
55 | 58 | });
|
56 | 59 |
|
57 | 60 | let mut annotations: Vec<&SourceAnnotation> = slice.annotations.iter().collect();
|
| 61 | + let mut inline_annotations: Vec<&InlineAnnotation> = |
| 62 | + slice.inline_annotations.iter().collect(); |
58 | 63 |
|
59 |
| - // let mut current_annotation = annotations.next(); |
60 | 64 | let mut line_start_pos = 0;
|
61 | 65 |
|
62 | 66 | let mut i = slice.line_start.unwrap_or(1);
|
63 | 67 | for line in slice.source.lines() {
|
64 | 68 | let line_range = line_start_pos..(line_start_pos + line.chars().count() + 1);
|
65 | 69 |
|
66 | 70 | let mut current_annotations = vec![];
|
| 71 | + let mut current_inline_annotations = vec![]; |
67 | 72 | let mut inline_marks = vec![];
|
68 | 73 |
|
69 | 74 | annotations.retain(|ann| {
|
@@ -100,10 +105,41 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
|
100 | 105 | }
|
101 | 106 | });
|
102 | 107 |
|
| 108 | + inline_annotations.retain(|ann| { |
| 109 | + if line_range.contains(&ann.range.start) && line_range.contains(&ann.range.end) { |
| 110 | + // Annotation in this line |
| 111 | + current_inline_annotations.push(*ann); |
| 112 | + false |
| 113 | + } else { |
| 114 | + true |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + let mut frag = vec![]; |
| 119 | + |
| 120 | + let mut ptr = 0; |
| 121 | + for ann in current_inline_annotations { |
| 122 | + if ptr < ann.range.start { |
| 123 | + frag.push(DisplayContentElement::Text( |
| 124 | + &line[ptr..ann.range.start - line_range.start], |
| 125 | + )); |
| 126 | + } |
| 127 | + frag.push(DisplayContentElement::AnnotatedText { |
| 128 | + text: &line |
| 129 | + [(ann.range.start - line_range.start)..(ann.range.end - line_range.start)], |
| 130 | + annotation_type: ann.annotation_type.clone(), |
| 131 | + }); |
| 132 | + ptr = ann.range.end - line_range.start; |
| 133 | + } |
| 134 | + |
| 135 | + if ptr < line_range.end { |
| 136 | + frag.push(DisplayContentElement::Text(&line[ptr..])); |
| 137 | + } |
| 138 | + |
103 | 139 | body.push(DisplayLine::Source {
|
104 | 140 | lineno: Some(i),
|
105 | 141 | inline_marks,
|
106 |
| - line: DisplaySourceLine::Content { text: line }, |
| 142 | + line: DisplaySourceLine::Content(frag), |
107 | 143 | });
|
108 | 144 | for ann in current_annotations {
|
109 | 145 | let start = if ann.range.start >= line_start_pos {
|
|
0 commit comments