aboutsummaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/src/commands.rs9
-rw-r--r--helix-term/src/ui/completion.rs46
-rw-r--r--helix-term/src/ui/editor.rs57
3 files changed, 78 insertions, 34 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 7d86bc0b..8d70cd9e 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -33,7 +33,7 @@ use helix_core::{
use helix_view::{
clipboard::ClipboardType,
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
- editor::{Action, Motion},
+ editor::{Action, CompleteAction, Motion},
info::Info,
input::KeyEvent,
keyboard::KeyCode,
@@ -4254,7 +4254,12 @@ 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);
- let savepoint = doc.savepoint(view);
+ let savepoint = if let Some(CompleteAction::Selected { savepoint }) = &cx.editor.last_completion
+ {
+ savepoint.clone()
+ } else {
+ doc.savepoint(view)
+ };
let trigger_doc = doc.id();
let trigger_view = view.id;
diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs
index bc216509..dd21be03 100644
--- a/helix-term/src/ui/completion.rs
+++ b/helix-term/src/ui/completion.rs
@@ -209,14 +209,27 @@ impl Completion {
let (view, doc) = current!(editor);
- // if more text was entered, remove it
- doc.restore(view, &savepoint);
-
match event {
- PromptEvent::Abort => {
- editor.last_completion = None;
- }
+ PromptEvent::Abort => {}
PromptEvent::Update => {
+ // Update creates "ghost" transactiosn which are not send to the
+ // lsp server to avoid messing up rerequesting completions. Once a
+ // completion has been selected (with) tab it's always accepted whenever anything
+ // is typed. The only way to avoid that is to explicitly abort the completion
+ // with esc/c-c. This will remove the "ghost" transaction.
+ //
+ // The ghost transaction is modeled with a transaction that is not send to the LS.
+ // (apply_temporary) and a savepoint. It's extremly important this savepoint is restored
+ // (also without sending the transaction to the LS) *before any further transaction is applied*.
+ // Otherwise incremental sync breaks (since the state of the LS doesn't match the state the transaction
+ // is applied to).
+ if editor.last_completion.is_none() {
+ editor.last_completion = Some(CompleteAction::Selected {
+ savepoint: doc.savepoint(view),
+ })
+ }
+ // if more text was entered, remove it
+ doc.restore(view, &savepoint, false);
// always present here
let item = item.unwrap();
@@ -229,19 +242,20 @@ impl Completion {
true,
replace_mode,
);
-
- // initialize a savepoint
- doc.apply(&transaction, view.id);
-
- editor.last_completion = Some(CompleteAction {
- trigger_offset,
- changes: completion_changes(&transaction, trigger_offset),
- });
+ doc.apply_temporary(&transaction, view.id);
}
PromptEvent::Validate => {
+ if let Some(CompleteAction::Selected { savepoint }) =
+ editor.last_completion.take()
+ {
+ doc.restore(view, &savepoint, false);
+ }
// always present here
let item = item.unwrap();
+
+ // if more text was entered, remove it
+ doc.restore(view, &savepoint, true);
let transaction = item_to_transaction(
doc,
view.id,
@@ -251,10 +265,9 @@ impl Completion {
false,
replace_mode,
);
-
doc.apply(&transaction, view.id);
- editor.last_completion = Some(CompleteAction {
+ editor.last_completion = Some(CompleteAction::Applied {
trigger_offset,
changes: completion_changes(&transaction, trigger_offset),
});
@@ -270,7 +283,6 @@ impl Completion {
} else {
Self::resolve_completion_item(doc, item.clone())
};
-
if let Some(additional_edits) = resolved_item
.as_ref()
.and_then(|item| item.additional_text_edits.as_ref())
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index 1ecbc8cc..f0989fa8 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -19,7 +19,7 @@ use helix_core::{
syntax::{self, HighlightEvent},
text_annotations::TextAnnotations,
unicode::width::UnicodeWidthStr,
- visual_offset_from_block, Position, Range, Selection, Transaction,
+ visual_offset_from_block, Change, Position, Range, Selection, Transaction,
};
use helix_view::{
document::{Mode, SavePoint, SCRATCH_BUFFER_NAME},
@@ -48,7 +48,10 @@ pub struct EditorView {
#[derive(Debug, Clone)]
pub enum InsertEvent {
Key(KeyEvent),
- CompletionApply(CompleteAction),
+ CompletionApply {
+ trigger_offset: usize,
+ changes: Vec<Change>,
+ },
TriggerCompletion,
RequestCompletion,
}
@@ -813,7 +816,7 @@ impl EditorView {
}
(Mode::Insert, Mode::Normal) => {
// if exiting insert mode, remove completion
- self.completion = None;
+ self.clear_completion(cxt.editor);
cxt.editor.completion_request_handle = None;
// TODO: Use an on_mode_change hook to remove signature help
@@ -891,22 +894,25 @@ impl EditorView {
for key in self.last_insert.1.clone() {
match key {
InsertEvent::Key(key) => self.insert_mode(cxt, key),
- InsertEvent::CompletionApply(compl) => {
+ InsertEvent::CompletionApply {
+ trigger_offset,
+ changes,
+ } => {
let (view, doc) = current!(cxt.editor);
if let Some(last_savepoint) = last_savepoint.as_deref() {
- doc.restore(view, last_savepoint);
+ doc.restore(view, last_savepoint, true);
}
let text = doc.text().slice(..);
let cursor = doc.selection(view.id).primary().cursor(text);
let shift_position =
- |pos: usize| -> usize { pos + cursor - compl.trigger_offset };
+ |pos: usize| -> usize { pos + cursor - trigger_offset };
let tx = Transaction::change(
doc.text(),
- compl.changes.iter().cloned().map(|(start, end, t)| {
+ changes.iter().cloned().map(|(start, end, t)| {
(shift_position(start), shift_position(end), t)
}),
);
@@ -979,6 +985,21 @@ impl EditorView {
pub fn clear_completion(&mut self, editor: &mut Editor) {
self.completion = None;
+ if let Some(last_completion) = editor.last_completion.take() {
+ match last_completion {
+ CompleteAction::Applied {
+ trigger_offset,
+ changes,
+ } => self.last_insert.1.push(InsertEvent::CompletionApply {
+ trigger_offset,
+ changes,
+ }),
+ CompleteAction::Selected { savepoint } => {
+ let (view, doc) = current!(editor);
+ doc.restore(view, &savepoint, false);
+ }
+ }
+ }
// Clear any savepoints
editor.clear_idle_timer(); // don't retrigger
@@ -1265,12 +1286,22 @@ impl Component for EditorView {
jobs: cx.jobs,
scroll: None,
};
- completion.handle_event(event, &mut cx)
- };
- if let EventResult::Consumed(callback) = res {
- consumed = true;
+ if let EventResult::Consumed(callback) =
+ completion.handle_event(event, &mut cx)
+ {
+ consumed = true;
+ Some(callback)
+ } else if let EventResult::Consumed(callback) =
+ completion.handle_event(&Event::Key(key!(Enter)), &mut cx)
+ {
+ Some(callback)
+ } else {
+ None
+ }
+ };
+ if let Some(callback) = res {
if callback.is_some() {
// assume close_fn
self.clear_completion(cx.editor);
@@ -1286,10 +1317,6 @@ impl Component for EditorView {
// if completion didn't take the event, we pass it onto commands
if !consumed {
- if let Some(compl) = cx.editor.last_completion.take() {
- self.last_insert.1.push(InsertEvent::CompletionApply(compl));
- }
-
self.insert_mode(&mut cx, key);
// record last_insert key