diff options
author | Mateusz S. Szczygieł | 2022-03-01 01:45:29 +0000 |
---|---|---|
committer | GitHub | 2022-03-01 01:45:29 +0000 |
commit | 14e2ced440a2ba0f1794644f5cc1295a5738ad36 (patch) | |
tree | a0ef9ea2b0c1e6ff8a6677550bcfba7d78038d57 /helix-term/src/ui/completion.rs | |
parent | e83cdf3fd33a43e1ff78793995adbe23bd62ae49 (diff) |
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>
Diffstat (limited to 'helix-term/src/ui/completion.rs')
-rw-r--r-- | helix-term/src/ui/completion.rs | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index a6e92a19..abed2846 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -1,10 +1,11 @@ use crate::compositor::{Component, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent}; +use helix_view::editor::CompleteAction; use tui::buffer::Buffer as Surface; use std::borrow::Cow; -use helix_core::Transaction; +use helix_core::{Change, Transaction}; use helix_view::{graphics::Rect, Document, Editor}; use crate::commands; @@ -92,13 +93,14 @@ impl Completion { start_offset: usize, trigger_offset: usize, ) -> Transaction { - if let Some(edit) = &item.text_edit { + let transaction = if let Some(edit) = &item.text_edit { let edit = match edit { lsp::CompletionTextEdit::Edit(edit) => edit.clone(), lsp::CompletionTextEdit::InsertAndReplace(item) => { unimplemented!("completion: insert_and_replace {:?}", item) } }; + util::generate_transaction_from_edits( doc.text(), vec![edit], @@ -114,7 +116,16 @@ impl Completion { doc.text(), vec![(trigger_offset, trigger_offset, Some(text.into()))].into_iter(), ) - } + }; + + transaction + } + + fn completion_changes(transaction: &Transaction, trigger_offset: usize) -> Vec<Change> { + transaction + .changes_iter() + .filter(|(start, end, _)| (*start..=*end).contains(&trigger_offset)) + .collect() } let (view, doc) = current!(editor); @@ -123,7 +134,9 @@ impl Completion { doc.restore(view.id); match event { - PromptEvent::Abort => {} + PromptEvent::Abort => { + editor.last_completion = None; + } PromptEvent::Update => { // always present here let item = item.unwrap(); @@ -138,8 +151,12 @@ impl Completion { // initialize a savepoint doc.savepoint(); - doc.apply(&transaction, view.id); + + editor.last_completion = Some(CompleteAction { + trigger_offset, + changes: completion_changes(&transaction, trigger_offset), + }); } PromptEvent::Validate => { // always present here @@ -152,8 +169,14 @@ impl Completion { start_offset, trigger_offset, ); + doc.apply(&transaction, view.id); + editor.last_completion = Some(CompleteAction { + trigger_offset, + changes: completion_changes(&transaction, trigger_offset), + }); + // apply additional edits, mostly used to auto import unqualified types let resolved_additional_text_edits = if item.additional_text_edits.is_some() { None |