on master help sign in
ensure correct trigger/start completion offset

When re requesting a completion that already has a selected item we reuse that selections savepoint. However, the selection has likely changed since that savepoint which requires us to use the selection from that savepoint

commit9c558fc4705934097b5f20b100462fc1fa4f50e1 authorPascal Kuthe <pascal.kuthe@semimod.de> dateWed 5 Apr 2023, 01:38 (4 years ago) parent30ff7f8
2 files changed+24−9
helix-term/src/commands.rs+10−9 helix-view/src/document.rs+14
helix-term/src/commands.rs+10−9
@@ -4210,16 +4210,23 @@ pub fn completion(cx: &mut Context) {
42104210
42114211 let (view, doc) = current!(cx.editor);
42124212
4213+ let savepoint = if let Some(CompleteAction::Selected { savepoint }) = &cx.editor.last_completion
4214+ {
4215+ savepoint.clone()
4216+ } else {
4217+ doc.savepoint(view)
4218+ };
4219+
42134220 let language_server = match doc.language_server() {
42144221 Some(language_server) => language_server,
42154222 None => return,
42164223 };
42174224
42184225 let offset_encoding = language_server.offset_encoding();
4219− let text = doc.text().slice(..);
4220− let cursor = doc.selection(view.id).primary().cursor(text);
4226+ let text = savepoint.text.clone();
4227+ let cursor = savepoint.cursor();
42214228
4222− let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding);
4229+ let pos = pos_to_lsp_pos(&text, cursor, offset_encoding);
42234230
42244231 let future = match language_server.completion(doc.identifier(), pos, None) {
42254232 Some(future) => future,
@@ -4254,12 +4261,6 @@ pub fn completion(cx: &mut Context) {
42544261 iter.reverse();
42554262 let offset = iter.take_while(|ch| chars::char_is_word(*ch)).count();
42564263 let start_offset = cursor.saturating_sub(offset);
4257− let savepoint = if let Some(CompleteAction::Selected { savepoint }) = &cx.editor.last_completion
4258− {
4259− savepoint.clone()
4260− } else {
4261− doc.savepoint(view)
4262− };
42634264
42644265 let trigger_doc = doc.id();
42654266 let trigger_view = view.id;
helix-view/src/document.rs+14
@@ -114,6 +114,19 @@ pub struct SavePoint {
114114 /// The view this savepoint is associated with
115115 pub view: ViewId,
116116 revert: Mutex<Transaction>,
117+ pub text: Rope,
118+}
119+
120+impl SavePoint {
121+ pub fn cursor(&self) -> usize {
122+ // we always create transactions with selections
123+ self.revert
124+ .lock()
125+ .selection()
126+ .unwrap()
127+ .primary()
128+ .cursor(self.text.slice(..))
129+ }
117130}
118131
119132pub struct Document {
@@ -1230,6 +1243,7 @@ impl Document {
12301243 let savepoint = Arc::new(SavePoint {
12311244 view: view.id,
12321245 revert: Mutex::new(revert),
1246+ text: self.text.clone(),
12331247 });
12341248 self.savepoints.push(Arc::downgrade(&savepoint));
12351249 savepoint