@@ -15,11 +15,11 @@ use helix_core::{
1515 syntax::{self, HighlightEvent},
1616 unicode::segmentation::UnicodeSegmentation,
1717 unicode::width::UnicodeWidthStr,
18− LineEnding, Position, Range, Selection,
18+ LineEnding, Position, Range, Selection, Transaction,
1919};
2020use helix_view::{
2121 document::{Mode, SCRATCH_BUFFER_NAME},
22− editor::CursorShapeConfig,
22+ editor::{CompleteAction, CursorShapeConfig},
2323 graphics::{CursorKind, Modifier, Rect, Style},
2424 input::KeyEvent,
2525 keyboard::{KeyCode, KeyModifiers},
@@ -33,11 +33,18 @@ use tui::buffer::Buffer as Surface;
3333pub struct EditorView {
3434 pub keymaps: Keymaps,
3535 on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
36− last_insert: (commands::MappableCommand, Vec<KeyEvent>),
36+ last_insert: (commands::MappableCommand, Vec<InsertEvent>),
3737 pub(crate) completion: Option<Completion>,
3838 spinners: ProgressSpinners,
3939}
4040
41+#[derive(Debug, Clone)]
42+pub enum InsertEvent {
43+ Key(KeyEvent),
44+ CompletionApply(CompleteAction),
45+ TriggerCompletion,
46+}
47+
4148impl Default for EditorView {
4249 fn default() -> Self {
4350 Self::new(Keymaps::default())
@@ -766,8 +773,33 @@ impl EditorView {
766773 // first execute whatever put us into insert mode
767774 self.last_insert.0.execute(cxt);
768775 // then replay the inputs
769− for &key in &self.last_insert.1.clone() {
770− self.insert_mode(cxt, key)
776+ for key in self.last_insert.1.clone() {
777+ match key {
778+ InsertEvent::Key(key) => self.insert_mode(cxt, key),
779+ InsertEvent::CompletionApply(compl) => {
780+ let (view, doc) = current!(cxt.editor);
781+
782+ doc.restore(view.id);
783+
784+ let text = doc.text().slice(..);
785+ let cursor = doc.selection(view.id).primary().cursor(text);
786+
787+ let shift_position =
788+ |pos: usize| -> usize { pos + cursor - compl.trigger_offset };
789+
790+ let tx = Transaction::change(
791+ doc.text(),
792+ compl.changes.iter().cloned().map(|(start, end, t)| {
793+ (shift_position(start), shift_position(end), t)
794+ }),
795+ );
796+ doc.apply(&tx, view.id);
797+ }
798+ InsertEvent::TriggerCompletion => {
799+ let (_, doc) = current!(cxt.editor);
800+ doc.savepoint();
801+ }
802+ }
771803 }
772804 }
773805 _ => {
@@ -808,6 +840,9 @@ impl EditorView {
808840 // Immediately initialize a savepoint
809841 doc_mut!(editor).savepoint();
810842
843+ editor.last_completion = None;
844+ self.last_insert.1.push(InsertEvent::TriggerCompletion);
845+
811846 // TODO : propagate required size on resize to completion too
812847 completion.required_size((size.width, size.height));
813848 self.completion = Some(completion);
@@ -1067,9 +1102,6 @@ impl Component for EditorView {
10671102 } else {
10681103 match mode {
10691104 Mode::Insert => {
1070− // record last_insert key
1071− self.last_insert.1.push(key);
1072−
10731105 // let completion swallow the event if necessary
10741106 let mut consumed = false;
10751107 if let Some(completion) = &mut self.completion {
@@ -1093,8 +1125,15 @@ impl Component for EditorView {
10931125
10941126 // if completion didn't take the event, we pass it onto commands
10951127 if !consumed {
1128+ if let Some(compl) = cx.editor.last_completion.take() {
1129+ self.last_insert.1.push(InsertEvent::CompletionApply(compl));
1130+ }
1131+
10961132 self.insert_mode(&mut cx, key);
10971133
1134+ // record last_insert key
1135+ self.last_insert.1.push(InsertEvent::Key(key));
1136+
10981137 // lastly we recalculate completion
10991138 if let Some(completion) = &mut self.completion {
11001139 completion.update(&mut cx);