chiark / gitweb /
Fix clipping of initial file positions.
authorSimon Tatham <anakin@pobox.com>
Sat, 6 Jan 2024 11:57:40 +0000 (11:57 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 6 Jan 2024 11:59:18 +0000 (11:59 +0000)
It's not enough to just modify the item index - we also have to reset
the line number.

src/file.rs

index 408eb93d724160a3dcb8727520c0366b5ad65dad..2df82515972efab55bd3f5e8273a8e5a8059e204 100644 (file)
@@ -38,6 +38,21 @@ impl FilePosition {
     fn item_bottom(item: isize) -> Self {
         FilePosition { item, line: 1, width: None }
     }
+
+    fn clip(self, first_index: isize, last_index: isize) -> Self {
+        let pos = self;
+        let pos = if pos.item < first_index {
+            Self::item_top(first_index)
+        } else {
+            pos
+        };
+        let pos = if pos.item > last_index {
+            Self::item_bottom(last_index)
+        } else {
+            pos
+        };
+        pos
+    }
 }
 
 trait FileDataSource {
@@ -325,8 +340,8 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
         let mut initial_pos = initial_pos.unwrap_or_else(
             || FilePosition::item_bottom(isize::MAX));
 
-        initial_pos.item = max(initial_pos.item, contents.first_index());
-        initial_pos.item = min(initial_pos.item, contents.last_index());
+        initial_pos = initial_pos.clip(
+            contents.first_index(), contents.last_index());
 
         let ff = File {
             contents,