Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: Properly handle annotating leading whitespace#334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletionsrc/renderer/margin.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -71,7 +71,12 @@ impl Margin {

if self.computed_right - self.computed_left > self.term_width {
// Trimming only whitespace isn't enough, let's get craftier.
if self.label_right - self.whitespace_left <= self.term_width {
if self.label_right.saturating_sub(self.whitespace_left) <= self.term_width
// Trimming whitespace when the right-most label is somewhrere
// within it would result in the label pointing to the wrong
// place
&& self.label_right >= self.whitespace_left
{
// Attempt to fit the code window only trimming whitespace.
self.computed_left = self.whitespace_left;
self.computed_right = self.computed_left + self.term_width;
Expand Down
5 changes: 0 additions & 5 deletionssrc/renderer/render.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -622,11 +622,6 @@ fn render_snippet_annotations(
// Get the left-side margin to remove it
let mut whitespace_margin = usize::MAX;
for line_info in annotated_lines {
// Whitespace can only be removed (aka considered leading)
// if the lexer considers it whitespace.
// non-rustc_lexer::is_whitespace() chars are reported as an
// error (ex. no-break-spaces \u{a0}), and thus can't be considered
// for removal during error reporting.
let leading_whitespace = line_info
.line
.chars()
Expand Down
52 changes: 52 additions & 0 deletionstests/rustc_tests.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5646,3 +5646,55 @@ LL │ static ROOK_ATTACKS_TABLE: () = {
let renderer = renderer.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}

#[test]
fn emitter_overflow_bad_whitespace() {
// tests/ui/errors/emitter-overflow-bad-whitespace.rs
let source = r#"                                        fn main() { return; }
"#;
let title_0 = "Unicode character ' ' (No-Break Space) looks like ' ' (Space), but it is not";

let report = &[
Group::with_title(Level::ERROR.primary_title("unknown start of token: \u{a0}")).element(
Snippet::source(source)
.path("$DIR/emitter-overflow-bad-whitespace.rs")
.line_start(10)
.annotation(AnnotationKind::Primary.span(0..2)),
),
Group::with_title(Level::HELP.secondary_title(title_0)).element(
Snippet::source(source)
.path("$DIR/emitter-overflow-bad-whitespace.rs")
.line_start(10)
.patch(Patch::new(0..2, " ")),
),
];
let expected_ascii = str![[r#"
error: unknown start of token:  
--> $DIR/emitter-overflow-bad-whitespace.rs:10:1
|
10 |     ...
| ^
|
help: Unicode character ' ' (No-Break Space) looks like ' ' (Space), but it is not
|
10 |                                       fn main() { return; }
| +
"#]];
let renderer_ascii = Renderer::plain().term_width(1);
assert_data_eq!(renderer_ascii.render(report), expected_ascii);

let expected_unicode = str![[r#"
error: unknown start of token:  
╭▸ $DIR/emitter-overflow-bad-whitespace.rs:10:1
10 │       …
│ ━
╰╴
help: Unicode character ' ' (No-Break Space) looks like ' ' (Space), but it is not
╭╴
10 │                                       fn main() { return; }
╰╴+
"#]];
let renderer_unicode = renderer_ascii.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer_unicode.render(report), expected_unicode);
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp