@@ -1,5 +1,5 @@
1use crate::compositor::{Component, Context, Event, EventResult};
1use crate::compositor::{Component, Context, Event, EventResult};
2−use helix_view::{apply_transaction, editor::CompleteAction};
2+use helix_view::{apply_transaction, editor::CompleteAction, ViewId};
3use tui::buffer::Buffer as Surface;
3use tui::buffer::Buffer as Surface;
4use tui::text::Spans;
4use tui::text::Spans;
@@ -107,6 +107,7 @@ impl Completion {
107 let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {
107 let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {
108 fn item_to_transaction(
108 fn item_to_transaction(
109 doc: &Document,
109 doc: &Document,
110 item: &CompletionItem,
111 item: &CompletionItem,
111 offset_encoding: helix_lsp::OffsetEncoding,
112 offset_encoding: helix_lsp::OffsetEncoding,
112 start_offset: usize,
113 start_offset: usize,
@@ -121,9 +122,10 @@ impl Completion {
124− util::generate_transaction_from_edits(
125+ util::generate_transaction_from_completion_edit(
125 doc.text(),
126 doc.text(),
127+ doc.selection(view_id),
126− vec![edit],
128+ edit,
127 offset_encoding, // TODO: should probably transcode in Client
129 offset_encoding, // TODO: should probably transcode in Client
@@ -132,10 +134,23 @@ impl Completion {
132 // in these cases we need to check for a common prefix and remove it
134 // in these cases we need to check for a common prefix and remove it
133 let prefix = Cow::from(doc.text().slice(start_offset..trigger_offset));
135 let prefix = Cow::from(doc.text().slice(start_offset..trigger_offset));
134 let text = text.trim_start_matches::<&str>(&prefix);
136 let text = text.trim_start_matches::<&str>(&prefix);
135− Transaction::change(
137+
136− doc.text(),
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?
143+ doc.selection(view_id)
145+ .cursor(doc.text().slice(..))
137− vec![(trigger_offset, trigger_offset, Some(text.into()))].into_iter(),
146+ == trigger_offset
149+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
150+ let cursor = range.cursor(doc.text().slice(..));
152+ (cursor, cursor, Some(text.into()))
141 transaction
156 transaction
@@ -164,6 +179,7 @@ impl Completion {
165 let transaction = item_to_transaction(
180 let transaction = item_to_transaction(
168 offset_encoding,
184 offset_encoding,
169 start_offset,
185 start_offset,
@@ -185,6 +201,7 @@ impl Completion {
186 let transaction = item_to_transaction(
202 let transaction = item_to_transaction(
189 offset_encoding,
206 offset_encoding,
190 start_offset,
207 start_offset,