From 2588fa3710921683c16a84ffd91103a0823a033b Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Thu, 2 Mar 2023 19:10:47 +0100 Subject: [PATCH] save selection before completion savepoint Currently, the selection is not saved/restored when completion checkpoints are applied. This is usually fine because undoing changes usually restores maps selections back in insert mode. But this is not always the case and especially problematic in the presence of multi-cursor completions (since completions are applied relative to the selection/cursor) and snippets (which can change the selection) --- helix-term/src/commands.rs | 2 +- helix-term/src/ui/completion.rs | 2 +- helix-term/src/ui/editor.rs | 4 ++-- helix-view/src/document.rs | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 55ca875dc..bc0e8ebea 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4181,7 +4181,7 @@ pub fn completion(cx: &mut Context) { iter.reverse(); let offset = iter.take_while(|ch| chars::char_is_word(*ch)).count(); let start_offset = cursor.saturating_sub(offset); - doc.savepoint(); + doc.savepoint(&view); cx.callback( future, diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 85931fe32..179a8cf8c 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -235,7 +235,7 @@ impl Completion { ); // initialize a savepoint - doc.savepoint(); + doc.savepoint(&view); doc.apply(&transaction, view.id); editor.last_completion = Some(CompleteAction { diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 2ea1b7147..62f04cc9d 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -914,8 +914,8 @@ impl EditorView { doc.apply(&tx, view.id); } InsertEvent::TriggerCompletion => { - let (_, doc) = current!(cxt.editor); - doc.savepoint(); + let (view, doc) = current!(cxt.editor); + doc.savepoint(view); } } } diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 4d3586f1a..13ffe7948 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -941,7 +941,8 @@ impl Document { } pub fn savepoint(&mut self) { - self.savepoint = Some(Transaction::new(self.text())); + self.savepoint = + Some(Transaction::new(self.text()).with_selection(self.selection(view.id).clone())); } pub fn restore(&mut self, view: &mut View) {