diff --git a/Cargo.lock b/Cargo.lock index 0598a5dbe3f..e9da353a21e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,9 +43,9 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "annotate-snippets" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "361e18e35c1879cfeba9d8e20c393c84285ac883eaffc71c743c8e9ba023f0f4" +checksum = "238760b2324c811147d933e41f1743e4a8e309c8f3a15f417232e5980e5ce765" dependencies = [ "anstyle", "unicode-width", diff --git a/Cargo.toml b/Cargo.toml index aea0be44de8..26e4e81256d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ homepage = "https://github.com/rust-lang/cargo" repository = "https://github.com/rust-lang/cargo" [workspace.dependencies] -annotate-snippets = "0.11.0" +annotate-snippets = "0.11.1" anstream = "0.6.13" anstyle = "1.0.6" anyhow = "1.0.80" diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 05e58bdf2fa..3898695fbe7 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -123,36 +123,16 @@ fn emit_diagnostic( return e.into(); }; - let line_num = get_line(&contents, span.start); - let source_start = contents[0..span.start] - .as_bytes() - .iter() - .rposition(|b| b == &b'\n') - .map(|s| s + 1) - .unwrap_or(0); - let source_end = contents[span.end.saturating_sub(1)..] - .as_bytes() - .iter() - .position(|b| b == &b'\n') - .map(|s| s + span.end) - .unwrap_or(contents.len()); - let source = &contents[source_start..source_end]; - let highlight_start = span.start - source_start; - // Make sure we don't try to highlight past the end of the line, - // but also make sure we are highlighting at least one character - let highlight_end = (span.end - source_start) - .min(source_end - source_start) - .max(highlight_start + 1); // Get the path to the manifest, relative to the cwd let manifest_path = diff_paths(manifest_file, gctx.cwd()) .unwrap_or_else(|| manifest_file.to_path_buf()) .display() .to_string(); let message = Level::Error.title(e.message()).snippet( - Snippet::source(&source) + Snippet::source(contents) .origin(&manifest_path) - .line_start(line_num + 1) - .annotation(Level::Error.span(highlight_start..highlight_end)), + .fold(true) + .annotation(Level::Error.span(span)), ); let renderer = Renderer::styled().term_width( gctx.shell() @@ -2363,28 +2343,3 @@ impl ResolveToPath for ConfigRelativePath { self.resolve_path(gctx) } } - -fn get_line(input: &str, index: usize) -> usize { - if input.is_empty() { - return 0; - } - - let safe_index = index.min(input.len() - 1); - - let nl = input[0..safe_index] - .as_bytes() - .iter() - .rev() - .enumerate() - .find(|(_, b)| **b == b'\n') - .map(|(nl, _)| safe_index - nl - 1); - let line_start = match nl { - Some(nl) => nl + 1, - None => 0, - }; - input[0..line_start] - .as_bytes() - .iter() - .filter(|c| **c == b'\n') - .count() -}