@@ -15,11 +15,11 @@ use helix_core::{
15 syntax::{self, HighlightEvent},
15 syntax::{self, HighlightEvent},
16 unicode::segmentation::UnicodeSegmentation,
16 unicode::segmentation::UnicodeSegmentation,
17 unicode::width::UnicodeWidthStr,
17 unicode::width::UnicodeWidthStr,
18− LineEnding, Position, Range, Selection,
18+ LineEnding, Position, Range, Selection, Transaction,
20use helix_view::{
20use helix_view::{
21 document::{Mode, SCRATCH_BUFFER_NAME},
21 document::{Mode, SCRATCH_BUFFER_NAME},
22− editor::CursorShapeConfig,
22+ editor::{CompleteAction, CursorShapeConfig},
23 graphics::{CursorKind, Modifier, Rect, Style},
23 graphics::{CursorKind, Modifier, Rect, Style},
24 input::KeyEvent,
24 input::KeyEvent,
25 keyboard::{KeyCode, KeyModifiers},
25 keyboard::{KeyCode, KeyModifiers},
@@ -33,11 +33,18 @@ use tui::buffer::Buffer as Surface;
33pub struct EditorView {
33pub struct EditorView {
34 pub keymaps: Keymaps,
34 pub keymaps: Keymaps,
35 on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
35 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>),
37 pub(crate) completion: Option<Completion>,
37 pub(crate) completion: Option<Completion>,
38 spinners: ProgressSpinners,
38 spinners: ProgressSpinners,
41+#[derive(Debug, Clone)]
42+pub enum InsertEvent {
44+ CompletionApply(CompleteAction),
41impl Default for EditorView {
48impl Default for EditorView {
42 fn default() -> Self {
49 fn default() -> Self {
43 Self::new(Keymaps::default())
50 Self::new(Keymaps::default())
@@ -766,8 +773,33 @@ impl EditorView {
766 // first execute whatever put us into insert mode
773 // first execute whatever put us into insert mode
767 self.last_insert.0.execute(cxt);
774 self.last_insert.0.execute(cxt);
768 // then replay the inputs
775 // then replay the inputs
769− for &key in &self.last_insert.1.clone() {
776+ for key in self.last_insert.1.clone() {
770− self.insert_mode(cxt, key)
778+ InsertEvent::Key(key) => self.insert_mode(cxt, key),
779+ InsertEvent::CompletionApply(compl) => {
780+ let (view, doc) = current!(cxt.editor);
782+ doc.restore(view.id);
784+ let text = doc.text().slice(..);
785+ let cursor = doc.selection(view.id).primary().cursor(text);
787+ let shift_position =
788+ |pos: usize| -> usize { pos + cursor - compl.trigger_offset };
790+ let tx = Transaction::change(
792+ compl.changes.iter().cloned().map(|(start, end, t)| {
793+ (shift_position(start), shift_position(end), t)
796+ doc.apply(&tx, view.id);
798+ InsertEvent::TriggerCompletion => {
799+ let (_, doc) = current!(cxt.editor);
@@ -808,6 +840,9 @@ impl EditorView {
808 // Immediately initialize a savepoint
840 // Immediately initialize a savepoint
809 doc_mut!(editor).savepoint();
841 doc_mut!(editor).savepoint();
843+ editor.last_completion = None;
844+ self.last_insert.1.push(InsertEvent::TriggerCompletion);
811 // TODO : propagate required size on resize to completion too
846 // TODO : propagate required size on resize to completion too
812 completion.required_size((size.width, size.height));
847 completion.required_size((size.width, size.height));
813 self.completion = Some(completion);
848 self.completion = Some(completion);
@@ -1067,9 +1102,6 @@ impl Component for EditorView {
1067 } else {
1102 } else {
1068 match mode {
1103 match mode {
1069 Mode::Insert => {
1104 Mode::Insert => {
1070− // record last_insert key
1071− self.last_insert.1.push(key);
1073 // let completion swallow the event if necessary
1105 // let completion swallow the event if necessary
1074 let mut consumed = false;
1106 let mut consumed = false;
1075 if let Some(completion) = &mut self.completion {
1107 if let Some(completion) = &mut self.completion {
@@ -1093,8 +1125,15 @@ impl Component for EditorView {
1094 // if completion didn't take the event, we pass it onto commands
1126 // if completion didn't take the event, we pass it onto commands
1095 if !consumed {
1127 if !consumed {
1128+ if let Some(compl) = cx.editor.last_completion.take() {
1129+ self.last_insert.1.push(InsertEvent::CompletionApply(compl));
1096 self.insert_mode(&mut cx, key);
1132 self.insert_mode(&mut cx, key);
1134+ // record last_insert key
1135+ self.last_insert.1.push(InsertEvent::Key(key));
1098 // lastly we recalculate completion
1137 // lastly we recalculate completion
1099 if let Some(completion) = &mut self.completion {
1138 if let Some(completion) = &mut self.completion {
1100 completion.update(&mut cx);
1139 completion.update(&mut cx);