Skip to content

Commit 137ae0a

Browse files
committed
Fix source_text treating span.lo as byte offset not char index
1 parent 4c0bd28 commit 137ae0a

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

src/fallback.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ impl FileInfo {
364364

365365
fn source_text(&self, span: Span) -> String {
366366
let lo = (span.lo - self.span.lo) as usize;
367-
let trunc_lo = &self.source_text[lo..];
367+
let trunc_lo = match self.source_text.char_indices().nth(lo) {
368+
Some((offset, _ch)) => &self.source_text[offset..],
369+
None => return String::new(),
370+
};
368371
let char_len = (span.hi - span.lo) as usize;
369372
let source_text = match trunc_lo.char_indices().nth(char_len) {
370373
Some((offset, _ch)) => &trunc_lo[..offset],

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ fn source_text() {
338338
assert_eq!("𓀕", ident.span().source_text().unwrap());
339339

340340
let ident = tokens.next().unwrap();
341-
assert_eq!("c", ident.span().source_text().unwrap()); // FIXME
341+
assert_eq!("c", ident.span().source_text().unwrap());
342342
}
343343

344344
#[test]

0 commit comments

Comments
 (0)