From 8664d70e731c73fa34dda293b4f6b6dec80a3273 Mon Sep 17 00:00:00 2001 From: BenoƮt CORTIER Date: Thu, 17 Jun 2021 18:09:10 -0400 Subject: Replace `Editor::current` by a macro This is necessary to workaround ownership issues across function calls. The issue notably arised when implementing the registers into `Editor` and I was getting annoyed again when implementing copy/pasting into system clipboard. The problem is addressed by using macro calls instead of function calls. There is no notable side effect. --- helix-view/src/editor.rs | 29 ++++------------------------- helix-view/src/lib.rs | 3 +++ helix-view/src/macros.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 helix-view/src/macros.rs (limited to 'helix-view/src') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 24f43c0e..a1c93f75 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -101,19 +101,19 @@ impl Editor { match action { Action::Replace => { - let view = self.view(); + let view = view!(self); let jump = ( view.doc, self.documents[view.doc].selection(view.id).clone(), ); - let view = self.view_mut(); + let view = view_mut!(self); view.jumps.push(jump); view.last_accessed_doc = Some(view.doc); view.doc = id; view.first_line = 0; - let (view, doc) = self.current(); + let (view, doc) = current!(self); // initialize selection for view let selection = doc @@ -238,27 +238,6 @@ impl Editor { self.tree.is_empty() } - pub fn current(&mut self) -> (&mut View, &mut Document) { - let view = self.tree.get_mut(self.tree.focus); - let doc = &mut self.documents[view.doc]; - (view, doc) - } - - pub fn current_with_registers(&mut self) -> (&mut View, &mut Document, &mut Registers) { - let view = self.tree.get_mut(self.tree.focus); - let doc = &mut self.documents[view.doc]; - let registers = &mut self.registers; - (view, doc, registers) - } - - pub fn view(&self) -> &View { - self.tree.get(self.tree.focus) - } - - pub fn view_mut(&mut self) -> &mut View { - self.tree.get_mut(self.tree.focus) - } - pub fn ensure_cursor_in_view(&mut self, id: ViewId) { let view = self.tree.get_mut(id); let doc = &self.documents[view.doc]; @@ -280,7 +259,7 @@ impl Editor { pub fn cursor(&self) -> (Option, CursorKind) { const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter - let view = self.view(); + let view = view!(self); let doc = &self.documents[view.doc]; let cursor = doc.selection(view.id).cursor(); if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) { diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index 7e253320..20613451 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +pub mod macros; + pub mod document; pub mod editor; pub mod register_selection; diff --git a/helix-view/src/macros.rs b/helix-view/src/macros.rs new file mode 100644 index 00000000..a06d37e7 --- /dev/null +++ b/helix-view/src/macros.rs @@ -0,0 +1,29 @@ +#[macro_export] +macro_rules! current { + ( $( $editor:ident ).+ ) => {{ + let view = $crate::view_mut!( $( $editor ).+ ); + let doc = &mut $( $editor ).+ .documents[view.doc]; + (view, doc) + }}; +} + +#[macro_export] +macro_rules! doc_mut { + ( $( $editor:ident ).+ ) => {{ + $crate::current!( $( $editor ).+ ).1 + }}; +} + +#[macro_export] +macro_rules! view_mut { + ( $( $editor:ident ).+ ) => {{ + $( $editor ).+ .tree.get_mut($( $editor ).+ .tree.focus) + }}; +} + +#[macro_export] +macro_rules! view { + ( $( $editor:ident ).+ ) => {{ + $( $editor ).+ .tree.get($( $editor ).+ .tree.focus) + }}; +} -- cgit v1.2.3-70-g09d2