mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-07 02:57:34 +00:00
Optimize CopilotState::text_for_active_completion
This commit is contained in:
parent
034bc75467
commit
da81ff3295
1 changed files with 9 additions and 8 deletions
|
@ -1030,21 +1030,22 @@ impl CopilotState {
|
||||||
cursor: Anchor,
|
cursor: Anchor,
|
||||||
buffer: &MultiBufferSnapshot,
|
buffer: &MultiBufferSnapshot,
|
||||||
) -> Option<&str> {
|
) -> Option<&str> {
|
||||||
|
let cursor_offset = cursor.to_offset(buffer);
|
||||||
let completion = self.completions.get(self.active_completion_index)?;
|
let completion = self.completions.get(self.active_completion_index)?;
|
||||||
if self.position.excerpt_id == cursor.excerpt_id
|
if self.position.excerpt_id == cursor.excerpt_id
|
||||||
&& self.position.buffer_id == cursor.buffer_id
|
&& self.position.buffer_id == cursor.buffer_id
|
||||||
&& buffer.chars_at(cursor).next().map_or(true, |ch| ch == '\n')
|
&& (cursor_offset == buffer.len() || buffer.contains_str_at(cursor_offset, "\n"))
|
||||||
{
|
{
|
||||||
let completion_position = Anchor {
|
let completion_offset = buffer.summary_for_anchor(&Anchor {
|
||||||
excerpt_id: self.position.excerpt_id,
|
excerpt_id: self.position.excerpt_id,
|
||||||
buffer_id: self.position.buffer_id,
|
buffer_id: self.position.buffer_id,
|
||||||
text_anchor: completion.position,
|
text_anchor: completion.position,
|
||||||
};
|
});
|
||||||
if completion_position.cmp(&cursor, buffer).is_le() {
|
let common_prefix_len = cursor_offset.saturating_sub(completion_offset);
|
||||||
let prefix = buffer
|
if common_prefix_len <= completion.text.len()
|
||||||
.text_for_range(completion_position..cursor)
|
&& buffer.contains_str_at(completion_offset, &completion.text[..common_prefix_len])
|
||||||
.collect::<String>();
|
{
|
||||||
let suffix = completion.text.strip_prefix(&prefix)?;
|
let suffix = &completion.text[common_prefix_len..];
|
||||||
if !suffix.is_empty() {
|
if !suffix.is_empty() {
|
||||||
return Some(suffix);
|
return Some(suffix);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue