Skip to content

Commit 131a7af

Browse files
authored
Merge pull request #67 from Muscraft/refactor
Move to a `Renderer` when for displaying a `Snippet`
2 parents a5f00ab + 71b1eb5 commit 131a7af

33 files changed

+1549
-1562
lines changed

Diff for: Cargo.lock

+1-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ keywords = ["code", "analysis", "ascii", "errors", "debug"]
1414
maintenance = { status = "actively-developed" }
1515

1616
[dependencies]
17+
anstyle = "1.0.4"
1718
unicode-width = "0.1.11"
18-
yansi-term = { version = "0.1.2", optional = true }
1919

2020
[dev-dependencies]
2121
criterion = "0.5.1"
2222
difference = "2.0.0"
2323
glob = "0.3.1"
2424
serde = { version = "1.0.192", features = ["derive"] }
2525
toml = "0.5.11"
26-
yansi-term = "0.1.2"
2726

2827
[[bench]]
2928
name = "simple"
3029
harness = false
3130

3231
[features]
3332
default = []
34-
color = ["yansi-term"]

Diff for: benches/simple.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ extern crate criterion;
44

55
use criterion::{black_box, Criterion};
66

7-
use annotate_snippets::{
8-
display_list::{DisplayList, FormatOptions},
9-
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
10-
};
7+
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
118

12-
fn create_snippet() {
9+
fn create_snippet(renderer: Renderer) {
1310
let snippet = Snippet {
1411
slices: vec![Slice {
1512
source: r#") -> Option<String> {
@@ -56,18 +53,15 @@ fn create_snippet() {
5653
annotation_type: AnnotationType::Error,
5754
}),
5855
footer: vec![],
59-
opt: FormatOptions {
60-
color: true,
61-
..Default::default()
62-
},
6356
};
6457

65-
let dl = DisplayList::from(snippet);
66-
let _result = dl.to_string();
58+
let _result = renderer.render(snippet).to_string();
6759
}
6860

6961
pub fn criterion_benchmark(c: &mut Criterion) {
70-
c.bench_function("format", |b| b.iter(|| black_box(create_snippet())));
62+
c.bench_function("format", |b| {
63+
b.iter(|| black_box(create_snippet(Renderer::plain())))
64+
});
7165
}
7266

7367
criterion_group!(benches, criterion_benchmark);

Diff for: examples/expected_type.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use annotate_snippets::{
2-
display_list::{DisplayList, FormatOptions},
3-
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
4-
};
1+
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
52

63
fn main() {
74
let snippet = Snippet {
@@ -32,12 +29,8 @@ fn main() {
3229
},
3330
],
3431
}],
35-
opt: FormatOptions {
36-
color: true,
37-
..Default::default()
38-
},
3932
};
4033

41-
let dl = DisplayList::from(snippet);
42-
println!("{}", dl);
34+
let renderer = Renderer::plain();
35+
println!("{}", renderer.render(snippet));
4336
}

Diff for: examples/footer.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use annotate_snippets::{
2-
display_list::{DisplayList, FormatOptions},
3-
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
4-
};
1+
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
52

63
fn main() {
74
let snippet = Snippet {
@@ -28,12 +25,8 @@ fn main() {
2825
annotation_type: AnnotationType::Error,
2926
}],
3027
}],
31-
opt: FormatOptions {
32-
color: true,
33-
..Default::default()
34-
},
3528
};
3629

37-
let dl = DisplayList::from(snippet);
38-
println!("{}", dl);
30+
let renderer = Renderer::plain();
31+
println!("{}", renderer.render(snippet));
3932
}

Diff for: examples/format.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use annotate_snippets::{
2-
display_list::{DisplayList, FormatOptions},
3-
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
4-
};
1+
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
52

63
fn main() {
74
let snippet = Snippet {
@@ -50,12 +47,8 @@ fn main() {
5047
annotation_type: AnnotationType::Error,
5148
}),
5249
footer: vec![],
53-
opt: FormatOptions {
54-
color: true,
55-
..Default::default()
56-
},
5750
};
5851

59-
let dl = DisplayList::from(snippet);
60-
println!("{}", dl);
52+
let renderer = Renderer::plain();
53+
println!("{}", renderer.render(snippet));
6154
}

Diff for: examples/multislice.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use annotate_snippets::{
2-
display_list::{DisplayList, FormatOptions},
3-
snippet::{Annotation, AnnotationType, Slice, Snippet},
4-
};
1+
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet};
52

63
fn main() {
74
let snippet = Snippet {
@@ -27,12 +24,8 @@ fn main() {
2724
annotations: vec![],
2825
},
2926
],
30-
opt: FormatOptions {
31-
color: true,
32-
..Default::default()
33-
},
3427
};
3528

36-
let dl = DisplayList::from(snippet);
37-
println!("{}", dl);
29+
let renderer = Renderer::plain();
30+
println!("{}", renderer.render(snippet));
3831
}

Diff for: src/formatter/mod.rs

-23
This file was deleted.

Diff for: src/formatter/style.rs

-51
This file was deleted.

Diff for: src/lib.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,24 @@
2626
//! The crate uses a three stage process with two conversions between states:
2727
//!
2828
//! ```text
29-
//! Snippet --> DisplayList --> String
29+
//! Snippet --> Renderer --> impl Display
3030
//! ```
3131
//!
32-
//! The input type - [Snippet](self::snippet) is a structure designed
32+
//! The input type - [Snippet] is a structure designed
3333
//! to align with likely output from any parser whose code snippet is to be
3434
//! annotated.
3535
//!
36-
//! The middle structure - [DisplayList](self::display_list) is a
37-
//! structure designed to store the snippet data converted into a vector
38-
//! of lines containing semantic information about each line.
39-
//! This structure is the easiest to manipulate and organize.
36+
//! The middle structure - [Renderer] is a structure designed
37+
//! to convert a snippet into an internal structure that is designed to store
38+
//! the snippet data in a way that is easy to format.
39+
//! [Renderer] also handles the user-configurable formatting
40+
//! options, such as color, or margins.
4041
//!
4142
//! Finally, `impl Display` into a final `String` output.
42-
//!
43-
//! A user of the crate may choose to provide their own equivalent of the input
44-
//! structure with an `Into<DisplayList>` trait.
45-
//!
46-
//! A user of the crate may also choose to provide their own formatter logic,
47-
//! to convert a `DisplayList` into a `String`, or just a `Stylesheet` to
48-
//! use the crate's formatting logic, but with a custom stylesheet.
49-
// TODO: check documentation
5043
51-
pub mod display_list;
52-
pub mod formatter;
53-
pub mod snippet;
54-
pub mod stylesheets;
44+
pub mod renderer;
45+
mod snippet;
46+
47+
#[doc(inline)]
48+
pub use renderer::Renderer;
49+
pub use snippet::*;

0 commit comments

Comments
 (0)