Simplify code by providin cx.current() = (view, doc).

This commit is contained in:
Blaž Hrastnik 2021-04-01 10:39:46 +09:00
parent ceea5eacd8
commit 0dbd5b61ef
6 changed files with 211 additions and 286 deletions

View file

@ -23,6 +23,8 @@
- [ ] repeat insert/command -> transaction
- [ ] repeat selection
- [] jump to alt buffer
- [ ] load toml configs, themes, tabsize/identation
- [ ] draw separator line between views

File diff suppressed because it is too large Load diff

View file

@ -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

View file

@ -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.

View file

@ -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
}

View file

@ -172,6 +172,12 @@ 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 view(&self) -> &View {
self.tree.get(self.tree.focus)
}