about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAriel Davis <ariel.z.davis@icloud.com>2023-05-06 01:05:28 -0700
committerAriel Davis <ariel.z.davis@icloud.com>2023-05-06 01:05:28 -0700
commit0ad2450396ea72bff341c65dbf24745e1e185aad (patch)
treebcdc75b98a3c52a62871ce63d2dd39a4030959e5
parent902b3438c97067c4fddf4eb7e429b8fd82cfe5b7 (diff)
downloadrust-0ad2450396ea72bff341c65dbf24745e1e185aad.tar.gz
rust-0ad2450396ea72bff341c65dbf24745e1e185aad.zip
Check for inside multibyte
-rw-r--r--lib/line-index/src/lib.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs
index c0e526a8e0f..2494975f9fb 100644
--- a/lib/line-index/src/lib.rs
+++ b/lib/line-index/src/lib.rs
@@ -144,12 +144,20 @@ impl LineIndex {
         self.try_line_col(offset).expect("invalid offset")
     }
 
-    /// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid.
+    /// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid,
+    /// e.g. if it points to the middle of a multi-byte character.
     pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> {
         let line = self.newlines.partition_point(|&it| it <= offset).checked_sub(1)?;
         let line_start_offset = self.newlines.get(line)?;
         let col = offset - line_start_offset;
-        Some(LineCol { line: line as u32, col: col.into() })
+        let ret = LineCol { line: line as u32, col: col.into() };
+        self.line_wide_chars
+            .get(&ret.line)
+            .into_iter()
+            .flat_map(|it| it.iter())
+            .find(|it| it.start < col && col < it.end)
+            .is_none()
+            .then_some(ret)
     }
 
     /// Transforms the `LineCol` into a `TextSize`.