From ce9774be530abfd692dafa7d84b4af15967b39d9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 28 Mar 2023 09:50:03 +0200 Subject: [PATCH] Improve detection of common prefix in `text_for_active_completion` --- crates/editor/src/editor.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 6b47034674..9c1b0913aa 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1034,19 +1034,16 @@ impl CopilotState { let completion = self.completions.get(self.active_completion_index)?; if self.position.excerpt_id == cursor.excerpt_id && self.position.buffer_id == cursor.buffer_id - && (cursor_offset == buffer.len() || buffer.contains_str_at(cursor_offset, "\n")) { - let completion_offset = buffer.summary_for_anchor(&Anchor { + let completion_offset: usize = buffer.summary_for_anchor(&Anchor { excerpt_id: self.position.excerpt_id, buffer_id: self.position.buffer_id, text_anchor: completion.position, }); - let common_prefix_len = cursor_offset.saturating_sub(completion_offset); - if common_prefix_len <= completion.text.len() - && buffer.contains_str_at(completion_offset, &completion.text[..common_prefix_len]) - { - let suffix = &completion.text[common_prefix_len..]; - if !suffix.is_empty() { + let prefix_len = cursor_offset.saturating_sub(completion_offset); + if completion_offset <= cursor_offset && prefix_len <= completion.text.len() { + let (prefix, suffix) = completion.text.split_at(prefix_len); + if buffer.contains_str_at(completion_offset, prefix) && !suffix.is_empty() { return Some(suffix); } } @@ -2867,8 +2864,8 @@ impl Editor { .text_for_active_completion(self.copilot_state.position, &snapshot) .map(|text| text.to_string()) { - self.copilot_state = Default::default(); self.insert(&text, cx); + self.clear_copilot_suggestions(cx); true } else { false