Skip to content

Commit a4f6fa9

Browse files
imumesh18bennetbo
authored andcommitted
agent: Fix inline assistant focusing behavior for cursor placement (zed-industries#29998)
Ref: zed-industries#29919 This PR improves how inline assistants are detected and focused based on cursor position. ### Problem The current implementation has inconsistent behavior: - When selecting text within an inline assistant's range, the assistant properly focuses - When placing a cursor on a line containing an assistant (without selection), a new assistant is created instead of focusing the existing one ### Solution Enhanced the assistant detection logic to: - Check if the cursor is anywhere within the line range of an existing assistant - Maintain the same behavior for both cursor placement and text selection - Convert both cursor position and assistant ranges to points for better line-based comparison This creates a more intuitive editing experience when working with inline assistants, reducing the creation of duplicate assistants when the user intends to interact with existing ones. https://github.com/user-attachments/assets/55eb80d1-76a7-4d42-aac4-2702e85f13c4 Release Notes: - agent: Improved inline assistant behavior to focus existing assistants when cursor is placed on their line, matching selection behavior --------- Co-authored-by: Bennet Bo Fenner <bennet@zed.dev>
1 parent aefb784 commit a4f6fa9

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

crates/agent/src/inline_assistant.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,27 @@ impl InlineAssistant {
338338
window: &mut Window,
339339
cx: &mut App,
340340
) {
341-
let (snapshot, initial_selections) = editor.update(cx, |editor, cx| {
342-
(
343-
editor.snapshot(window, cx),
344-
editor.selections.all::<Point>(cx),
345-
)
341+
let (snapshot, initial_selections, newest_selection) = editor.update(cx, |editor, cx| {
342+
let selections = editor.selections.all::<Point>(cx);
343+
let newest_selection = editor.selections.newest::<Point>(cx);
344+
(editor.snapshot(window, cx), selections, newest_selection)
346345
});
347346

347+
// Check if there is already an inline assistant that contains the
348+
// newest selection, if there is, focus it
349+
if let Some(editor_assists) = self.assists_by_editor.get(&editor.downgrade()) {
350+
for assist_id in &editor_assists.assist_ids {
351+
let assist = &self.assists[assist_id];
352+
let range = assist.range.to_point(&snapshot.buffer_snapshot);
353+
if range.start.row <= newest_selection.start.row
354+
&& newest_selection.end.row <= range.end.row
355+
{
356+
self.focus_assist(*assist_id, window, cx);
357+
return;
358+
}
359+
}
360+
}
361+
348362
let mut selections = Vec::<Selection<Point>>::new();
349363
let mut newest_selection = None;
350364
for mut selection in initial_selections {

0 commit comments

Comments
 (0)