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
pull/6446/merge
Pascal Kuthe 2 years ago committed by Blaž Hrastnik
parent 30ff7f8db2
commit 9c558fc470

@ -4210,16 +4210,23 @@ pub fn completion(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let savepoint = if let Some(CompleteAction::Selected { savepoint }) = &cx.editor.last_completion
{
savepoint.clone()
} else {
doc.savepoint(view)
};
let language_server = match doc.language_server() { let language_server = match doc.language_server() {
Some(language_server) => language_server, Some(language_server) => language_server,
None => return, None => return,
}; };
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let text = doc.text().slice(..); let text = savepoint.text.clone();
let cursor = doc.selection(view.id).primary().cursor(text); let cursor = savepoint.cursor();
let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding); let pos = pos_to_lsp_pos(&text, cursor, offset_encoding);
let future = match language_server.completion(doc.identifier(), pos, None) { let future = match language_server.completion(doc.identifier(), pos, None) {
Some(future) => future, Some(future) => future,
@ -4254,12 +4261,6 @@ pub fn completion(cx: &mut Context) {
iter.reverse(); iter.reverse();
let offset = iter.take_while(|ch| chars::char_is_word(*ch)).count(); let offset = iter.take_while(|ch| chars::char_is_word(*ch)).count();
let start_offset = cursor.saturating_sub(offset); let start_offset = cursor.saturating_sub(offset);
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_doc = doc.id();
let trigger_view = view.id; let trigger_view = view.id;

@ -114,6 +114,19 @@ pub struct SavePoint {
/// The view this savepoint is associated with /// The view this savepoint is associated with
pub view: ViewId, pub view: ViewId,
revert: Mutex<Transaction>, revert: Mutex<Transaction>,
pub text: Rope,
}
impl SavePoint {
pub fn cursor(&self) -> usize {
// we always create transactions with selections
self.revert
.lock()
.selection()
.unwrap()
.primary()
.cursor(self.text.slice(..))
}
} }
pub struct Document { pub struct Document {
@ -1230,6 +1243,7 @@ impl Document {
let savepoint = Arc::new(SavePoint { let savepoint = Arc::new(SavePoint {
view: view.id, view: view.id,
revert: Mutex::new(revert), revert: Mutex::new(revert),
text: self.text.clone(),
}); });
self.savepoints.push(Arc::downgrade(&savepoint)); self.savepoints.push(Arc::downgrade(&savepoint));
savepoint savepoint

Loading…
Cancel
Save