Skip to content

Commit 5a282bd

Browse files
authored
Merge pull request #227 from Muscraft/rename-origin
Improve clarity arount Origin and Snippet
2 parents 1198546 + 8df4d37 commit 5a282bd

29 files changed

+194
-184
lines changed

benches/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn simple() -> String {
2828
Group::new().element(
2929
Snippet::source(source)
3030
.line_start(51)
31-
.origin("src/format.rs")
31+
.path("src/format.rs")
3232
.annotation(
3333
AnnotationKind::Context
3434
.span(5..19)
@@ -73,7 +73,7 @@ fn fold(bencher: divan::Bencher<'_, '_>, context: usize) {
7373
Group::new().element(
7474
Snippet::source(&input)
7575
.fold(true)
76-
.origin("src/format.rs")
76+
.path("src/format.rs")
7777
.annotation(
7878
AnnotationKind::Context
7979
.span(span)

examples/custom_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub static C: u32 = 0 - 1;
2222
.group(
2323
Group::new().element(
2424
Snippet::source(source)
25-
.origin("$DIR/err.rs")
25+
.path("$DIR/err.rs")
2626
.fold(true)
2727
.annotation(
2828
AnnotationKind::Primary

examples/custom_level.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
Group::new().element(
3737
Snippet::source(source)
3838
.line_start(1)
39-
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
39+
.path("$DIR/issue-114529-illegal-break-with-value.rs")
4040
.fold(true)
4141
.annotation(
4242
AnnotationKind::Primary
@@ -60,7 +60,7 @@ fn main() {
6060
.element(
6161
Snippet::source(source)
6262
.line_start(1)
63-
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
63+
.path("$DIR/issue-114529-illegal-break-with-value.rs")
6464
.fold(true)
6565
.patch(Patch::new(483..581, "break")),
6666
),

examples/expected_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
Group::new().element(
1111
Snippet::source(source)
1212
.line_start(26)
13-
.origin("examples/footer.rs")
13+
.path("examples/footer.rs")
1414
.fold(true)
1515
.annotation(AnnotationKind::Primary.span(193..195).label(
1616
"expected struct `annotate_snippets::snippet::Slice`, found reference",

examples/footer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
Group::new().element(
99
Snippet::source(" slices: vec![\"A\",")
1010
.line_start(13)
11-
.origin("src/multislice.rs")
11+
.path("src/multislice.rs")
1212
.annotation(AnnotationKind::Primary.span(21..24).label(
1313
"expected struct `annotate_snippets::snippet::Slice`, found reference",
1414
)),

examples/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() {
2727
Group::new().element(
2828
Snippet::source(source)
2929
.line_start(51)
30-
.origin("src/format.rs")
30+
.path("src/format.rs")
3131
.annotation(
3232
AnnotationKind::Context
3333
.span(5..19)

examples/highlight_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {}
1717
.element(
1818
Snippet::source(source)
1919
.fold(true)
20-
.origin("$DIR/E0010-teach.rs")
20+
.path("$DIR/E0010-teach.rs")
2121
.annotation(
2222
AnnotationKind::Primary
2323
.span(72..85)

examples/highlight_title.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() {
4949
.element(
5050
Snippet::source(source)
5151
.fold(true)
52-
.origin("$DIR/highlighting.rs")
52+
.path("$DIR/highlighting.rs")
5353
.annotation(
5454
AnnotationKind::Primary
5555
.span(553..563)
@@ -69,7 +69,7 @@ fn main() {
6969
.element(
7070
Snippet::source(source)
7171
.fold(true)
72-
.origin("$DIR/highlighting.rs")
72+
.path("$DIR/highlighting.rs")
7373
.annotation(AnnotationKind::Context.span(200..333).label(""))
7474
.annotation(AnnotationKind::Primary.span(194..199)),
7575
),

examples/multislice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ fn main() {
66
.element(
77
Snippet::<Annotation<'_>>::source("Foo")
88
.line_start(51)
9-
.origin("src/format.rs"),
9+
.path("src/format.rs"),
1010
)
1111
.element(
1212
Snippet::<Annotation<'_>>::source("Faa")
1313
.line_start(129)
14-
.origin("src/display.rs"),
14+
.path("src/display.rs"),
1515
),
1616
);
1717

src/renderer/mod.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! .group(
2525
//! Group::new().element(
2626
//! Snippet::source(source)
27-
//! .origin("temp.rs")
27+
//! .path("temp.rs")
2828
//! .line_start(1)
2929
//! .fold(true)
3030
//! .annotation(
@@ -232,21 +232,21 @@ impl Renderer {
232232
) -> Result<String, fmt::Error> {
233233
let mut out_string = String::new();
234234

235-
let og_primary_origin = message
235+
let og_primary_path = message
236236
.groups
237237
.iter()
238238
.find_map(|group| {
239239
group.elements.iter().find_map(|s| match &s {
240240
Element::Cause(cause) => {
241241
if cause.markers.iter().any(|m| m.kind.is_primary()) {
242-
Some(cause.origin)
242+
Some(cause.path)
243243
} else {
244244
None
245245
}
246246
}
247247
Element::Origin(origin) => {
248248
if origin.primary {
249-
Some(Some(origin.origin))
249+
Some(Some(origin.path))
250250
} else {
251251
None
252252
}
@@ -260,8 +260,8 @@ impl Renderer {
260260
.iter()
261261
.find_map(|group| {
262262
group.elements.iter().find_map(|s| match &s {
263-
Element::Cause(cause) => Some(cause.origin),
264-
Element::Origin(origin) => Some(Some(origin.origin)),
263+
Element::Cause(cause) => Some(cause.path),
264+
Element::Origin(origin) => Some(Some(origin.path)),
265265
_ => None,
266266
})
267267
})
@@ -270,20 +270,20 @@ impl Renderer {
270270
let group_len = message.groups.len();
271271
for (g, group) in message.groups.into_iter().enumerate() {
272272
let mut buffer = StyledBuffer::new();
273-
let primary_origin = group
273+
let primary_path = group
274274
.elements
275275
.iter()
276276
.find_map(|s| match &s {
277277
Element::Cause(cause) => {
278278
if cause.markers.iter().any(|m| m.kind.is_primary()) {
279-
Some(cause.origin)
279+
Some(cause.path)
280280
} else {
281281
None
282282
}
283283
}
284284
Element::Origin(origin) => {
285285
if origin.primary {
286-
Some(Some(origin.origin))
286+
Some(Some(origin.path))
287287
} else {
288288
None
289289
}
@@ -295,8 +295,8 @@ impl Renderer {
295295
.elements
296296
.iter()
297297
.find_map(|s| match &s {
298-
Element::Cause(cause) => Some(cause.origin),
299-
Element::Origin(origin) => Some(Some(origin.origin)),
298+
Element::Cause(cause) => Some(cause.path),
299+
Element::Origin(origin) => Some(Some(origin.path)),
300300
_ => None,
301301
})
302302
.unwrap_or_default(),
@@ -357,7 +357,7 @@ impl Renderer {
357357
&mut buffer,
358358
max_line_num_len,
359359
cause,
360-
primary_origin,
360+
primary_path,
361361
&source_map,
362362
&annotated_lines,
363363
max_depth,
@@ -396,7 +396,7 @@ impl Renderer {
396396
suggestion,
397397
max_line_num_len,
398398
&source_map,
399-
primary_origin.or(og_primary_origin),
399+
primary_path.or(og_primary_path),
400400
last_was_suggestion,
401401
);
402402
last_was_suggestion = true;
@@ -490,8 +490,8 @@ impl Renderer {
490490
labels = Some(labels_inner);
491491
}
492492

493-
if let Some(origin) = cause.origin {
494-
let mut origin = Origin::new(origin);
493+
if let Some(path) = cause.path {
494+
let mut origin = Origin::new(path);
495495
origin.primary = true;
496496

497497
let source_map = SourceMap::new(cause.source, cause.line_start);
@@ -764,10 +764,10 @@ impl Renderer {
764764

765765
let str = match (&origin.line, &origin.char_column) {
766766
(Some(line), Some(col)) => {
767-
format!("{}:{}:{}", origin.origin, line, col)
767+
format!("{}:{}:{}", origin.path, line, col)
768768
}
769-
(Some(line), None) => format!("{}:{}", origin.origin, line),
770-
_ => origin.origin.to_owned(),
769+
(Some(line), None) => format!("{}:{}", origin.path, line),
770+
_ => origin.path.to_owned(),
771771
};
772772

773773
buffer.append(buffer_msg_line_offset, &str, ElementStyle::LineAndColumn);
@@ -784,17 +784,17 @@ impl Renderer {
784784
buffer: &mut StyledBuffer,
785785
max_line_num_len: usize,
786786
snippet: &Snippet<'_, Annotation<'_>>,
787-
primary_origin: Option<&str>,
787+
primary_path: Option<&str>,
788788
sm: &SourceMap<'_>,
789789
annotated_lines: &[AnnotatedLineInfo<'_>],
790790
multiline_depth: usize,
791791
is_cont: bool,
792792
) {
793-
if let Some(origin) = snippet.origin {
794-
let mut origin = Origin::new(origin);
793+
if let Some(path) = snippet.path {
794+
let mut origin = Origin::new(path);
795795
// print out the span location and spacer before we print the annotated source
796796
// to do this, we need to know if this span will be primary
797-
let is_primary = primary_origin == Some(origin.origin);
797+
let is_primary = primary_path == Some(origin.path);
798798

799799
if is_primary {
800800
origin.primary = true;
@@ -1648,7 +1648,7 @@ impl Renderer {
16481648
suggestion: &Snippet<'_, Patch<'_>>,
16491649
max_line_num_len: usize,
16501650
sm: &SourceMap<'_>,
1651-
primary_origin: Option<&str>,
1651+
primary_path: Option<&str>,
16521652
is_cont: bool,
16531653
) {
16541654
let suggestions = sm.splice_lines(suggestion.markers.clone());
@@ -1671,14 +1671,14 @@ impl Renderer {
16711671
ElementStyle::LineNumber,
16721672
);
16731673
}
1674-
if suggestion.origin != primary_origin {
1675-
if let Some(origin) = suggestion.origin {
1674+
if suggestion.path != primary_path {
1675+
if let Some(path) = suggestion.path {
16761676
let (loc, _) = sm.span_to_locations(parts[0].span.clone());
16771677
// --> file.rs:line:col
16781678
// |
16791679
let arrow = self.file_start();
16801680
buffer.puts(row_num - 1, 0, arrow, ElementStyle::LineNumber);
1681-
let message = format!("{}:{}:{}", origin, loc.line, loc.char + 1);
1681+
let message = format!("{}:{}:{}", path, loc.line, loc.char + 1);
16821682
if is_cont {
16831683
buffer.append(row_num - 1, &message, ElementStyle::LineAndColumn);
16841684
} else {

src/snippet.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ pub struct Title<'a> {
162162
}
163163

164164
/// A source view [`Element`] in a [`Group`]
165+
///
166+
/// If you do not have [source][Snippet::source] available, see instead [`Origin`]
165167
#[derive(Clone, Debug)]
166168
pub struct Snippet<'a, T> {
167-
pub(crate) origin: Option<&'a str>,
169+
pub(crate) path: Option<&'a str>,
168170
pub(crate) line_start: usize,
169171
pub(crate) source: &'a str,
170172
pub(crate) markers: Vec<T>,
@@ -183,7 +185,7 @@ impl<'a, T: Clone> Snippet<'a, T> {
183185
/// </div>
184186
pub fn source(source: &'a str) -> Self {
185187
Self {
186-
origin: None,
188+
path: None,
187189
line_start: 1,
188190
source,
189191
markers: vec![],
@@ -207,8 +209,8 @@ impl<'a, T: Clone> Snippet<'a, T> {
207209
/// not allowed to be passed to this function.
208210
///
209211
/// </div>
210-
pub fn origin(mut self, origin: &'a str) -> Self {
211-
self.origin = Some(origin);
212+
pub fn path(mut self, path: &'a str) -> Self {
213+
self.path = Some(path);
212214
self
213215
}
214216

@@ -375,10 +377,12 @@ impl<'a> Patch<'a> {
375377
}
376378
}
377379

378-
/// The location of the [`Snippet`] (e.g. a path)
380+
/// The referenced location (e.g. a path)
381+
///
382+
/// If you have source available, see instead [`Snippet`]
379383
#[derive(Clone, Debug)]
380384
pub struct Origin<'a> {
381-
pub(crate) origin: &'a str,
385+
pub(crate) path: &'a str,
382386
pub(crate) line: Option<usize>,
383387
pub(crate) char_column: Option<usize>,
384388
pub(crate) primary: bool,
@@ -392,9 +396,9 @@ impl<'a> Origin<'a> {
392396
/// not allowed to be passed to this function.
393397
///
394398
/// </div>
395-
pub fn new(origin: &'a str) -> Self {
399+
pub fn new(path: &'a str) -> Self {
396400
Self {
397-
origin,
401+
path,
398402
line: None,
399403
char_column: None,
400404
primary: false,
@@ -412,6 +416,12 @@ impl<'a> Origin<'a> {
412416
/// Set the default column to display
413417
///
414418
/// Otherwise this will be inferred from the primary [`Annotation`]
419+
///
420+
/// <div class="warning">
421+
///
422+
/// `char_column` is only be respected if [`Origin::line`] is also set.
423+
///
424+
/// </div>
415425
pub fn char_column(mut self, char_column: usize) -> Self {
416426
self.char_column = Some(char_column);
417427
self

tests/color/ann_eof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn case() {
77
let input = Level::ERROR.header("expected `.`, `=`").group(
88
Group::new().element(
99
Snippet::source("asdf")
10-
.origin("Cargo.toml")
10+
.path("Cargo.toml")
1111
.line_start(1)
1212
.annotation(AnnotationKind::Primary.span(4..4).label("")),
1313
),

tests/color/ann_insertion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn case() {
77
let input = Level::ERROR.header("expected `.`, `=`").group(
88
Group::new().element(
99
Snippet::source("asf")
10-
.origin("Cargo.toml")
10+
.path("Cargo.toml")
1111
.line_start(1)
1212
.annotation(AnnotationKind::Primary.span(2..2).label("'d' belongs here")),
1313
),

tests/color/ann_multiline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn case() {
1515
.group(
1616
Group::new().element(
1717
Snippet::source(source)
18-
.origin("src/display_list.rs")
18+
.path("src/display_list.rs")
1919
.line_start(139)
2020
.fold(false)
2121
.annotation(

tests/color/ann_multiline2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ to exactly one character on next line.
1515
.group(
1616
Group::new().element(
1717
Snippet::source(source)
18-
.origin("foo.txt")
18+
.path("foo.txt")
1919
.line_start(26)
2020
.fold(false)
2121
.annotation(

0 commit comments

Comments
 (0)