Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore view offset when switching buffers with multiple windows #7568

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -131,6 +132,7 @@ pub struct Document {
pub(crate) id: DocumentId,
text: Rope,
selections: HashMap<ViewId, Selection>,
view_data: HashMap<ViewId, ViewData>,

/// Inlay hints annotations for the document, by view.
///
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
Comment on lines +1621 to +1627
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these should return Option<&[mut ]Viewdata> instead? That would allow us to drop the Default requirement for ViewData. It's not currently useful to do that between this and #6198 though so I don't have a strong opinion either way


pub fn relative_path(&self) -> Option<PathBuf> {
self.path
.as_deref()
Expand Down Expand Up @@ -1788,6 +1800,11 @@ impl Document {
}
}

#[derive(Debug, Default)]
pub struct ViewData {
pub view_position: ViewPosition,
}

#[derive(Clone, Debug)]
pub enum FormatterError {
SpawningFailed {
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::{
align_view,
clipboard::{get_clipboard_provider, ClipboardProvider},
document::{DocumentSavedEventFuture, DocumentSavedEventResult, Mode, SavePoint},
graphics::{CursorKind, Rect},
info::Info,
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;
Expand Down Expand Up @@ -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) {
Expand Down