Skip to content

Commit 1c18950

Browse files
committed
fix!: Rename Slice to Snippet
1 parent 105da76 commit 1c18950

21 files changed

+114
-112
lines changed

benches/simple.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate criterion;
44

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

7-
use annotate_snippets::{Label, Message, Renderer, Slice};
7+
use annotate_snippets::{Label, Message, Renderer, Snippet};
88

99
fn create_snippet(renderer: Renderer) {
1010
let source = r#") -> Option<String> {
@@ -29,8 +29,8 @@ fn create_snippet(renderer: Renderer) {
2929
_ => continue,
3030
}
3131
}"#;
32-
let message = Message::error("mismatched types").id("E0308").slice(
33-
Slice::new(source, 51)
32+
let message = Message::error("mismatched types").id("E0308").snippet(
33+
Snippet::new(source, 51)
3434
.origin("src/format.rs")
3535
.annotation(
3636
Label::warning("expected `Option<String>` because of return type").span(5..19),

examples/expected_type.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use annotate_snippets::{Label, Message, Renderer, Slice};
1+
use annotate_snippets::{Label, Message, Renderer, Snippet};
22

33
fn main() {
44
let source = r#" annotations: vec![SourceAnnotation {
55
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
66
,
77
range: <22, 25>,"#;
8-
let message = Message::error("expected type, found `22`").slice(
9-
Slice::new(source, 26)
8+
let message = Message::error("expected type, found `22`").snippet(
9+
Snippet::new(source, 26)
1010
.origin("examples/footer.rs")
1111
.fold(true)
1212
.annotation(

examples/footer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use annotate_snippets::{Label, Message, Renderer, Slice};
1+
use annotate_snippets::{Label, Message, Renderer, Snippet};
22

33
fn main() {
44
let message = Message::error("mismatched types")
55
.id("E0308")
6-
.slice(
7-
Slice::new(" slices: vec![\"A\",", 13)
6+
.snippet(
7+
Snippet::new(" slices: vec![\"A\",", 13)
88
.origin("src/multislice.rs")
99
.annotation(
1010
Label::error(

examples/format.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{Label, Message, Renderer, Slice};
1+
use annotate_snippets::{Label, Message, Renderer, Snippet};
22

33
fn main() {
44
let source = r#") -> Option<String> {
@@ -23,8 +23,8 @@ fn main() {
2323
_ => continue,
2424
}
2525
}"#;
26-
let message = Message::error("mismatched types").id("E0308").slice(
27-
Slice::new(source, 51)
26+
let message = Message::error("mismatched types").id("E0308").snippet(
27+
Snippet::new(source, 51)
2828
.origin("src/format.rs")
2929
.annotation(
3030
Label::warning("expected `Option<String>` because of return type").span(5..19),

examples/multislice.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use annotate_snippets::{Message, Renderer, Slice};
1+
use annotate_snippets::{Message, Renderer, Snippet};
22

33
fn main() {
44
let message = Message::error("mismatched types")
5-
.slice(Slice::new("Foo", 51).origin("src/format.rs"))
6-
.slice(Slice::new("Faa", 129).origin("src/display.rs"));
5+
.snippet(Snippet::new("Foo", 51).origin("src/format.rs"))
6+
.snippet(Snippet::new("Faa", 129).origin("src/display.rs"));
77

88
let renderer = Renderer::styled();
99
anstream::println!("{}", renderer.render(message));

src/renderer/display_list.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a> DisplayList<'a> {
110110
title,
111111
id,
112112
footer,
113-
slices,
113+
snippets,
114114
}: snippet::Message<'a>,
115115
stylesheet: &'a Stylesheet,
116116
anonymized_line_numbers: bool,
@@ -120,9 +120,9 @@ impl<'a> DisplayList<'a> {
120120

121121
body.push(format_title(title, id));
122122

123-
for (idx, slice) in slices.into_iter().enumerate() {
123+
for (idx, snippet) in snippets.into_iter().enumerate() {
124124
body.append(&mut format_slice(
125-
slice,
125+
snippet,
126126
idx == 0,
127127
!footer.is_empty(),
128128
margin,
@@ -542,7 +542,7 @@ pub enum DisplayLine<'a> {
542542
/// A source line.
543543
#[derive(Debug, PartialEq)]
544544
pub enum DisplaySourceLine<'a> {
545-
/// A line with the content of the Slice.
545+
/// A line with the content of the Snippet.
546546
Content {
547547
text: &'a str,
548548
range: (usize, usize), // meta information for annotation placement.
@@ -762,15 +762,15 @@ fn format_footer(footer: snippet::Label<'_>) -> Vec<DisplayLine<'_>> {
762762
}
763763

764764
fn format_slice(
765-
slice: snippet::Slice<'_>,
765+
snippet: snippet::Snippet<'_>,
766766
is_first: bool,
767767
has_footer: bool,
768768
margin: Option<Margin>,
769769
) -> Vec<DisplayLine<'_>> {
770-
let main_range = slice.annotations.first().map(|x| x.range.start);
771-
let origin = slice.origin;
770+
let main_range = snippet.annotations.first().map(|x| x.range.start);
771+
let origin = snippet.origin;
772772
let need_empty_header = origin.is_some() || is_first;
773-
let mut body = format_body(slice, need_empty_header, has_footer, margin);
773+
let mut body = format_body(snippet, need_empty_header, has_footer, margin);
774774
let header = format_header(origin, main_range, &body, is_first);
775775
let mut result = vec![];
776776

@@ -942,13 +942,13 @@ fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
942942
}
943943

944944
fn format_body(
945-
slice: snippet::Slice<'_>,
945+
snippet: snippet::Snippet<'_>,
946946
need_empty_header: bool,
947947
has_footer: bool,
948948
margin: Option<Margin>,
949949
) -> Vec<DisplayLine<'_>> {
950-
let source_len = slice.source.len();
951-
if let Some(bigger) = slice.annotations.iter().find_map(|x| {
950+
let source_len = snippet.source.len();
951+
if let Some(bigger) = snippet.annotations.iter().find_map(|x| {
952952
// Allow highlighting one past the last character in the source.
953953
if source_len + 1 < x.range.end {
954954
Some(&x.range)
@@ -963,7 +963,7 @@ fn format_body(
963963
}
964964

965965
let mut body = vec![];
966-
let mut current_line = slice.line_start;
966+
let mut current_line = snippet.line_start;
967967
let mut current_index = 0;
968968
let mut line_info = vec![];
969969

@@ -972,7 +972,7 @@ fn format_body(
972972
line_end_index: usize,
973973
}
974974

975-
for (line, end_line) in CursorLines::new(slice.source) {
975+
for (line, end_line) in CursorLines::new(snippet.source) {
976976
let line_length: usize = line
977977
.chars()
978978
.map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0))
@@ -995,7 +995,7 @@ fn format_body(
995995
}
996996

997997
let mut annotation_line_count = 0;
998-
let mut annotations = slice.annotations;
998+
let mut annotations = snippet.annotations;
999999
for (
10001000
idx,
10011001
LineInfo {
@@ -1143,7 +1143,7 @@ fn format_body(
11431143
});
11441144
}
11451145

1146-
if slice.fold {
1146+
if snippet.fold {
11471147
body = fold_body(body);
11481148
}
11491149

@@ -1227,7 +1227,7 @@ mod tests {
12271227
let line_1 = "This is line 1";
12281228
let line_2 = "This is line 2";
12291229
let source = [line_1, line_2].join("\n");
1230-
let input = snippet::Message::error("").slice(snippet::Slice::new(&source, 5402));
1230+
let input = snippet::Message::error("").snippet(snippet::Snippet::new(&source, 5402));
12311231
let output = from_display_lines(vec![
12321232
DisplayLine::Raw(DisplayRawLine::Annotation {
12331233
annotation: Annotation {
@@ -1278,8 +1278,8 @@ mod tests {
12781278
let src_1 = "This is slice 2";
12791279
let src_1_len = src_1.len();
12801280
let input = snippet::Message::error("")
1281-
.slice(snippet::Slice::new(src_0, 5402).origin("file1.rs"))
1282-
.slice(snippet::Slice::new(src_1, 2).origin("file2.rs"));
1281+
.snippet(snippet::Snippet::new(src_0, 5402).origin("file1.rs"))
1282+
.snippet(snippet::Snippet::new(src_1, 2).origin("file2.rs"));
12831283
let output = from_display_lines(vec![
12841284
DisplayLine::Raw(DisplayRawLine::Annotation {
12851285
annotation: Annotation {
@@ -1350,8 +1350,8 @@ mod tests {
13501350
let source = [line_1, line_2].join("\n");
13511351
// In line 2
13521352
let range = 22..24;
1353-
let input = snippet::Message::error("").slice(
1354-
snippet::Slice::new(&source, 5402)
1353+
let input = snippet::Message::error("").snippet(
1354+
snippet::Snippet::new(&source, 5402)
13551355
.annotation(snippet::Label::info("Test annotation").span(range.clone())),
13561356
);
13571357
let output = from_display_lines(vec![
@@ -1455,17 +1455,17 @@ mod tests {
14551455
fn test_i26() {
14561456
let source = "short";
14571457
let label = "label";
1458-
let input = snippet::Message::error("").slice(
1459-
snippet::Slice::new(source, 0)
1458+
let input = snippet::Message::error("").snippet(
1459+
snippet::Snippet::new(source, 0)
14601460
.annotation(snippet::Label::error(label).span(0..source.len() + 2)),
14611461
);
14621462
let _ = DisplayList::new(input, &STYLESHEET, false, None);
14631463
}
14641464

14651465
#[test]
14661466
fn test_i_29() {
1467-
let snippets = snippet::Message::error("oops").slice(
1468-
snippet::Slice::new("First line\r\nSecond oops line", 1)
1467+
let snippets = snippet::Message::error("oops").snippet(
1468+
snippet::Snippet::new("First line\r\nSecond oops line", 1)
14691469
.origin("<current file>")
14701470
.fold(true)
14711471
.annotation(snippet::Label::error("oops").span(19..23)),

src/renderer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//!
33
//! # Example
44
//! ```
5-
//! use annotate_snippets::{Renderer, Slice, Message};
5+
//! use annotate_snippets::{Renderer, Snippet, Message};
66
//! let snippet = Message::error("mismatched types")
7-
//! .slice(Slice::new("Foo", 51).origin("src/format.rs"))
8-
//! .slice(Slice::new("Faa", 129).origin("src/display.rs"));
7+
//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs"))
8+
//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs"));
99
//!
1010
//! let renderer = Renderer::styled();
1111
//! println!("{}", renderer.render(snippet));

src/snippet.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//! use annotate_snippets::*;
77
//!
88
//! Message::error("mismatched types")
9-
//! .slice(Slice::new("Foo", 51).origin("src/format.rs"))
10-
//! .slice(Slice::new("Faa", 129).origin("src/display.rs"));
9+
//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs"))
10+
//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs"));
1111
//! ```
1212
1313
use std::ops::Range;
@@ -16,7 +16,7 @@ use std::ops::Range;
1616
pub struct Message<'a> {
1717
pub(crate) title: Label<'a>,
1818
pub(crate) id: Option<&'a str>,
19-
pub(crate) slices: Vec<Slice<'a>>,
19+
pub(crate) snippets: Vec<Snippet<'a>>,
2020
pub(crate) footer: Vec<Label<'a>>,
2121
}
2222

@@ -25,7 +25,7 @@ impl<'a> Message<'a> {
2525
Self {
2626
title,
2727
id: None,
28-
slices: vec![],
28+
snippets: vec![],
2929
footer: vec![],
3030
}
3131
}
@@ -55,8 +55,8 @@ impl<'a> Message<'a> {
5555
self
5656
}
5757

58-
pub fn slice(mut self, slice: Slice<'a>) -> Self {
59-
self.slices.push(slice);
58+
pub fn snippet(mut self, slice: Snippet<'a>) -> Self {
59+
self.snippets.push(slice);
6060
self
6161
}
6262

@@ -100,7 +100,7 @@ impl<'a> Label<'a> {
100100
self
101101
}
102102

103-
/// Create a [`Annotation`] with the given span for a [`Slice`]
103+
/// Create a [`Annotation`] with the given span for a [`Snippet`]
104104
pub fn span(&self, span: Range<usize>) -> Annotation<'a> {
105105
Annotation {
106106
range: span,
@@ -113,17 +113,17 @@ impl<'a> Label<'a> {
113113
/// Structure containing the slice of text to be annotated and
114114
/// basic information about the location of the slice.
115115
///
116-
/// One `Slice` is meant to represent a single, continuous,
116+
/// One `Snippet` is meant to represent a single, continuous,
117117
/// slice of source code that you want to annotate.
118-
pub struct Slice<'a> {
118+
pub struct Snippet<'a> {
119119
pub(crate) source: &'a str,
120120
pub(crate) line_start: usize,
121121
pub(crate) origin: Option<&'a str>,
122122
pub(crate) annotations: Vec<Annotation<'a>>,
123123
pub(crate) fold: bool,
124124
}
125125

126-
impl<'a> Slice<'a> {
126+
impl<'a> Snippet<'a> {
127127
pub fn new(source: &'a str, line_start: usize) -> Self {
128128
Self {
129129
source,
@@ -162,7 +162,7 @@ pub enum Level {
162162
Help,
163163
}
164164

165-
/// An annotation for a [`Slice`].
165+
/// An annotation for a [`Snippet`].
166166
///
167167
/// This gets created by [`Label::span`].
168168
#[derive(Debug)]

0 commit comments

Comments
 (0)