@@ -19,7 +19,7 @@ use helix_core::{
1919 syntax::{self, HighlightEvent},
2020 text_annotations::TextAnnotations,
2121 unicode::width::UnicodeWidthStr,
22− visual_offset_from_block, Position, Range, Selection, Transaction,
22+ visual_offset_from_block, Change, Position, Range, Selection, Transaction,
2323};
2424use helix_view::{
2525 document::{Mode, SavePoint, SCRATCH_BUFFER_NAME},
@@ -48,7 +48,10 @@ pub struct EditorView {
4848#[derive(Debug, Clone)]
4949pub enum InsertEvent {
5050 Key(KeyEvent),
51− CompletionApply(CompleteAction),
51+ CompletionApply {
52+ trigger_offset: usize,
53+ changes: Vec<Change>,
54+ },
5255 TriggerCompletion,
5356 RequestCompletion,
5457}
@@ -813,7 +816,7 @@ impl EditorView {
813816 }
814817 (Mode::Insert, Mode::Normal) => {
815818 // if exiting insert mode, remove completion
816− self.completion = None;
819+ self.clear_completion(cxt.editor);
817820 cxt.editor.completion_request_handle = None;
818821
819822 // TODO: Use an on_mode_change hook to remove signature help
@@ -891,22 +894,25 @@ impl EditorView {
891894 for key in self.last_insert.1.clone() {
892895 match key {
893896 InsertEvent::Key(key) => self.insert_mode(cxt, key),
894− InsertEvent::CompletionApply(compl) => {
897+ InsertEvent::CompletionApply {
898+ trigger_offset,
899+ changes,
900+ } => {
895901 let (view, doc) = current!(cxt.editor);
896902
897903 if let Some(last_savepoint) = last_savepoint.as_deref() {
898− doc.restore(view, last_savepoint);
904+ doc.restore(view, last_savepoint, true);
899905 }
900906
901907 let text = doc.text().slice(..);
902908 let cursor = doc.selection(view.id).primary().cursor(text);
903909
904910 let shift_position =
905− |pos: usize| -> usize { pos + cursor - compl.trigger_offset };
911+ |pos: usize| -> usize { pos + cursor - trigger_offset };
906912
907913 let tx = Transaction::change(
908914 doc.text(),
909− compl.changes.iter().cloned().map(|(start, end, t)| {
915+ changes.iter().cloned().map(|(start, end, t)| {
910916 (shift_position(start), shift_position(end), t)
911917 }),
912918 );
@@ -979,6 +985,21 @@ impl EditorView {
979985
980986 pub fn clear_completion(&mut self, editor: &mut Editor) {
981987 self.completion = None;
988+ if let Some(last_completion) = editor.last_completion.take() {
989+ match last_completion {
990+ CompleteAction::Applied {
991+ trigger_offset,
992+ changes,
993+ } => self.last_insert.1.push(InsertEvent::CompletionApply {
994+ trigger_offset,
995+ changes,
996+ }),
997+ CompleteAction::Selected { savepoint } => {
998+ let (view, doc) = current!(editor);
999+ doc.restore(view, &savepoint, false);
1000+ }
1001+ }
1002+ }
9821003
9831004 // Clear any savepoints
9841005 editor.clear_idle_timer(); // don't retrigger
@@ -1265,12 +1286,22 @@ impl Component for EditorView {
12651286 jobs: cx.jobs,
12661287 scroll: None,
12671288 };
1268− completion.handle_event(event, &mut cx)
1269− };
12701289
1271− if let EventResult::Consumed(callback) = res {
1272− consumed = true;
1290+ if let EventResult::Consumed(callback) =
1291+ completion.handle_event(event, &mut cx)
1292+ {
1293+ consumed = true;
1294+ Some(callback)
1295+ } else if let EventResult::Consumed(callback) =
1296+ completion.handle_event(&Event::Key(key!(Enter)), &mut cx)
1297+ {
1298+ Some(callback)
1299+ } else {
1300+ None
1301+ }
1302+ };
12731303
1304+ if let Some(callback) = res {
12741305 if callback.is_some() {
12751306 // assume close_fn
12761307 self.clear_completion(cx.editor);
@@ -1286,10 +1317,6 @@ impl Component for EditorView {
12861317
12871318 // if completion didn't take the event, we pass it onto commands
12881319 if !consumed {
1289− if let Some(compl) = cx.editor.last_completion.take() {
1290− self.last_insert.1.push(InsertEvent::CompletionApply(compl));
1291− }
1292−
12931320 self.insert_mode(&mut cx, key);
12941321
12951322 // record last_insert key