From: Simon Tatham Date: Sat, 6 Jan 2024 11:57:40 +0000 (+0000) Subject: Fix clipping of initial file positions. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=f972070d04bcbb5c62116dafa59dcc72ea131911;p=mastodonochrome.git Fix clipping of initial file positions. It's not enough to just modify the item index - we also have to reset the line number. --- diff --git a/src/file.rs b/src/file.rs index 408eb93..2df8251 100644 --- a/src/file.rs +++ b/src/file.rs @@ -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 File { 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,