on master help sign in
Make repeat operator work with completion edits (#1640)

* add basic completion replay * use transaction as the last completion * completion replay only on trigger position * cache changes in CompletionAction Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>

commit14e2ced440a2ba0f1794644f5cc1295a5738ad36 authorMateusz S. Szczygieł <mateuszszczygiel95@gmail.com> dateTue 1 Mar 2022, 02:45 (5 years ago) parente83cdf3
3 files changed+85−13
helix-term/src/ui/completion.rs+28−5 helix-term/src/ui/editor.rs+47−8 helix-view/src/editor.rs+10
helix-term/src/ui/completion.rs+28−5
@@ -1,10 +1,11 @@
11use crate::compositor::{Component, Context, EventResult};
22use crossterm::event::{Event, KeyCode, KeyEvent};
3+use helix_view::editor::CompleteAction;
34use tui::buffer::Buffer as Surface;
45
56use std::borrow::Cow;
67
7−use helix_core::Transaction;
8+use helix_core::{Change, Transaction};
89use helix_view::{graphics::Rect, Document, Editor};
910
1011use crate::commands;
@@ -92,13 +93,14 @@ impl Completion {
9293 start_offset: usize,
9394 trigger_offset: usize,
9495 ) -> Transaction {
95− if let Some(edit) = &item.text_edit {
96+ let transaction = if let Some(edit) = &item.text_edit {
9697 let edit = match edit {
9798 lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
9899 lsp::CompletionTextEdit::InsertAndReplace(item) => {
99100 unimplemented!("completion: insert_and_replace {:?}", item)
100101 }
101102 };
103+
102104 util::generate_transaction_from_edits(
103105 doc.text(),
104106 vec![edit],
@@ -114,7 +116,16 @@ impl Completion {
114116 doc.text(),
115117 vec![(trigger_offset, trigger_offset, Some(text.into()))].into_iter(),
116118 )
117− }
119+ };
120+
121+ transaction
122+ }
123+
124+ fn completion_changes(transaction: &Transaction, trigger_offset: usize) -> Vec<Change> {
125+ transaction
126+ .changes_iter()
127+ .filter(|(start, end, _)| (*start..=*end).contains(&trigger_offset))
128+ .collect()
118129 }
119130
120131 let (view, doc) = current!(editor);
@@ -123,7 +134,9 @@ impl Completion {
123134 doc.restore(view.id);
124135
125136 match event {
126− PromptEvent::Abort => {}
137+ PromptEvent::Abort => {
138+ editor.last_completion = None;
139+ }
127140 PromptEvent::Update => {
128141 // always present here
129142 let item = item.unwrap();
@@ -138,8 +151,12 @@ impl Completion {
138151
139152 // initialize a savepoint
140153 doc.savepoint();
141−
142154 doc.apply(&transaction, view.id);
155+
156+ editor.last_completion = Some(CompleteAction {
157+ trigger_offset,
158+ changes: completion_changes(&transaction, trigger_offset),
159+ });
143160 }
144161 PromptEvent::Validate => {
145162 // always present here
@@ -152,8 +169,14 @@ impl Completion {
152169 start_offset,
153170 trigger_offset,
154171 );
172+
155173 doc.apply(&transaction, view.id);
156174
175+ editor.last_completion = Some(CompleteAction {
176+ trigger_offset,
177+ changes: completion_changes(&transaction, trigger_offset),
178+ });
179+
157180 // apply additional edits, mostly used to auto import unqualified types
158181 let resolved_additional_text_edits = if item.additional_text_edits.is_some() {
159182 None
helix-term/src/ui/editor.rs+47−8
@@ -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);
helix-view/src/editor.rs+10
@@ -33,6 +33,7 @@ pub use helix_core::register::Registers;
3333use helix_core::{
3434 auto_pairs::AutoPairs,
3535 syntax::{self, AutoPairConfig},
36+ Change,
3637};
3738use helix_core::{Position, Selection};
3839use helix_dap as dap;
@@ -301,9 +302,17 @@ pub struct Editor {
301302 pub last_motion: Option<Motion>,
302303 pub pseudo_pending: Option<String>,
303304
305+ pub last_completion: Option<CompleteAction>,
306+
304307 pub exit_code: i32,
305308}
306309
310+#[derive(Debug, Clone)]
311+pub struct CompleteAction {
312+ pub trigger_offset: usize,
313+ pub changes: Vec<Change>,
314+}
315+
307316#[derive(Debug, Copy, Clone)]
308317pub enum Action {
309318 Load,
@@ -347,6 +356,7 @@ impl Editor {
347356 autoinfo: None,
348357 idle_timer: Box::pin(sleep(config.idle_timeout)),
349358 last_motion: None,
359+ last_completion: None,
350360 pseudo_pending: None,
351361 config,
352362 auto_pairs,