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

imgbot
Blaž Hrastnik 3 years ago
parent ceea5eacd8
commit 0dbd5b61ef

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

File diff suppressed because it is too large Load Diff

@ -44,10 +44,7 @@ impl Completion {
// doc.state = snapshot.clone(); // doc.state = snapshot.clone();
} }
PromptEvent::Validate => { PromptEvent::Validate => {
let view = editor.view(); let (view, doc) = editor.current();
let view_id = view.id;
let id = view.doc;
let doc = &mut editor.documents[id];
// revert state to what it was before the last update // revert state to what it was before the last update
// doc.state = snapshot.clone(); // doc.state = snapshot.clone();
@ -92,18 +89,18 @@ impl Completion {
} }
// if more text was entered, remove it // 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 { if trigger_offset < cursor {
let remove = Transaction::change( let remove = Transaction::change(
doc.text(), doc.text(),
vec![(trigger_offset, cursor, None)].into_iter(), vec![(trigger_offset, cursor, None)].into_iter(),
); );
doc.apply(&remove, view_id); doc.apply(&remove, view.id);
} }
let transaction = let transaction =
util::generate_transaction_from_edits(doc.text(), vec![edit]); 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 // recompute menu based on matches
let menu = self.popup.contents(); let menu = self.popup.contents();
let view = cx.editor.view(); let (view, doc) = cx.editor.current();
let view_id = view.id;
let id = view.doc;
let doc = cx.editor.document(id).unwrap();
// cx.hooks() // cx.hooks()
// cx.add_hook(enum type, ||) // 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 // TODO: hooks should get processed immediately so maybe do it after select!(), before
// looping? // looping?
let cursor = doc.selection(view_id).cursor(); let cursor = doc.selection(view.id).cursor();
if self.trigger_offset <= cursor { if self.trigger_offset <= cursor {
let fragment = doc.text().slice(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 // ^ problem seems to be that we handle events here before the editor layer, so the

@ -487,14 +487,12 @@ impl Component for EditorView {
EventResult::Consumed(None) EventResult::Consumed(None)
} }
Event::Key(event) => { Event::Key(event) => {
let view = cx.editor.view(); let (view, doc) = cx.editor.current();
let view_id = view.id; let mode = doc.mode();
let id = view.doc;
let mode = cx.editor.document(id).unwrap().mode();
let mut cxt = commands::Context { let mut cxt = commands::Context {
view_id: view.id,
editor: &mut cx.editor, editor: &mut cx.editor,
view_id,
count: 1, count: 1,
callback: None, callback: None,
callbacks: cx.callbacks, callbacks: cx.callbacks,
@ -524,9 +522,10 @@ impl Component for EditorView {
// appease borrowck // appease borrowck
let callback = cxt.callback.take(); 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 // HAXX: if we just entered insert mode from normal, clear key buf
// and record the command that got us into this mode. // and record the command that got us into this mode.

@ -45,11 +45,8 @@ pub fn regex_prompt(
match event { match event {
PromptEvent::Abort => { PromptEvent::Abort => {
// TODO: also revert text // TODO: also revert text
let view = editor.view(); let (view, doc) = editor.current();
let view_id = view.id; doc.set_selection(view.id, snapshot.clone());
let id = view.doc;
let doc = &mut editor.documents[id];
doc.set_selection(view_id, snapshot.clone());
} }
PromptEvent::Validate => { PromptEvent::Validate => {
// TODO: push_jump to store selection just before jump // TODO: push_jump to store selection just before jump
@ -62,19 +59,15 @@ pub fn regex_prompt(
match Regex::new(input) { match Regex::new(input) {
Ok(regex) => { Ok(regex) => {
// let view = &mut editor.view_mut(); let (view, doc) = editor.current();
let view = editor.view();
let view_id = view.id;
let id = view.doc;
let doc = &mut editor.documents[id];
// revert state to what it was before the last update // revert state to what it was before the last update
// TODO: also revert text // TODO: also revert text
doc.set_selection(view_id, snapshot.clone()); doc.set_selection(view.id, snapshot.clone());
fun(doc, regex); fun(doc, regex);
editor.ensure_cursor_in_view(view_id); view.ensure_cursor_in_view(doc);
} }
Err(_err) => (), // TODO: mark command line as error Err(_err) => (), // TODO: mark command line as error
} }

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

Loading…
Cancel
Save