on master help sign in
completion: fully revert state before apply & insertText common prefix
commit3edca7854e66cbdb0c4baca25962a4f390fede55 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 25 Oct 2021, 11:03 (5 years ago) parentbfb6cff
4 files changed+71−44
helix-core/src/transaction.rs+7 helix-term/src/ui/completion.rs+30−41 helix-term/src/ui/editor.rs+11−2 helix-view/src/document.rs+23−1
helix-core/src/transaction.rs+7
@@ -468,6 +468,13 @@ impl Transaction {
468468 }
469469 }
470470
471+ pub fn compose(mut self, other: Self) -> Self {
472+ self.changes = self.changes.compose(other.changes);
473+ // Other selection takes precedence
474+ self.selection = other.selection;
475+ self
476+ }
477+
471478 pub fn with_selection(mut self, selection: Selection) -> Self {
472479 self.selection = Some(selection);
473480 self
helix-term/src/ui/completion.rs+30−41
@@ -5,7 +5,7 @@ use tui::buffer::Buffer as Surface;
55use std::borrow::Cow;
66
77use helix_core::Transaction;
8−use helix_view::{graphics::Rect, Document, Editor, View};
8+use helix_view::{graphics::Rect, Document, Editor};
99
1010use crate::commands;
1111use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};
@@ -83,13 +83,13 @@ impl Completion {
8383 start_offset: usize,
8484 trigger_offset: usize,
8585 ) -> Self {
86− // let items: Vec<CompletionItem> = Vec::new();
8786 let menu = Menu::new(items, move |editor: &mut Editor, item, event| {
8887 fn item_to_transaction(
8988 doc: &Document,
90− view: &View,
9189 item: &CompletionItem,
9290 offset_encoding: helix_lsp::OffsetEncoding,
91+ start_offset: usize,
92+ trigger_offset: usize,
9393 ) -> Transaction {
9494 if let Some(edit) = &item.text_edit {
9595 let edit = match edit {
@@ -105,63 +105,52 @@ impl Completion {
105105 )
106106 } else {
107107 let text = item.insert_text.as_ref().unwrap_or(&item.label);
108− let cursor = doc
109− .selection(view.id)
110− .primary()
111− .cursor(doc.text().slice(..));
108+ // Some LSPs just give you an insertText with no offset ¯\_(ツ)_/¯
109+ // in these cases we need to check for a common prefix and remove it
110+ let prefix = Cow::from(doc.text().slice(start_offset..trigger_offset));
111+ let text = text.trim_start_matches::<&str>(&prefix);
112112 Transaction::change(
113113 doc.text(),
114− vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(),
114+ vec![(trigger_offset, trigger_offset, Some(text.into()))].into_iter(),
115115 )
116116 }
117117 }
118118
119+ let (view, doc) = current!(editor);
120+
121+ // if more text was entered, remove it
122+ doc.restore(view.id);
123+
119124 match event {
120125 PromptEvent::Abort => {}
121126 PromptEvent::Update => {
122− let (view, doc) = current!(editor);
123−
124127 // always present here
125128 let item = item.unwrap();
126129
127− // if more text was entered, remove it
128− // TODO: ideally to undo we should keep the last completion tx revert, and map it over new changes
129− let cursor = doc
130− .selection(view.id)
131− .primary()
132− .cursor(doc.text().slice(..));
133− if trigger_offset < cursor {
134− let remove = Transaction::change(
135− doc.text(),
136− vec![(trigger_offset, cursor, None)].into_iter(),
137− );
138− doc.apply(&remove, view.id);
139− }
130+ let transaction = item_to_transaction(
131+ doc,
132+ item,
133+ offset_encoding,
134+ start_offset,
135+ trigger_offset,
136+ );
137+
138+ // initialize a savepoint
139+ doc.savepoint();
140140
141− let transaction = item_to_transaction(doc, view, item, offset_encoding);
142141 doc.apply(&transaction, view.id);
143142 }
144143 PromptEvent::Validate => {
145− let (view, doc) = current!(editor);
146−
147144 // always present here
148145 let item = item.unwrap();
149146
150− // if more text was entered, remove it
151− // TODO: ideally to undo we should keep the last completion tx revert, and map it over new changes
152− let cursor = doc
153− .selection(view.id)
154− .primary()
155− .cursor(doc.text().slice(..));
156− if trigger_offset < cursor {
157− let remove = Transaction::change(
158− doc.text(),
159− vec![(trigger_offset, cursor, None)].into_iter(),
160− );
161− doc.apply(&remove, view.id);
162− }
163−
164− let transaction = item_to_transaction(doc, view, item, offset_encoding);
147+ let transaction = item_to_transaction(
148+ doc,
149+ item,
150+ offset_encoding,
151+ start_offset,
152+ trigger_offset,
153+ );
165154 doc.apply(&transaction, view.id);
166155
167156 if let Some(additional_edits) = &item.additional_text_edits {
helix-term/src/ui/editor.rs+11−2
@@ -13,7 +13,7 @@ use helix_core::{
1313 syntax::{self, HighlightEvent},
1414 unicode::segmentation::UnicodeSegmentation,
1515 unicode::width::UnicodeWidthStr,
16− LineEnding, Position, Range, Selection,
16+ LineEnding, Position, Range, Selection, Transaction,
1717};
1818use helix_view::{
1919 document::Mode,
@@ -721,7 +721,7 @@ impl EditorView {
721721
722722 pub fn set_completion(
723723 &mut self,
724− editor: &Editor,
724+ editor: &mut Editor,
725725 items: Vec<helix_lsp::lsp::CompletionItem>,
726726 offset_encoding: helix_lsp::OffsetEncoding,
727727 start_offset: usize,
@@ -736,6 +736,9 @@ impl EditorView {
736736 return;
737737 }
738738
739+ // Immediately initialize a savepoint
740+ doc_mut!(editor).savepoint();
741+
739742 // TODO : propagate required size on resize to completion too
740743 completion.required_size((size.width, size.height));
741744 self.completion = Some(completion);
@@ -945,6 +948,9 @@ impl Component for EditorView {
945948 if callback.is_some() {
946949 // assume close_fn
947950 self.completion = None;
951+ // Clear any savepoints
952+ let (_, doc) = current!(cxt.editor);
953+ doc.savepoint = None;
948954 cxt.editor.clear_idle_timer(); // don't retrigger
949955 }
950956 }
@@ -959,6 +965,9 @@ impl Component for EditorView {
959965 completion.update(&mut cxt);
960966 if completion.is_empty() {
961967 self.completion = None;
968+ // Clear any savepoints
969+ let (_, doc) = current!(cxt.editor);
970+ doc.savepoint = None;
962971 cxt.editor.clear_idle_timer(); // don't retrigger
963972 }
964973 }
helix-view/src/document.rs+23−1
@@ -97,6 +97,9 @@ pub struct Document {
9797 // it back as it separated from the edits. We could split out the parts manually but that will
9898 // be more troublesome.
9999 history: Cell<History>,
100+
101+ pub savepoint: Option<Transaction>,
102+
100103 last_saved_revision: usize,
101104 version: i32, // should be usize?
102105
@@ -328,6 +331,7 @@ impl Document {
328331 text,
329332 selections: HashMap::default(),
330333 indent_style: DEFAULT_INDENT,
334+ line_ending: DEFAULT_LINE_ENDING,
331335 mode: Mode::Normal,
332336 restore_cursor: false,
333337 syntax: None,
@@ -337,9 +341,9 @@ impl Document {
337341 diagnostics: Vec::new(),
338342 version: 0,
339343 history: Cell::new(History::default()),
344+ savepoint: None,
340345 last_saved_revision: 0,
341346 language_server: None,
342− line_ending: DEFAULT_LINE_ENDING,
343347 }
344348 }
345349
@@ -635,6 +639,14 @@ impl Document {
635639 if !transaction.changes().is_empty() {
636640 self.version += 1;
637641
642+ // generate revert to savepoint
643+ if self.savepoint.is_some() {
644+ take_with(&mut self.savepoint, |prev_revert| {
645+ let revert = transaction.invert(&old_doc);
646+ Some(revert.compose(prev_revert.unwrap()))
647+ });
648+ }
649+
638650 // update tree-sitter syntax tree
639651 if let Some(syntax) = &mut self.syntax {
640652 // TODO: no unwrap
@@ -724,6 +736,16 @@ impl Document {
724736 }
725737 }
726738
739+ pub fn savepoint(&mut self) {
740+ self.savepoint = Some(Transaction::new(self.text()));
741+ }
742+
743+ pub fn restore(&mut self, view_id: ViewId) {
744+ if let Some(revert) = self.savepoint.take() {
745+ self.apply(&revert, view_id);
746+ }
747+ }
748+
727749 /// Undo modifications to the [`Document`] according to `uk`.
728750 pub fn earlier(&mut self, view_id: ViewId, uk: helix_core::history::UndoKind) {
729751 let txns = self.history.get_mut().earlier(uk);