Skip to content

Commit 46c5588

Browse files
authored
Merge pull request #37 from botika/master
Fix character split when strip code
2 parents dd3a5ca + 65fced1 commit 46c5588

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/formatter/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
cmp,
33
fmt::{self, Display, Write},
4+
iter::once,
45
};
56

67
pub mod style;
@@ -217,19 +218,30 @@ impl<'a> DisplayList<'a> {
217218
} else {
218219
false
219220
};
221+
// Specifies that it will end on the next character, so it will return
222+
// until the next one to the final condition.
223+
let mut ended = false;
220224
let range = text
221225
.char_indices()
222226
.skip(left)
227+
// Complete char iterator with final character
228+
.chain(once((text.len(), '\0')))
229+
// Take until the next one to the final condition
223230
.take_while(|(_, ch)| {
231+
// Fast return to iterate over final byte position
232+
if ended {
233+
return false;
234+
}
224235
// Make sure that the trimming on the right will fall within the terminal width.
225236
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is.
226237
// For now, just accept that sometimes the code line will be longer than desired.
227238
taken += unicode_width::UnicodeWidthChar::width(*ch).unwrap_or(1);
228239
if taken > right - left {
229-
return false;
240+
ended = true;
230241
}
231242
true
232243
})
244+
// Reduce to start and end byte position
233245
.fold((None, 0), |acc, (i, _)| {
234246
if acc.0.is_some() {
235247
(acc.0, i)
@@ -238,7 +250,8 @@ impl<'a> DisplayList<'a> {
238250
}
239251
});
240252

241-
text[range.0.expect("One character at line")..=range.1].fmt(f)?;
253+
// Format text with margins
254+
text[range.0.expect("One character at line")..range.1].fmt(f)?;
242255

243256
if cut_right {
244257
// We have stripped some code after the right-most span end, make it clear we did so.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[title]
2+
id = "E0308"
3+
label = "mismatched types"
4+
annotation_type = "Error"
5+
6+
[[slices]]
7+
source = " let _: () = 42ñ"
8+
line_start = 4
9+
origin = "$DIR/whitespace-trimming.rs"
10+
11+
[[slices.annotations]]
12+
label = "expected (), found integer"
13+
annotation_type = "Error"
14+
range = [192, 194]
15+
16+
[opt]
17+
color = false
18+
anonymized_line_numbers = true
19+
[opt.margin]
20+
whitespace_left = 180
21+
span_left = 192
22+
span_right = 194
23+
label_right = 221
24+
column_width = 140
25+
max_line_len = 195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/whitespace-trimming.rs:4:193
3+
|
4+
LL | ... let _: () = 42ñ
5+
| ^^ expected (), found integer
6+
|

0 commit comments

Comments
 (0)