diff --git a/src/fallback.rs b/src/fallback.rs index db52959..352b459 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -364,8 +364,13 @@ impl FileInfo { fn source_text(&self, span: Span) -> String { let lo = (span.lo - self.span.lo) as usize; - let hi = (span.hi - self.span.lo) as usize; - self.source_text[lo..hi].to_owned() + let trunc_lo = &self.source_text[lo..]; + let char_len = (span.hi - span.lo) as usize; + let source_text = match trunc_lo.char_indices().nth(char_len) { + Some((offset, _ch)) => &trunc_lo[..offset], + None => trunc_lo, + }; + source_text.to_owned() } } diff --git a/tests/test.rs b/tests/test.rs index a1ea72a..4a6ca75 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -331,7 +331,7 @@ fn source_text() { let input = " 𓀕 "; let tokens = input.parse::().unwrap(); let ident = tokens.into_iter().next().unwrap(); - assert_eq!("𓀕", ident.span().source_text().unwrap()); // FIXME + assert_eq!("𓀕", ident.span().source_text().unwrap()); } #[test]