@@ -322,17 +322,23 @@ pub fn select_line(cx: &mut Context) {
322 cx.view.doc.set_selection(Selection::single(start, end));
322 cx.view.doc.set_selection(Selection::single(start, end));
325+// heuristic: append changes to history after each command, unless we're in insert mode
325−pub fn delete_selection(cx: &mut Context) {
327+fn _delete_selection(cx: &mut Context) {
326 let transaction = Transaction::change_by_selection(&cx.view.doc.state, |range| {
328 let transaction = Transaction::change_by_selection(&cx.view.doc.state, |range| {
327 (range.from(), range.to() + 1, None)
329 (range.from(), range.to() + 1, None)
329 cx.view.doc.apply(&transaction);
331 cx.view.doc.apply(&transaction);
334+pub fn delete_selection(cx: &mut Context) {
335+ _delete_selection(cx);
331 append_changes_to_history(cx);
337 append_changes_to_history(cx);
334pub fn change_selection(cx: &mut Context) {
340pub fn change_selection(cx: &mut Context) {
335− delete_selection(cx);
341+ _delete_selection(cx);
336 insert_mode(cx);
342 insert_mode(cx);
@@ -359,8 +365,9 @@ pub fn flip_selections(cx: &mut Context) {
359fn enter_insert_mode(cx: &mut Context) {
365fn enter_insert_mode(cx: &mut Context) {
360 cx.view.doc.mode = Mode::Insert;
366 cx.view.doc.mode = Mode::Insert;
362− append_changes_to_history(cx);
368+ // TODO: store selection for undo
364// inserts at the start of each selection
371// inserts at the start of each selection
365pub fn insert_mode(cx: &mut Context) {
372pub fn insert_mode(cx: &mut Context) {
366 enter_insert_mode(cx);
373 enter_insert_mode(cx);
@@ -535,6 +542,8 @@ fn append_changes_to_history(cx: &mut Context) {
545+ // TODO: change -> change -> undo -> change -> change fails, probably old_state needs reset
538 let new_changeset = ChangeSet::new(cx.view.doc.text());
547 let new_changeset = ChangeSet::new(cx.view.doc.text());
539 let changes = std::mem::replace(&mut cx.view.doc.changes, new_changeset);
548 let changes = std::mem::replace(&mut cx.view.doc.changes, new_changeset);
540 // Instead of doing this messy merge we could always commit, and based on transaction
549 // Instead of doing this messy merge we could always commit, and based on transaction
@@ -548,7 +557,13 @@ fn append_changes_to_history(cx: &mut Context) {
548 // TODO: trigger lsp/documentDidChange with changes
557 // TODO: trigger lsp/documentDidChange with changes
550 // HAXX: we need to reconstruct the state as it was before the changes..
559 // HAXX: we need to reconstruct the state as it was before the changes..
551− let old_state = std::mem::replace(&mut cx.view.doc.old_state, cx.view.doc.state.clone());
560+ let old_state = cx
565+ .expect("no old_state available");
552 // TODO: take transaction by value?
567 // TODO: take transaction by value?
@@ -649,6 +664,7 @@ pub fn undo(cx: &mut Context) {
649 if let Some(revert) = cx.view.doc.history.undo() {
664 if let Some(revert) = cx.view.doc.history.undo() {
650 cx.view.doc.version += 1;
665 cx.view.doc.version += 1;
651 cx.view.doc.apply(&revert);
666 cx.view.doc.apply(&revert);
667+ // TODO: undo/redo needs to avoid storing in self.changes/self.old_state
654 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
670 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?