diff options
author | Blaž Hrastnik | 2021-04-01 01:39:46 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-04-01 02:01:26 +0000 |
commit | 0dbd5b61ef4b75ba141b0b52636f3ba7c61c70cb (patch) | |
tree | d831ce257c3333d338ac41c235afac295bd90bf0 /helix-term/src/ui | |
parent | ceea5eacd814c5bbd8c6789610c87a882e3d72cf (diff) |
Simplify code by providin cx.current() = (view, doc).
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/completion.rs | 18 | ||||
-rw-r--r-- | helix-term/src/ui/editor.rs | 13 | ||||
-rw-r--r-- | helix-term/src/ui/mod.rs | 17 |
3 files changed, 17 insertions, 31 deletions
diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 9c753007..53241c57 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -44,10 +44,7 @@ impl Completion { // doc.state = snapshot.clone(); } PromptEvent::Validate => { - let view = editor.view(); - let view_id = view.id; - let id = view.doc; - let doc = &mut editor.documents[id]; + let (view, doc) = editor.current(); // revert state to what it was before the last update // doc.state = snapshot.clone(); @@ -92,18 +89,18 @@ impl Completion { } // if more text was entered, remove it - let cursor = doc.selection(view_id).cursor(); + let cursor = doc.selection(view.id).cursor(); if trigger_offset < cursor { let remove = Transaction::change( doc.text(), vec![(trigger_offset, cursor, None)].into_iter(), ); - doc.apply(&remove, view_id); + doc.apply(&remove, view.id); } let transaction = util::generate_transaction_from_edits(doc.text(), vec![edit]); - doc.apply(&transaction, view_id); + doc.apply(&transaction, view.id); } _ => (), }; @@ -127,10 +124,7 @@ impl Component for Completion { { // recompute menu based on matches let menu = self.popup.contents(); - let view = cx.editor.view(); - let view_id = view.id; - let id = view.doc; - let doc = cx.editor.document(id).unwrap(); + let (view, doc) = cx.editor.current(); // cx.hooks() // cx.add_hook(enum type, ||) @@ -142,7 +136,7 @@ impl Component for Completion { // TODO: hooks should get processed immediately so maybe do it after select!(), before // looping? - let cursor = doc.selection(view_id).cursor(); + let cursor = doc.selection(view.id).cursor(); if self.trigger_offset <= cursor { let fragment = doc.text().slice(self.trigger_offset..cursor); // ^ problem seems to be that we handle events here before the editor layer, so the diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index d0822500..24c46bde 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -487,14 +487,12 @@ impl Component for EditorView { EventResult::Consumed(None) } Event::Key(event) => { - let view = cx.editor.view(); - let view_id = view.id; - let id = view.doc; - let mode = cx.editor.document(id).unwrap().mode(); + let (view, doc) = cx.editor.current(); + let mode = doc.mode(); let mut cxt = commands::Context { + view_id: view.id, editor: &mut cx.editor, - view_id, count: 1, callback: None, callbacks: cx.callbacks, @@ -524,9 +522,10 @@ impl Component for EditorView { // appease borrowck let callback = cxt.callback.take(); - cx.editor.ensure_cursor_in_view(cx.editor.tree.focus); + let (view, doc) = cx.editor.current(); + view.ensure_cursor_in_view(doc); - if mode == Mode::Normal && cx.editor.document(id).unwrap().mode() == Mode::Insert { + if mode == Mode::Normal && doc.mode() == Mode::Insert { // HAXX: if we just entered insert mode from normal, clear key buf // and record the command that got us into this mode. diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 479e684a..8cbf706e 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -45,11 +45,8 @@ pub fn regex_prompt( match event { PromptEvent::Abort => { // TODO: also revert text - let view = editor.view(); - let view_id = view.id; - let id = view.doc; - let doc = &mut editor.documents[id]; - doc.set_selection(view_id, snapshot.clone()); + let (view, doc) = editor.current(); + doc.set_selection(view.id, snapshot.clone()); } PromptEvent::Validate => { // TODO: push_jump to store selection just before jump @@ -62,19 +59,15 @@ pub fn regex_prompt( match Regex::new(input) { Ok(regex) => { - // let view = &mut editor.view_mut(); - let view = editor.view(); - let view_id = view.id; - let id = view.doc; - let doc = &mut editor.documents[id]; + let (view, doc) = editor.current(); // revert state to what it was before the last update // TODO: also revert text - doc.set_selection(view_id, snapshot.clone()); + doc.set_selection(view.id, snapshot.clone()); fun(doc, regex); - editor.ensure_cursor_in_view(view_id); + view.ensure_cursor_in_view(doc); } Err(_err) => (), // TODO: mark command line as error } |