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

Commit 26fb6e1

Browse files
Merge pull request #34 from botika/master
Removing duplicate Slice origins and surrounding empty lines
2 parents da93915 + 147a823 commit 26fb6e1

File tree

4 files changed

+79
-22
lines changed

4 files changed

+79
-22
lines changed

‎src/display_list/from_snippet.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,16 @@ fn format_annotation(annotation: snippet::Annotation<'_>) -> Vec<DisplayLine<'_>
104104
}
105105

106106
fn format_slice(
107-
mutslice: snippet::Slice<'_>,
107+
slice: snippet::Slice<'_>,
108108
is_first: bool,
109109
has_footer: bool,
110110
) -> Vec<DisplayLine<'_>> {
111111
let main_range = slice.annotations.get(0).map(|x| x.range.0);
112-
let row = slice.line_start;
113-
let origin = slice.origin.take();
114-
let mut body = format_body(slice, has_footer);
115-
let header = format_header(origin, main_range, row, &body, is_first);
112+
let origin = slice.origin;
113+
let line_start = slice.line_start;
114+
let need_empty_header = origin.is_some() || is_first;
115+
let mut body = format_body(slice, need_empty_header, has_footer);
116+
let header = format_header(origin, main_range, line_start, &body, is_first);
116117
let mut result = vec![];
117118

118119
if let Some(header) = header {
@@ -122,6 +123,12 @@ fn format_slice(
122123
result
123124
}
124125

126+
#[inline]
127+
// TODO: option_zip
128+
fn zip_opt<A, B>(a: Option<A>, b: Option<B>) -> Option<(A, B)> {
129+
a.and_then(|a| b.map(|b| (a, b)))
130+
}
131+
125132
fn format_header<'a>(
126133
origin: Option<&'a str>,
127134
main_range: Option<usize>,
@@ -135,7 +142,7 @@ fn format_header<'a>(
135142
DisplayHeaderType::Continuation
136143
};
137144

138-
if let Some(main_range) = main_range {
145+
if let Some((main_range, path)) = zip_opt(main_range, origin) {
139146
let mut col = 1;
140147

141148
for item in body {
@@ -151,21 +158,22 @@ fn format_header<'a>(
151158
row += 1;
152159
}
153160
}
154-
if let Some(path) = origin {
155-
return Some(DisplayLine::Raw(DisplayRawLine::Origin {
156-
path,
157-
pos: Some((row, col)),
158-
header_type: display_header,
159-
}));
160-
}
161+
162+
return Some(DisplayLine::Raw(DisplayRawLine::Origin {
163+
path,
164+
pos: Some((row, col)),
165+
header_type: display_header,
166+
}));
161167
}
168+
162169
if let Some(path) = origin {
163170
return Some(DisplayLine::Raw(DisplayRawLine::Origin {
164171
path,
165172
pos: None,
166173
header_type: display_header,
167174
}));
168175
}
176+
169177
None
170178
}
171179

@@ -261,7 +269,11 @@ fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
261269
new_body
262270
}
263271

264-
fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec<DisplayLine<'_>> {
272+
fn format_body(
273+
slice: snippet::Slice<'_>,
274+
need_empty_header: bool,
275+
has_footer: bool,
276+
) -> Vec<DisplayLine<'_>> {
265277
let source_len = slice.source.chars().count();
266278
if let Some(bigger) = slice.annotations.iter().find_map(|x| {
267279
if source_len < x.range.1 {
@@ -445,14 +457,17 @@ fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec<DisplayLine<'
445457
body = fold_body(body);
446458
}
447459

448-
body.insert(
449-
0,
450-
DisplayLine::Source {
451-
lineno: None,
452-
inline_marks: vec![],
453-
line: DisplaySourceLine::Empty,
454-
},
455-
);
460+
if need_empty_header {
461+
body.insert(
462+
0,
463+
DisplayLine::Source {
464+
lineno: None,
465+
inline_marks: vec![],
466+
line: DisplaySourceLine::Empty,
467+
},
468+
);
469+
}
470+
456471
if has_footer {
457472
body.push(DisplayLine::Source {
458473
lineno: None,

‎tests/fixtures/no-color/issue_9.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[title]
2+
label = "expected one of `.`, `;`, `?`, or an operator, found `for`"
3+
annotation_type = "Error"
4+
5+
[[slices]]
6+
source = "let x = vec![1];"
7+
line_start = 4
8+
origin = "/code/rust/src/test/ui/annotate-snippet/suggestion.rs"
9+
[[slices.annotations]]
10+
label = "move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait"
11+
annotation_type = "Warning"
12+
range = [4, 5]
13+
14+
[[slices]]
15+
source = "let y = x;"
16+
line_start = 7
17+
[[slices.annotations]]
18+
label = "value moved here"
19+
annotation_type = "Warning"
20+
range = [8, 9]
21+
22+
[[slices]]
23+
source = "x;"
24+
line_start = 9
25+
[[slices.annotations]]
26+
label = "value used here after move"
27+
annotation_type = "Error"
28+
range = [0, 1]

‎tests/fixtures/no-color/issue_9.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: expected one of `.`, `;`, `?`, or an operator, found `for`
2+
--> /code/rust/src/test/ui/annotate-snippet/suggestion.rs:4:5
3+
|
4+
4 | let x = vec![1];
5+
| - move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
6+
|
7+
7 | let y = x;
8+
| - value moved here
9+
|
10+
9 | x;
11+
| ^ value used here after move
12+
|

‎tests/snippet/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ where
5353
#[derive(Deserialize)]
5454
#[serde(remote = "FormatOptions")]
5555
pub struct FormatOptionsDef {
56+
#[serde(default)]
5657
pub color: bool,
58+
#[serde(default)]
5759
pub anonymized_line_numbers: bool,
5860
}
5961

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /