Skip to content

Commit b8b7fa5

Browse files
committed
Auto merge of #13619 - epage:fold, r=Muscraft
refactor(toml): Push diagnostic complexity on annotate-snippets
2 parents c3ad639 + 69bbb2c commit b8b7fa5

File tree

3 files changed

+6
-51
lines changed

3 files changed

+6
-51
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
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.11.0"
21+
annotate-snippets = "0.11.1"
2222
anstream = "0.6.13"
2323
anstyle = "1.0.6"
2424
anyhow = "1.0.80"

src/cargo/util/toml/mod.rs

+3-48
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,16 @@ fn emit_diagnostic(
123123
return e.into();
124124
};
125125

126-
let line_num = get_line(&contents, span.start);
127-
let source_start = contents[0..span.start]
128-
.as_bytes()
129-
.iter()
130-
.rposition(|b| b == &b'\n')
131-
.map(|s| s + 1)
132-
.unwrap_or(0);
133-
let source_end = contents[span.end.saturating_sub(1)..]
134-
.as_bytes()
135-
.iter()
136-
.position(|b| b == &b'\n')
137-
.map(|s| s + span.end)
138-
.unwrap_or(contents.len());
139-
let source = &contents[source_start..source_end];
140-
let highlight_start = span.start - source_start;
141-
// Make sure we don't try to highlight past the end of the line,
142-
// but also make sure we are highlighting at least one character
143-
let highlight_end = (span.end - source_start)
144-
.min(source_end - source_start)
145-
.max(highlight_start + 1);
146126
// Get the path to the manifest, relative to the cwd
147127
let manifest_path = diff_paths(manifest_file, gctx.cwd())
148128
.unwrap_or_else(|| manifest_file.to_path_buf())
149129
.display()
150130
.to_string();
151131
let message = Level::Error.title(e.message()).snippet(
152-
Snippet::source(&source)
132+
Snippet::source(contents)
153133
.origin(&manifest_path)
154-
.line_start(line_num + 1)
155-
.annotation(Level::Error.span(highlight_start..highlight_end)),
134+
.fold(true)
135+
.annotation(Level::Error.span(span)),
156136
);
157137
let renderer = Renderer::styled().term_width(
158138
gctx.shell()
@@ -2363,28 +2343,3 @@ impl ResolveToPath for ConfigRelativePath {
23632343
self.resolve_path(gctx)
23642344
}
23652345
}
2366-
2367-
fn get_line(input: &str, index: usize) -> usize {
2368-
if input.is_empty() {
2369-
return 0;
2370-
}
2371-
2372-
let safe_index = index.min(input.len() - 1);
2373-
2374-
let nl = input[0..safe_index]
2375-
.as_bytes()
2376-
.iter()
2377-
.rev()
2378-
.enumerate()
2379-
.find(|(_, b)| **b == b'\n')
2380-
.map(|(nl, _)| safe_index - nl - 1);
2381-
let line_start = match nl {
2382-
Some(nl) => nl + 1,
2383-
None => 0,
2384-
};
2385-
input[0..line_start]
2386-
.as_bytes()
2387-
.iter()
2388-
.filter(|c| **c == b'\n')
2389-
.count()
2390-
}

0 commit comments

Comments
 (0)