Skip to content

Commit f0ae765

Browse files
committed
Auto merge of #13609 - Muscraft:update-annotate-snippets, r=epage
Update annotate snippets This updates `annotate-snippets` to `0.11.0`, which introduces the new builder API as well as a few rendering fixes.
2 parents 5b776ec + f983e20 commit f0ae765

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ homepage = "https://github.com/rust-lang/cargo"
1818
repository = "https://github.com/rust-lang/cargo"
1919

2020
[workspace.dependencies]
21-
annotate-snippets = "0.10.2"
21+
annotate-snippets = "0.11.0"
2222
anstream = "0.6.13"
2323
anstyle = "1.0.6"
2424
anyhow = "1.0.80"

src/cargo/util/toml/mod.rs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
1+
use annotate_snippets::{Level, Renderer, Snippet};
22
use std::collections::{BTreeMap, BTreeSet, HashMap};
33
use std::ffi::OsStr;
44
use std::path::{Path, PathBuf};
@@ -123,47 +123,44 @@ fn emit_diagnostic(
123123
return e.into();
124124
};
125125

126-
let (line_num, column) = translate_position(&contents, span.start);
126+
let line_num = get_line(&contents, span.start);
127127
let source_start = contents[0..span.start]
128-
.rfind('\n')
128+
.as_bytes()
129+
.iter()
130+
.rposition(|b| b == &b'\n')
129131
.map(|s| s + 1)
130132
.unwrap_or(0);
131133
let source_end = contents[span.end.saturating_sub(1)..]
132-
.find('\n')
134+
.as_bytes()
135+
.iter()
136+
.position(|b| b == &b'\n')
133137
.map(|s| s + span.end)
134138
.unwrap_or(contents.len());
135139
let source = &contents[source_start..source_end];
140+
let highlight_start = span.start - source_start;
136141
// Make sure we don't try to highlight past the end of the line,
137142
// but also make sure we are highlighting at least one character
138-
let highlight_end = (column + contents[span].chars().count())
139-
.min(source.len())
140-
.max(column + 1);
143+
let highlight_end = (span.end - source_start)
144+
.min(source_end - source_start)
145+
.max(highlight_start + 1);
141146
// Get the path to the manifest, relative to the cwd
142147
let manifest_path = diff_paths(manifest_file, gctx.cwd())
143148
.unwrap_or_else(|| manifest_file.to_path_buf())
144149
.display()
145150
.to_string();
146-
let snippet = Snippet {
147-
title: Some(Annotation {
148-
id: None,
149-
label: Some(e.message()),
150-
annotation_type: AnnotationType::Error,
151-
}),
152-
footer: vec![],
153-
slices: vec![Slice {
154-
source: &source,
155-
line_start: line_num + 1,
156-
origin: Some(manifest_path.as_str()),
157-
annotations: vec![SourceAnnotation {
158-
range: (column, highlight_end),
159-
label: "",
160-
annotation_type: AnnotationType::Error,
161-
}],
162-
fold: false,
163-
}],
164-
};
165-
let renderer = Renderer::styled();
166-
if let Err(err) = writeln!(gctx.shell().err(), "{}", renderer.render(snippet)) {
151+
let message = Level::Error.title(e.message()).snippet(
152+
Snippet::source(&source)
153+
.origin(&manifest_path)
154+
.line_start(line_num + 1)
155+
.annotation(Level::Error.span(highlight_start..highlight_end)),
156+
);
157+
let renderer = Renderer::styled().term_width(
158+
gctx.shell()
159+
.err_width()
160+
.diagnostic_terminal_width()
161+
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH),
162+
);
163+
if let Err(err) = writeln!(gctx.shell().err(), "{}", renderer.render(message)) {
167164
return err.into();
168165
}
169166
return AlreadyPrintedError::new(e.into()).into();
@@ -2367,13 +2364,12 @@ impl ResolveToPath for ConfigRelativePath {
23672364
}
23682365
}
23692366

2370-
fn translate_position(input: &str, index: usize) -> (usize, usize) {
2367+
fn get_line(input: &str, index: usize) -> usize {
23712368
if input.is_empty() {
2372-
return (0, index);
2369+
return 0;
23732370
}
23742371

23752372
let safe_index = index.min(input.len() - 1);
2376-
let column_offset = index - safe_index;
23772373

23782374
let nl = input[0..safe_index]
23792375
.as_bytes()
@@ -2386,13 +2382,9 @@ fn translate_position(input: &str, index: usize) -> (usize, usize) {
23862382
Some(nl) => nl + 1,
23872383
None => 0,
23882384
};
2389-
let line = input[0..line_start]
2385+
input[0..line_start]
23902386
.as_bytes()
23912387
.iter()
23922388
.filter(|c| **c == b'\n')
2393-
.count();
2394-
let column = input[line_start..=safe_index].chars().count() - 1;
2395-
let column = column + column_offset;
2396-
2397-
(line, column)
2389+
.count()
23982390
}

0 commit comments

Comments
 (0)