Skip to content

Commit 28ed51b

Browse files
committed
Handle all features
1 parent 3b921bc commit 28ed51b

File tree

8 files changed

+113
-200
lines changed

8 files changed

+113
-200
lines changed

benches/simple.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ extern crate criterion;
44
use criterion::black_box;
55
use criterion::Criterion;
66

7-
use std::fmt::Write;
8-
97
use annotate_snippets::DisplayList;
108
use annotate_snippets::{Annotation, AnnotationType, SourceAnnotation};
119
use annotate_snippets::{Slice, Snippet};
1210

11+
use annotate_snippets::renderers::ascii_default::styles::plain::Style as PlainStyle;
12+
use annotate_snippets::renderers::ascii_default::Renderer as AsciiRenderer;
13+
1314
const SOURCE: &'static str = r#") -> Option<String> {
1415
for ann in annotations {
1516
match (ann.range.0, ann.range.1) {
@@ -59,9 +60,10 @@ fn create_snippet() {
5960
],
6061
}],
6162
};
63+
let r = AsciiRenderer::<PlainStyle>::new();
6264
let dl: DisplayList = (&snippet).into();
63-
let mut result = String::new();
64-
write!(result, "{}", dl).unwrap();
65+
let mut result: Vec<u8> = Vec::new();
66+
r.fmt(&mut result, &dl).unwrap();
6567
}
6668

6769
pub fn criterion_benchmark(c: &mut Criterion) {

src/display_list/annotation.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::annotation::AnnotationType;
2-
use crate::styles::Stylesheet;
32
use std::fmt;
43

54
#[derive(Debug, Clone)]
@@ -9,17 +8,6 @@ pub struct Annotation<'d> {
98
pub label: &'d str,
109
}
1110

12-
impl<'d> Annotation<'d> {
13-
pub fn fmt_with_style(
14-
&self,
15-
f: &mut fmt::Formatter<'_>,
16-
_style: &impl Stylesheet,
17-
) -> fmt::Result {
18-
// style.format(f, &self.annotation_type, self.label)
19-
f.write_str(self.label)
20-
}
21-
}
22-
2311
impl fmt::Display for AnnotationType {
2412
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2513
match self {

src/display_list/line.rs

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fmt::Write;
33

44
use super::annotation::Annotation;
55
use crate::annotation::AnnotationType;
6-
use crate::styles::{StyleClass, Stylesheet};
76

87
#[derive(Debug, Clone)]
98
pub enum DisplayLine<'d> {
@@ -15,39 +14,6 @@ pub enum DisplayLine<'d> {
1514
Raw(DisplayRawLine<'d>),
1615
}
1716

18-
impl<'d> DisplayLine<'d> {
19-
pub fn fmt_with_style(
20-
&self,
21-
f: &mut fmt::Formatter<'_>,
22-
style: &impl Stylesheet,
23-
lineno_max: Option<usize>,
24-
inline_marks_width: usize,
25-
) -> fmt::Result {
26-
let lineno_max = lineno_max.unwrap_or(1);
27-
match self {
28-
Self::Source {
29-
lineno,
30-
inline_marks,
31-
line,
32-
} => {
33-
if let Some(lineno) = lineno {
34-
write!(f, "{:>1$}", lineno, lineno_max)?;
35-
} else {
36-
write!(f, "{:>1$}", "", lineno_max)?;
37-
}
38-
f.write_str(" | ")?;
39-
write!(f, "{:>1$}", "", inline_marks_width - inline_marks.len())?;
40-
for mark in inline_marks {
41-
write!(f, "{}", mark)?;
42-
}
43-
line.fmt_with_style(f, style)?;
44-
f.write_char('\n')
45-
}
46-
Self::Raw(dl) => dl.fmt_with_style(f, style, lineno_max),
47-
}
48-
}
49-
}
50-
5117
#[derive(Debug, Clone)]
5218
pub enum DisplaySourceLine<'d> {
5319
Content {
@@ -60,32 +26,6 @@ pub enum DisplaySourceLine<'d> {
6026
Empty,
6127
}
6228

63-
impl<'d> DisplaySourceLine<'d> {
64-
fn fmt_with_style(&self, f: &mut fmt::Formatter<'_>, style: &impl Stylesheet) -> fmt::Result {
65-
match self {
66-
Self::Content { text } => {
67-
f.write_char(' ')?;
68-
f.write_str(text)
69-
}
70-
Self::Annotation {
71-
annotation,
72-
range: (start, end),
73-
} => {
74-
let indent = if start == &0 { 0 } else { start + 1 };
75-
write!(f, "{:>1$}", "", indent)?;
76-
if start == &0 {
77-
write!(f, "{:_>1$}", "^", end - start + 1)?;
78-
} else {
79-
write!(f, "{:->1$}", "", end - start)?;
80-
}
81-
f.write_char(' ')?;
82-
annotation.fmt_with_style(f, style)
83-
}
84-
Self::Empty => Ok(()),
85-
}
86-
}
87-
}
88-
8929
#[derive(Debug, Clone)]
9030
pub enum DisplayRawLine<'d> {
9131
Origin {
@@ -99,40 +39,6 @@ pub enum DisplayRawLine<'d> {
9939
},
10040
}
10141

102-
impl<'d> DisplayRawLine<'d> {
103-
fn fmt_with_style(
104-
&self,
105-
f: &mut fmt::Formatter<'_>,
106-
style: &impl Stylesheet,
107-
lineno_max: usize,
108-
) -> fmt::Result {
109-
match self {
110-
Self::Origin { path, pos } => {
111-
write!(f, "{:>1$}", "", lineno_max)?;
112-
write!(f, "--> {}", path)?;
113-
if let Some(line) = pos.0 {
114-
write!(f, ":{}", line)?;
115-
}
116-
f.write_char('\n')
117-
}
118-
Self::Annotation { annotation, .. } => {
119-
style.format(
120-
f,
121-
format_args!("{}", annotation.annotation_type),
122-
&[
123-
StyleClass::TitleLineAnnotationType,
124-
StyleClass::AnnotationTypeError,
125-
],
126-
)?;
127-
if let Some(id) = annotation.id {
128-
write!(f, "[{}]", id)?;
129-
}
130-
writeln!(f, ": {}", annotation.label)
131-
}
132-
}
133-
}
134-
}
135-
13642
#[derive(Debug, Clone)]
13743
pub struct DisplayMark {
13844
pub mark_type: DisplayMarkType,

src/display_list/list.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,13 @@
11
use super::annotation::Annotation;
22
use super::line::{DisplayLine, DisplayMark, DisplayMarkType, DisplayRawLine, DisplaySourceLine};
33
use crate::annotation::AnnotationType;
4-
use crate::styles::{get_stylesheet, Stylesheet};
54
use crate::{Slice, Snippet, SourceAnnotation};
6-
use std::cmp;
7-
use std::fmt;
85

96
#[derive(Debug, Clone)]
107
pub struct DisplayList<'d> {
118
pub body: Vec<DisplayLine<'d>>,
129
}
1310

14-
impl<'d> DisplayList<'d> {
15-
pub fn fmt_with_style(
16-
&self,
17-
f: &mut fmt::Formatter<'_>,
18-
style: &impl Stylesheet,
19-
) -> fmt::Result {
20-
let lineno_max = self.body.iter().rev().find_map(|line| {
21-
if let DisplayLine::Source {
22-
lineno: Some(lineno),
23-
..
24-
} = line
25-
{
26-
Some(digits(*lineno))
27-
} else {
28-
None
29-
}
30-
});
31-
let inline_marks_width = self.body.iter().fold(0, |max, line| match line {
32-
DisplayLine::Source { inline_marks, .. } => cmp::max(inline_marks.len(), max),
33-
_ => max,
34-
});
35-
for line in &self.body {
36-
line.fmt_with_style(f, style, lineno_max, inline_marks_width)?
37-
}
38-
Ok(())
39-
}
40-
}
41-
4211
fn get_header_pos(slice: &Slice) -> (Option<usize>, Option<usize>) {
4312
let line = slice.line_start;
4413
(line, None)
@@ -181,20 +150,3 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
181150
DisplayList { body }
182151
}
183152
}
184-
185-
fn digits(n: usize) -> usize {
186-
let mut n = n;
187-
let mut sum = 0;
188-
while n != 0 {
189-
n /= 10;
190-
sum += 1;
191-
}
192-
sum
193-
}
194-
195-
impl<'d> fmt::Display for DisplayList<'d> {
196-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197-
let style = get_stylesheet();
198-
self.fmt_with_style(f, &style)
199-
}
200-
}

src/renderers/ascii_default/mod.rs

Lines changed: 101 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,134 @@
11
pub mod styles;
22

33
use super::Renderer as RendererTrait;
4+
use crate::DisplayList;
45
use crate::display_list::line::DisplayLine;
56
use crate::display_list::line::DisplayRawLine;
6-
use crate::DisplayList;
7+
use crate::display_list::line::DisplaySourceLine;
8+
use crate::display_list::annotation::Annotation;
79
use std::io::Write;
810
use std::marker::PhantomData;
911
use styles::Style as StyleTrait;
12+
use std::cmp;
13+
14+
fn digits(n: usize) -> usize {
15+
let mut n = n;
16+
let mut sum = 0;
17+
while n != 0 {
18+
n /= 10;
19+
sum += 1;
20+
}
21+
sum
22+
}
1023

1124
pub struct Renderer<S: StyleTrait> {
12-
style: PhantomData<S>,
25+
style: PhantomData<S>
1326
}
1427

1528
impl<S: StyleTrait> Renderer<S> {
1629
pub fn new() -> Self {
17-
Renderer { style: PhantomData }
30+
Renderer {
31+
style: PhantomData
32+
}
1833
}
1934

2035
pub fn fmt(&self, w: &mut impl Write, dl: &DisplayList) -> std::io::Result<()> {
36+
let lineno_max = dl.body.iter().rev().find_map(|line| {
37+
if let DisplayLine::Source {
38+
lineno: Some(lineno),
39+
..
40+
} = line
41+
{
42+
Some(digits(*lineno))
43+
} else {
44+
None
45+
}
46+
});
47+
let inline_marks_width = dl.body.iter().fold(0, |max, line| match line {
48+
DisplayLine::Source { inline_marks, .. } => cmp::max(inline_marks.len(), max),
49+
_ => max,
50+
});
2151
for line in &dl.body {
22-
self.fmt_line(w, line)?;
52+
self.fmt_line(w, line, lineno_max, inline_marks_width)?;
2353
}
2454
Ok(())
2555
}
2656

27-
fn fmt_line(&self, w: &mut impl Write, line: &DisplayLine) -> std::io::Result<()> {
57+
fn fmt_line(&self, w: &mut impl Write, line: &DisplayLine, lineno_max: Option<usize>, inline_marks_width: usize) -> std::io::Result<()> {
58+
let lineno_max = lineno_max.unwrap_or(1);
2859
match line {
29-
DisplayLine::Raw(l) => self.fmt_raw_line(w, l),
30-
_ => Ok(()),
60+
DisplayLine::Source {
61+
lineno,
62+
inline_marks,
63+
line,
64+
} => {
65+
if let Some(lineno) = lineno {
66+
write!(w, "{:>1$}", lineno, lineno_max)?;
67+
} else {
68+
write!(w, "{:>1$}", "", lineno_max)?;
69+
}
70+
write!(w, " | ")?;
71+
write!(w, "{:>1$}", "", inline_marks_width - inline_marks.len())?;
72+
for mark in inline_marks {
73+
write!(w, "{}", mark)?;
74+
}
75+
self.fmt_source_line(w, line)?;
76+
//line.fmt_with_style(w, style)?;
77+
write!(w, "\n")
78+
},
79+
DisplayLine::Raw(l) => {
80+
self.fmt_raw_line(w, l, lineno_max)
81+
},
3182
}
3283
}
3384

34-
fn fmt_raw_line(
35-
&self,
36-
w: &mut impl std::io::Write,
37-
line: &DisplayRawLine,
38-
) -> std::io::Result<()> {
85+
fn fmt_source_line(&self, w: &mut impl std::io::Write, line: &DisplaySourceLine) -> std::io::Result<()> {
86+
match line {
87+
DisplaySourceLine::Content { text } => {
88+
write!(w, " {}", text)
89+
}
90+
DisplaySourceLine::Annotation {
91+
annotation,
92+
range: (start, end),
93+
} => {
94+
let indent = if start == &0 { 0 } else { start + 1 };
95+
write!(w, "{:>1$}", "", indent)?;
96+
if start == &0 {
97+
write!(w, "{:_>1$}", "^", end - start + 1)?;
98+
} else {
99+
write!(w, "{:->1$}", "", end - start)?;
100+
}
101+
write!(w, " ")?;
102+
self.fmt_annotation(w, annotation)
103+
}
104+
DisplaySourceLine::Empty => Ok(()),
105+
}
106+
}
107+
108+
fn fmt_raw_line(&self, w: &mut impl std::io::Write, line: &DisplayRawLine, lineno_max: usize) -> std::io::Result<()> {
39109
match line {
40-
DisplayRawLine::Origin { path, .. } => {
41-
let _lineno_max = 1;
42-
S::fmt(w, path)
43-
//write!(w, "{:>1$}", "", lineno_max)?;
44-
//write!(w, "--> {}", path)?;
45-
//if let Some(line) = pos.0 {
46-
//write!(w, ":{}", line)?;
47-
//}
48-
//w.write_char('\n')
110+
DisplayRawLine::Origin { path, pos } => {
111+
S::fmt(w, format_args!("{:>1$}", "", lineno_max))?;
112+
S::fmt(w, format_args!("--> {}", path))?;
113+
if let Some(line) = pos.0 {
114+
S::fmt(w, format_args!(":{}", line))?;
115+
}
116+
write!(w, "\n")
117+
},
118+
DisplayRawLine::Annotation { annotation, .. } => {
119+
S::fmt(w, format_args!("{}", annotation.annotation_type))?;
120+
if let Some(id) = annotation.id {
121+
write!(w, "[{}]", id)?;
122+
}
123+
writeln!(w, ": {}", annotation.label)
49124
}
50-
_ => Ok(()),
51125
}
52126
}
127+
128+
129+
fn fmt_annotation(&self, w: &mut impl std::io::Write, annotation: &Annotation) -> std::io::Result<()> {
130+
write!(w, "{}", annotation.label)
131+
}
53132
}
54133

55134
impl<S: StyleTrait> RendererTrait for Renderer<S> {

0 commit comments

Comments
 (0)