diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index c8e8ecb1ae61..791f04457437 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -397,7 +397,7 @@ impl Application { // reset view position in case softwrap was enabled/disabled let scrolloff = self.editor.config().scrolloff; for (view, _) in self.editor.tree.views_mut() { - let doc = &self.editor.documents[&view.doc]; + let doc = &self.editor.documents[&view.doc_id]; view.ensure_cursor_in_view(doc, scrolloff) } } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index fb55ca2a8e72..6fc1a19ab93d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -742,7 +742,7 @@ fn goto_previous_buffer(cx: &mut Context) { } fn goto_buffer(editor: &mut Editor, direction: Direction) { - let current = view!(editor).doc; + let current = view!(editor).doc_id; let id = match direction { Direction::Forward => { @@ -2432,7 +2432,7 @@ fn file_picker_in_current_directory(cx: &mut Context) { } fn buffer_picker(cx: &mut Context) { - let current = view!(cx.editor).doc; + let current = view!(cx.editor).doc_id; struct BufferMeta { id: DocumentId, @@ -2547,7 +2547,7 @@ fn jumplist_picker(cx: &mut Context) { path: doc.and_then(|d| d.path().cloned()), selection, text, - is_current: view.doc == doc_id, + is_current: view.doc_id == doc_id, } }; @@ -2640,7 +2640,7 @@ pub fn command_palette(cx: &mut Context) { let config = ctx.editor.config(); let mode = ctx.editor.mode(); let view = view_mut!(ctx.editor, focus); - let doc = doc_mut!(ctx.editor, &view.doc); + let doc = doc_mut!(ctx.editor, &view.doc_id); view.ensure_cursor_in_view(doc, config.scrolloff); @@ -2899,7 +2899,7 @@ fn goto_last_modified_file(cx: &mut Context) { .last_modified_docs .into_iter() .flatten() - .find(|&id| id != view.doc); + .find(|&id| id != view.doc_id); if let Some(alt) = alternate_file { cx.editor.switch(alt, Action::Replace); } else { @@ -4397,10 +4397,10 @@ fn jump_forward(cx: &mut Context) { let count = cx.count(); let config = cx.editor.config(); let view = view_mut!(cx.editor); - let doc_id = view.doc; + let doc_id = view.doc_id; if let Some((id, selection)) = view.jumps.forward(count) { - view.doc = *id; + view.doc_id = *id; let selection = selection.clone(); let (view, doc) = current!(cx.editor); // refetch doc @@ -4420,7 +4420,7 @@ fn jump_backward(cx: &mut Context) { let doc_id = doc.id(); if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) { - view.doc = *id; + view.doc_id = *id; let selection = selection.clone(); let (view, doc) = current!(cx.editor); // refetch doc diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index b0fd18a76b79..acf9a93a9fa4 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -131,7 +131,7 @@ fn buffer_close_by_ids_impl( fn buffer_gather_paths_impl(editor: &mut Editor, args: &[Cow]) -> Vec { // No arguments implies current document if args.is_empty() { - let doc_id = view!(editor).doc; + let doc_id = view!(editor).doc_id; return vec![doc_id]; } @@ -1222,7 +1222,7 @@ fn reload_all( for view_id in view_ids { let view = view_mut!(cx.editor, view_id); - if view.doc.eq(&doc_id) { + if view.doc_id.eq(&doc_id) { view.ensure_cursor_in_view(doc, scrolloff); } } @@ -1396,7 +1396,7 @@ fn vsplit( return Ok(()); } - let id = view!(cx.editor).doc; + let id = view!(cx.editor).doc_id; if args.is_empty() { cx.editor.switch(id, Action::VerticalSplit); @@ -1419,7 +1419,7 @@ fn hsplit( return Ok(()); } - let id = view!(cx.editor).doc; + let id = view!(cx.editor).doc_id; if args.is_empty() { cx.editor.switch(id, Action::HorizontalSplit); diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 59f371bda5dc..b9437817c8db 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -544,7 +544,7 @@ impl EditorView { .unwrap_or_else(|| editor.theme.get("ui.statusline.inactive")); let mut x = viewport.x; - let current_doc = view!(editor).doc; + let current_doc = view!(editor).doc_id; for doc in editor.documents() { let fname = doc @@ -1013,7 +1013,7 @@ impl EditorView { let pos_and_view = |editor: &Editor, row, column| { editor.tree.views().find_map(|(view, _focus)| { - view.pos_at_screen_coords(&editor.documents[&view.doc], row, column, true) + view.pos_at_screen_coords(&editor.documents[&view.doc_id], row, column, true) .map(|pos| (pos, view.id)) }) }; @@ -1030,7 +1030,7 @@ impl EditorView { let editor = &mut cxt.editor; if let Some((pos, view_id)) = pos_and_view(editor, row, column) { - let doc = doc_mut!(editor, &view!(editor, view_id).doc); + let doc = doc_mut!(editor, &view!(editor, view_id).doc_id); if modifiers == KeyModifiers::ALT { let selection = doc.selection(view_id).clone(); @@ -1165,7 +1165,7 @@ impl EditorView { } if let Some((pos, view_id)) = pos_and_view(editor, row, column) { - let doc = doc_mut!(editor, &view!(editor, view_id).doc); + let doc = doc_mut!(editor, &view!(editor, view_id).doc_id); doc.set_selection(view_id, Selection::point(pos)); cxt.editor.focus(view_id); commands::MappableCommand::paste_primary_clipboard_before.execute(cxt); @@ -1302,7 +1302,7 @@ impl Component for EditorView { let config = cx.editor.config(); let mode = cx.editor.mode(); let view = view_mut!(cx.editor, focus); - let doc = doc_mut!(cx.editor, &view.doc); + let doc = doc_mut!(cx.editor, &view.doc_id); view.ensure_cursor_in_view(doc, config.scrolloff); @@ -1357,7 +1357,7 @@ impl Component for EditorView { } for (view, is_focused) in cx.editor.tree.views() { - let doc = cx.editor.document(view.doc).unwrap(); + let doc = cx.editor.document(view.doc_id).unwrap(); self.render_view(cx.editor, doc, view, area, surface, is_focused); } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index d7717f8cf59c..712ac3f29821 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -67,7 +67,7 @@ pub fn regex_prompt( fun: impl Fn(&mut Editor, Regex, PromptEvent) + 'static, ) { let (view, doc) = current!(cx.editor); - let doc_id = view.doc; + let doc_id = view.doc_id; let snapshot = doc.selection(view.id).clone(); let offset_snapshot = view.offset; let config = cx.editor.config(); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 50da3ddeac2d..436c91605c6d 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1147,7 +1147,7 @@ impl Editor { fn _refresh(&mut self) { let config = self.config(); for (view, _) in self.tree.views_mut() { - let doc = doc_mut!(self, &view.doc); + let doc = doc_mut!(self, &view.doc_id); view.sync_changes(doc); view.gutters = config.gutters.clone(); view.ensure_cursor_in_view(doc, config.scrolloff) @@ -1156,7 +1156,7 @@ impl Editor { fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) { let view = self.tree.get_mut(current_view); - view.doc = doc_id; + view.doc_id = doc_id; view.offset = ViewPosition::default(); let doc = doc_mut!(self, &doc_id); @@ -1191,7 +1191,7 @@ impl Editor { && !self .tree .traverse() - .any(|(_, v)| v.doc == doc.id && v.id != view.id); + .any(|(_, v)| v.doc_id == doc.id && v.id != view.id); let (view, doc) = current!(self); let view_id = view.id; @@ -1210,16 +1210,17 @@ impl Editor { view.remove_document(&id); } } else { - let jump = (view.doc, doc.selection(view.id).clone()); + let jump = (view.doc_id, doc.selection(view.id).clone()); view.jumps.push(jump); // Set last accessed doc if it is a different document if doc.id != id { - view.add_to_history(view.doc); + view.add_to_history(view.doc_id); // Set last modified doc if modified and last modified doc is different if std::mem::take(&mut doc.modified_since_accessed) - && view.last_modified_docs[0] != Some(view.doc) + && view.last_modified_docs[0] != Some(view.doc_id) { - view.last_modified_docs = [Some(view.doc), view.last_modified_docs[0]]; + view.last_modified_docs = + [Some(view.doc_id), view.last_modified_docs[0]]; } } } @@ -1239,7 +1240,7 @@ impl Editor { let view = self .tree .try_get(self.tree.focus) - .filter(|v| id == v.doc) // Different Document + .filter(|v| id == v.doc_id) // Different Document .cloned() .unwrap_or_else(|| View::new(id, self.config().gutters.clone())); let view_id = self.tree.split( @@ -1361,7 +1362,7 @@ impl Editor { .filter_map(|(view, _focus)| { view.remove_document(&doc_id); - if view.doc == doc_id { + if view.doc_id == doc_id { // something was previously open in the view, switch to previous doc if let Some(prev_doc) = view.docs_access_history.pop() { Some(Action::ReplaceDoc(view.id, prev_doc)) @@ -1452,7 +1453,7 @@ impl Editor { // Update jumplist selections with new document changes. for (view, _focused) in self.tree.views_mut() { - let doc = doc_mut!(self, &view.doc); + let doc = doc_mut!(self, &view.doc_id); view.sync_changes(doc); } } @@ -1488,7 +1489,7 @@ impl Editor { pub fn ensure_cursor_in_view(&mut self, id: ViewId) { let config = self.config(); let view = self.tree.get_mut(id); - let doc = &self.documents[&view.doc]; + let doc = &self.documents[&view.doc_id]; view.ensure_cursor_in_view(doc, config.scrolloff) } diff --git a/helix-view/src/macros.rs b/helix-view/src/macros.rs index ee9cd4111795..c606d93deba4 100644 --- a/helix-view/src/macros.rs +++ b/helix-view/src/macros.rs @@ -13,8 +13,7 @@ macro_rules! current { ($editor:expr) => {{ let view = $crate::view_mut!($editor); - let id = view.doc; - let doc = $crate::doc_mut!($editor, &id); + let doc = $crate::doc_mut!($editor, &view.doc_id); (view, doc) }}; } @@ -23,7 +22,7 @@ macro_rules! current { macro_rules! current_ref { ($editor:expr) => {{ let view = $editor.tree.get($editor.tree.focus); - let doc = &$editor.documents[&view.doc]; + let doc = &$editor.documents[&view.doc_id]; (view, doc) }}; } @@ -32,14 +31,24 @@ macro_rules! current_ref { /// Returns `&mut Document` #[macro_export] macro_rules! doc_mut { - ($editor:expr, $id:expr) => {{ - $editor.documents.get_mut($id).unwrap() + ($editor:expr, $doc_id:expr) => {{ + $editor.documents.get_mut($doc_id).unwrap() }}; ($editor:expr) => {{ $crate::current!($editor).1 }}; } +#[macro_export] +macro_rules! doc { + ($editor:expr, $doc_id:expr) => {{ + &$editor.documents[$doc_id] + }}; + ($editor:expr) => {{ + $crate::current_ref!($editor).1 + }}; +} + /// Get the current view mutably. /// Returns `&mut View` #[macro_export] @@ -63,13 +72,3 @@ macro_rules! view { $editor.tree.get($editor.tree.focus) }}; } - -#[macro_export] -macro_rules! doc { - ($editor:expr, $id:expr) => {{ - &$editor.documents[$id] - }}; - ($editor:expr) => {{ - $crate::current_ref!($editor).1 - }}; -} diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index 5ec2773d9465..6265295db968 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -804,7 +804,7 @@ mod test { fn doc_id(tree: &Tree, view_id: ViewId) -> Option { if let Content::View(view) = &tree.nodes[view_id].content { - Some(view.doc) + Some(view.doc_id) } else { None } diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index f793cbe364f5..4b37f5595097 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -107,7 +107,7 @@ pub struct View { pub id: ViewId, pub offset: ViewPosition, pub area: Rect, - pub doc: DocumentId, + pub doc_id: DocumentId, pub jumps: JumpList, // documents accessed from this view from the oldest one to last viewed one pub docs_access_history: Vec, @@ -132,7 +132,7 @@ impl fmt::Debug for View { f.debug_struct("View") .field("id", &self.id) .field("area", &self.area) - .field("doc", &self.doc) + .field("doc", &self.doc_id) .finish() } } @@ -141,7 +141,7 @@ impl View { pub fn new(doc: DocumentId, gutters: GutterConfig) -> Self { Self { id: ViewId::default(), - doc, + doc_id: doc, offset: ViewPosition { anchor: 0, horizontal_offset: 0,