From 23ee43ae550fcf13f3476db9974e1af964d951b7 Mon Sep 17 00:00:00 2001 From: Ingrid Date: Fri, 26 Apr 2024 21:40:52 +0200 Subject: [PATCH] introduce helper functions on Document .view_offset and .set_view_offset --- helix-term/src/commands.rs | 12 +++++------- helix-term/src/ui/editor.rs | 8 ++++---- helix-view/src/document.rs | 8 ++++++++ helix-view/src/view.rs | 6 +++--- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 474f422c1fa5a..7d2e1978b059e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1023,7 +1023,7 @@ fn goto_window(cx: &mut Context, align: Align) { let count = cx.count() - 1; let config = cx.editor.config(); let (view, doc) = current!(cx.editor); - let view_offset = doc.view_data_mut(view.id).view_position; + let view_offset = doc.view_offset(view.id); let height = view.inner_height(); @@ -1668,7 +1668,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor use Direction::*; let config = cx.editor.config(); let (view, doc) = current!(cx.editor); - let view_offset = doc.view_data_mut(view.id).view_position; + let mut view_offset = doc.view_offset(view.id).to_owned(); let range = doc.selection(view.id).primary(); let text = doc.text().slice(..); @@ -1686,7 +1686,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor let viewport = view.inner_area(doc); let text_fmt = doc.text_format(viewport.width, None); let annotations = view.text_annotations(&*doc, None); - let (new_anchor, new_vertical_offset) = char_idx_at_visual_offset( + (view_offset.anchor, view_offset.vertical_offset) = char_idx_at_visual_offset( doc_text, view_offset.anchor, view_offset.vertical_offset as isize + offset, @@ -1694,9 +1694,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor &text_fmt, &annotations, ); - let view_data = doc.view_data_mut(view.id); - view_data.view_position.anchor = new_anchor; - view_data.view_position.vertical_offset = new_vertical_offset; + doc.set_view_offset(view.id, view_offset); let doc_text = doc.text().slice(..); let mut annotations = view.text_annotations(&*doc, None); @@ -1725,7 +1723,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor return; } - let view_offset = doc.view_data(view.id).view_position; + let view_offset = doc.view_offset(view.id); let mut head; match direction { diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index f757f2536446e..0403e2c378704 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -93,7 +93,7 @@ impl EditorView { let theme = &editor.theme; let config = editor.config(); - let view_offset = doc.view_data(view.id).view_position; + let view_offset = doc.view_offset(view.id); let text_annotations = view.text_annotations(doc, Some(theme)); let mut line_decorations: Vec> = Vec::new(); @@ -198,7 +198,7 @@ impl EditorView { surface, inner, doc, - view_offset, + view_offset.to_owned(), &text_annotations, syntax_highlights, overlay_highlights, @@ -251,7 +251,7 @@ impl EditorView { .and_then(|config| config.rulers.as_ref()) .unwrap_or(editor_rulers); - let view_offset = doc.view_data(view.id).view_position; + let view_offset = doc.view_offset(view.id); rulers .iter() @@ -827,7 +827,7 @@ impl EditorView { let inner_area = view.inner_area(doc); let selection = doc.selection(view.id); - let view_offset = doc.view_data(view.id).view_position; + let view_offset = doc.view_offset(view.id); let primary = selection.primary(); let text_format = doc.text_format(viewport.width, None); for range in selection.iter() { diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index e0a1b191ff6f9..26a97ffa49336 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1747,6 +1747,14 @@ impl Document { self.view_data.entry(view_id).or_default() } + pub fn view_offset(&self, view_id: ViewId) -> &ViewPosition { + &self.view_data(view_id).view_position + } + + pub fn set_view_offset(&mut self, view_id: ViewId, new_offset: ViewPosition) { + self.view_data_mut(view_id).view_position = new_offset; + } + pub fn relative_path(&self) -> Option> { self.path .as_deref() diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 87310496070db..b410af5365224 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -341,7 +341,7 @@ impl View { let viewport = self.inner_area(doc); let text_fmt = doc.text_format(viewport.width, None); let annotations = self.text_annotations(doc, None); - let view_offset = doc.view_data(self.id).view_position; + let view_offset = doc.view_offset(self.id); // last visual line in view is trivial to compute let visual_height = @@ -378,7 +378,7 @@ impl View { text: RopeSlice, pos: usize, ) -> Option { - let view_offset = doc.view_data(self.id).view_position; + let view_offset = doc.view_offset(self.id); if pos < view_offset.anchor { // Line is not visible on screen @@ -500,7 +500,7 @@ impl View { ignore_virtual_text: bool, ) -> Option { let text = doc.text().slice(..); - let view_offset = doc.view_data(self.id).view_position; + let view_offset = doc.view_offset(self.id); let text_row = row as usize + view_offset.vertical_offset; let text_col = column as usize + view_offset.horizontal_offset;