Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to a Renderer when for displaying a Snippet #67

Merged
merged 12 commits into from
Dec 5, 2023
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ keywords = ["code", "analysis", "ascii", "errors", "debug"]
maintenance = { status = "actively-developed" }

[dependencies]
anstyle = "1.0.4"
unicode-width = "0.1.11"
yansi-term = { version = "0.1.2", optional = true }

[dev-dependencies]
criterion = "0.5.1"
difference = "2.0.0"
glob = "0.3.1"
serde = { version = "1.0.192", features = ["derive"] }
toml = "0.5.11"
yansi-term = "0.1.2"

[[bench]]
name = "simple"
harness = false

[features]
default = []
color = ["yansi-term"]
18 changes: 6 additions & 12 deletions benches/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ extern crate criterion;

use criterion::{black_box, Criterion};

use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};

fn create_snippet() {
fn create_snippet(renderer: Renderer) {
let snippet = Snippet {
slices: vec![Slice {
source: r#") -> Option<String> {
Expand Down Expand Up @@ -56,18 +53,15 @@ fn create_snippet() {
annotation_type: AnnotationType::Error,
}),
footer: vec![],
opt: FormatOptions {
color: true,
..Default::default()
},
};

let dl = DisplayList::from(snippet);
let _result = dl.to_string();
let _result = renderer.render(snippet).to_string();
}

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("format", |b| b.iter(|| black_box(create_snippet())));
c.bench_function("format", |b| {
b.iter(|| black_box(create_snippet(Renderer::plain())))
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
13 changes: 3 additions & 10 deletions examples/expected_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};

fn main() {
let snippet = Snippet {
Expand Down Expand Up @@ -32,12 +29,8 @@ fn main() {
},
],
}],
opt: FormatOptions {
color: true,
..Default::default()
},
};

let dl = DisplayList::from(snippet);
println!("{}", dl);
let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
}
13 changes: 3 additions & 10 deletions examples/footer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};

fn main() {
let snippet = Snippet {
Expand All @@ -28,12 +25,8 @@ fn main() {
annotation_type: AnnotationType::Error,
}],
}],
opt: FormatOptions {
color: true,
..Default::default()
},
};

let dl = DisplayList::from(snippet);
println!("{}", dl);
let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
}
13 changes: 3 additions & 10 deletions examples/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};

fn main() {
let snippet = Snippet {
Expand Down Expand Up @@ -50,12 +47,8 @@ fn main() {
annotation_type: AnnotationType::Error,
}),
footer: vec![],
opt: FormatOptions {
color: true,
..Default::default()
},
};

let dl = DisplayList::from(snippet);
println!("{}", dl);
let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
}
13 changes: 3 additions & 10 deletions examples/multislice.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, AnnotationType, Slice, Snippet},
};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet};

fn main() {
let snippet = Snippet {
Expand All @@ -27,12 +24,8 @@ fn main() {
annotations: vec![],
},
],
opt: FormatOptions {
color: true,
..Default::default()
},
};

let dl = DisplayList::from(snippet);
println!("{}", dl);
let renderer = Renderer::plain();
println!("{}", renderer.render(snippet));
}
23 changes: 0 additions & 23 deletions src/formatter/mod.rs

This file was deleted.

51 changes: 0 additions & 51 deletions src/formatter/style.rs

This file was deleted.

31 changes: 13 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,24 @@
//! The crate uses a three stage process with two conversions between states:
//!
//! ```text
//! Snippet --> DisplayList --> String
//! Snippet --> Renderer --> impl Display
//! ```
//!
//! The input type - [Snippet](self::snippet) is a structure designed
//! The input type - [Snippet] is a structure designed
//! to align with likely output from any parser whose code snippet is to be
//! annotated.
//!
//! The middle structure - [DisplayList](self::display_list) is a
//! structure designed to store the snippet data converted into a vector
//! of lines containing semantic information about each line.
//! This structure is the easiest to manipulate and organize.
//! The middle structure - [Renderer] is a structure designed
//! to convert a snippet into an internal structure that is designed to store
//! the snippet data in a way that is easy to format.
//! [Renderer] also handles the user-configurable formatting
//! options, such as color, or margins.
//!
//! Finally, `impl Display` into a final `String` output.
//!
//! A user of the crate may choose to provide their own equivalent of the input
//! structure with an `Into<DisplayList>` trait.
//!
//! A user of the crate may also choose to provide their own formatter logic,
//! to convert a `DisplayList` into a `String`, or just a `Stylesheet` to
//! use the crate's formatting logic, but with a custom stylesheet.
// TODO: check documentation

pub mod display_list;
pub mod formatter;
pub mod snippet;
pub mod stylesheets;
pub mod renderer;
mod snippet;

#[doc(inline)]
pub use renderer::Renderer;
pub use snippet::*;
Loading
Loading