@@ -1,5 +1,5 @@
11use crate::compositor::{Component, Context, Event, EventResult};
2−use helix_view::{apply_transaction, editor::CompleteAction};
2+use helix_view::{apply_transaction, editor::CompleteAction, ViewId};
33use tui::buffer::Buffer as Surface;
44use tui::text::Spans;
55
@@ -107,6 +107,7 @@ impl Completion {
107107 let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {
108108 fn item_to_transaction(
109109 doc: &Document,
110+ view_id: ViewId,
110111 item: &CompletionItem,
111112 offset_encoding: helix_lsp::OffsetEncoding,
112113 start_offset: usize,
@@ -121,9 +122,10 @@ impl Completion {
121122 }
122123 };
123124
124− util::generate_transaction_from_edits(
125+ util::generate_transaction_from_completion_edit(
125126 doc.text(),
126− vec![edit],
127+ doc.selection(view_id),
128+ edit,
127129 offset_encoding, // TODO: should probably transcode in Client
128130 )
129131 } else {
@@ -132,10 +134,23 @@ impl Completion {
132134 // in these cases we need to check for a common prefix and remove it
133135 let prefix = Cow::from(doc.text().slice(start_offset..trigger_offset));
134136 let text = text.trim_start_matches::<&str>(&prefix);
135− Transaction::change(
136− doc.text(),
137− vec![(trigger_offset, trigger_offset, Some(text.into()))].into_iter(),
138− )
137+
138+ // TODO: this needs to be true for the numbers to work out correctly
139+ // in the closure below. It's passed in to a callback as this same
140+ // formula, but can the value change between the LSP request and
141+ // response? If it does, can we recover?
142+ debug_assert!(
143+ doc.selection(view_id)
144+ .primary()
145+ .cursor(doc.text().slice(..))
146+ == trigger_offset
147+ );
148+
149+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
150+ let cursor = range.cursor(doc.text().slice(..));
151+
152+ (cursor, cursor, Some(text.into()))
153+ })
139154 };
140155
141156 transaction
@@ -164,6 +179,7 @@ impl Completion {
164179
165180 let transaction = item_to_transaction(
166181 doc,
182+ view.id,
167183 item,
168184 offset_encoding,
169185 start_offset,
@@ -185,6 +201,7 @@ impl Completion {
185201
186202 let transaction = item_to_transaction(
187203 doc,
204+ view.id,
188205 item,
189206 offset_encoding,
190207 start_offset,