From 5aa1db8183891df057bd0630d055d7cdf39bdb65 Mon Sep 17 00:00:00 2001 From: t-monaghan Date: Sat, 8 Jul 2023 12:30:01 +1000 Subject: [PATCH 1/2] ViewData struct in style of dead10ck's changes see https://github.com/helix-editor/helix/commit/599bb29f26d3051cec24ea6c1fcd16ff82199b4e --- helix-view/src/document.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index b08370f9f371..c2dee6b83476 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -34,6 +34,7 @@ use helix_core::{ }; use crate::editor::{Config, RedrawHandle}; +use crate::view::ViewPosition; use crate::{DocumentId, Editor, Theme, View, ViewId}; /// 8kB of buffer space for encoding and decoding `Rope`s. @@ -131,6 +132,7 @@ pub struct Document { pub(crate) id: DocumentId, text: Rope, selections: HashMap, + view_data: HashMap, /// Inlay hints annotations for the document, by view. /// @@ -263,6 +265,7 @@ impl fmt::Debug for Document { .field("selections", &self.selections) .field("inlay_hints_oudated", &self.inlay_hints_oudated) .field("text_annotations", &self.inlay_hints) + .field("view_data", &self.view_data) .field("path", &self.path) .field("encoding", &self.encoding) .field("restore_cursor", &self.restore_cursor) @@ -654,6 +657,7 @@ impl Document { selections: HashMap::default(), inlay_hints: HashMap::default(), inlay_hints_oudated: false, + view_data: Default::default(), indent_style: DEFAULT_INDENT, line_ending, restore_cursor: false, @@ -1614,6 +1618,14 @@ impl Document { &self.selections } + pub fn view_data(&mut self, view_id: ViewId) -> &ViewData { + self.view_data.entry(view_id).or_default() + } + + pub fn view_data_mut(&mut self, view_id: ViewId) -> &mut ViewData { + self.view_data.entry(view_id).or_default() + } + pub fn relative_path(&self) -> Option { self.path .as_deref() @@ -1788,6 +1800,11 @@ impl Document { } } +#[derive(Debug, Default)] +pub struct ViewData { + pub view_position: ViewPosition, +} + #[derive(Clone, Debug)] pub enum FormatterError { SpawningFailed { @@ -1837,6 +1854,7 @@ mod test { Arc::new(ArcSwap::new(Arc::new(Config::default()))), ); let view = ViewId::default(); + doc.ensure_view_init(view); doc.set_selection(view, Selection::single(0, 0)); let transaction = @@ -1875,6 +1893,7 @@ mod test { Arc::new(ArcSwap::new(Arc::new(Config::default()))), ); let view = ViewId::default(); + doc.ensure_view_init(view); doc.set_selection(view, Selection::single(5, 5)); // insert From 411c4e772053445f2e15069cff136c309fe32963 Mon Sep 17 00:00:00 2001 From: t-monaghan Date: Sat, 8 Jul 2023 12:43:13 +1000 Subject: [PATCH 2/2] view position updates based on view id --- helix-view/src/editor.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 630ea5cf987b..3194d345cb88 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,5 +1,4 @@ use crate::{ - align_view, clipboard::{get_clipboard_provider, ClipboardProvider}, document::{DocumentSavedEventFuture, DocumentSavedEventResult, Mode, SavePoint}, graphics::{CursorKind, Rect}, @@ -7,8 +6,7 @@ use crate::{ input::KeyEvent, theme::{self, Theme}, tree::{self, Tree}, - view::ViewPosition, - Align, Document, DocumentId, View, ViewId, + Document, DocumentId, View, ViewId, }; use dap::StackFrame; use helix_vcs::DiffProviderRegistry; @@ -1252,16 +1250,22 @@ impl Editor { } fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) { + let scrolloff = self.config().scrolloff; let view = self.tree.get_mut(current_view); + + if let Some(old_doc) = self.documents.get_mut(&view.doc) { + old_doc.view_data_mut(current_view).view_position = view.offset; + } + view.doc = doc_id; - view.offset = ViewPosition::default(); let doc = doc_mut!(self, &doc_id); + view.offset = doc.view_data(current_view).view_position; doc.ensure_view_init(view.id); view.sync_changes(doc); doc.mark_as_focused(); - align_view(doc, view, Align::Center); + view.ensure_cursor_in_view(doc, scrolloff); } pub fn switch(&mut self, id: DocumentId, action: Action) {