editor: Show code actions in mouse context menu (#28677)

Closes #27989

Asynchronous fetch of code actions on right-click, and shows them in
context menu.


https://github.com/user-attachments/assets/413eb0dd-cd1c-4628-a6f1-84eac813da32

Release Notes:

- Improved visibility of code actions by showing them in right-click
context menu.
This commit is contained in:
Smit Barmase 2025-04-14 17:44:00 +05:30 committed by GitHub
parent 98891e4c70
commit f2ce183286
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 398 additions and 173 deletions

View file

@ -1693,6 +1693,7 @@ impl Editor {
self.mouse_context_menu = Some(MouseContextMenu::new(
crate::mouse_context_menu::MenuPosition::PinnedToScreen(position),
context_menu,
None,
window,
cx,
));
@ -4833,6 +4834,89 @@ impl Editor {
}))
}
fn prepare_code_actions_task(
&mut self,
action: &ToggleCodeActions,
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<Option<(Entity<Buffer>, CodeActionContents)>> {
let snapshot = self.snapshot(window, cx);
let multibuffer_point = action
.deployed_from_indicator
.map(|row| DisplayPoint::new(row, 0).to_point(&snapshot))
.unwrap_or_else(|| self.selections.newest::<Point>(cx).head());
let Some((buffer, buffer_row)) = snapshot
.buffer_snapshot
.buffer_line_for_row(MultiBufferRow(multibuffer_point.row))
.and_then(|(buffer_snapshot, range)| {
self.buffer
.read(cx)
.buffer(buffer_snapshot.remote_id())
.map(|buffer| (buffer, range.start.row))
})
else {
return Task::ready(None);
};
let (_, code_actions) = self
.available_code_actions
.clone()
.and_then(|(location, code_actions)| {
let snapshot = location.buffer.read(cx).snapshot();
let point_range = location.range.to_point(&snapshot);
let point_range = point_range.start.row..=point_range.end.row;
if point_range.contains(&buffer_row) {
Some((location, code_actions))
} else {
None
}
})
.unzip();
let buffer_id = buffer.read(cx).remote_id();
let tasks = self
.tasks
.get(&(buffer_id, buffer_row))
.map(|t| Arc::new(t.to_owned()));
if tasks.is_none() && code_actions.is_none() {
return Task::ready(None);
}
self.completion_tasks.clear();
self.discard_inline_completion(false, cx);
let task_context = tasks
.as_ref()
.zip(self.project.clone())
.map(|(tasks, project)| {
Self::build_tasks_context(&project, &buffer, buffer_row, tasks, cx)
});
cx.spawn_in(window, async move |_, _| {
let task_context = match task_context {
Some(task_context) => task_context.await,
None => None,
};
let resolved_tasks = tasks.zip(task_context).map(|(tasks, task_context)| {
Rc::new(ResolvedTasks {
templates: tasks.resolve(&task_context).collect(),
position: snapshot
.buffer_snapshot
.anchor_before(Point::new(multibuffer_point.row, tasks.column)),
})
});
Some((
buffer,
CodeActionContents {
actions: code_actions,
tasks: resolved_tasks,
},
))
})
}
pub fn toggle_code_actions(
&mut self,
action: &ToggleCodeActions,
@ -4853,113 +4937,58 @@ impl Editor {
}
}
drop(context_menu);
let snapshot = self.snapshot(window, cx);
let deployed_from_indicator = action.deployed_from_indicator;
let mut task = self.code_actions_task.take();
let action = action.clone();
cx.spawn_in(window, async move |editor, cx| {
while let Some(prev_task) = task {
prev_task.await.log_err();
task = editor.update(cx, |this, _| this.code_actions_task.take())?;
}
let spawned_test_task = editor.update_in(cx, |editor, window, cx| {
if editor.focus_handle.is_focused(window) {
let multibuffer_point = action
.deployed_from_indicator
.map(|row| DisplayPoint::new(row, 0).to_point(&snapshot))
.unwrap_or_else(|| editor.selections.newest::<Point>(cx).head());
let (buffer, buffer_row) = snapshot
.buffer_snapshot
.buffer_line_for_row(MultiBufferRow(multibuffer_point.row))
.and_then(|(buffer_snapshot, range)| {
editor
.buffer
.read(cx)
.buffer(buffer_snapshot.remote_id())
.map(|buffer| (buffer, range.start.row))
})?;
let (_, code_actions) = editor
.available_code_actions
.clone()
.and_then(|(location, code_actions)| {
let snapshot = location.buffer.read(cx).snapshot();
let point_range = location.range.to_point(&snapshot);
let point_range = point_range.start.row..=point_range.end.row;
if point_range.contains(&buffer_row) {
Some((location, code_actions))
} else {
None
}
})
.unzip();
let buffer_id = buffer.read(cx).remote_id();
let tasks = editor
.tasks
.get(&(buffer_id, buffer_row))
.map(|t| Arc::new(t.to_owned()));
if tasks.is_none() && code_actions.is_none() {
return None;
}
editor.completion_tasks.clear();
editor.discard_inline_completion(false, cx);
let task_context =
tasks
.as_ref()
.zip(editor.project.clone())
.map(|(tasks, project)| {
Self::build_tasks_context(&project, &buffer, buffer_row, tasks, cx)
});
let debugger_flag = cx.has_flag::<Debugger>();
Some(cx.spawn_in(window, async move |editor, cx| {
let task_context = match task_context {
Some(task_context) => task_context.await,
None => None,
};
let resolved_tasks =
tasks.zip(task_context).map(|(tasks, task_context)| {
Rc::new(ResolvedTasks {
templates: tasks.resolve(&task_context).collect(),
position: snapshot.buffer_snapshot.anchor_before(Point::new(
multibuffer_point.row,
tasks.column,
)),
})
});
let spawn_straight_away = resolved_tasks.as_ref().map_or(false, |tasks| {
tasks
.templates
.iter()
.filter(|task| {
if matches!(task.1.task_type(), task::TaskType::Debug(_)) {
debugger_flag
} else {
true
}
})
.count()
== 1
}) && code_actions
.as_ref()
.map_or(true, |actions| actions.is_empty());
let context_menu_task = editor.update_in(cx, |editor, window, cx| {
if !editor.focus_handle.is_focused(window) {
return Some(Task::ready(Ok(())));
}
let debugger_flag = cx.has_flag::<Debugger>();
let code_actions_task = editor.prepare_code_actions_task(&action, window, cx);
Some(cx.spawn_in(window, async move |editor, cx| {
if let Some((buffer, code_action_contents)) = code_actions_task.await {
let spawn_straight_away =
code_action_contents.tasks.as_ref().map_or(false, |tasks| {
tasks
.templates
.iter()
.filter(|task| {
if matches!(task.1.task_type(), task::TaskType::Debug(_)) {
debugger_flag
} else {
true
}
})
.count()
== 1
}) && code_action_contents
.actions
.as_ref()
.map_or(true, |actions| actions.is_empty());
if let Ok(task) = editor.update_in(cx, |editor, window, cx| {
*editor.context_menu.borrow_mut() =
Some(CodeContextMenu::CodeActions(CodeActionsMenu {
buffer,
actions: CodeActionContents {
tasks: resolved_tasks,
actions: code_actions,
},
actions: code_action_contents,
selected_item: Default::default(),
scroll_handle: UniformListScrollHandle::default(),
deployed_from_indicator,
}));
if spawn_straight_away {
if let Some(task) = editor.confirm_code_action(
&ConfirmCodeAction { item_ix: Some(0) },
&ConfirmCodeAction {
item_ix: Some(0),
from_mouse_context_menu: false,
},
window,
cx,
) {
@ -4974,12 +5003,12 @@ impl Editor {
} else {
Ok(())
}
}))
} else {
Some(Task::ready(Ok(())))
}
} else {
Ok(())
}
}))
})?;
if let Some(task) = spawned_test_task {
if let Some(task) = context_menu_task {
task.await?;
}
@ -4996,17 +5025,27 @@ impl Editor {
) -> Option<Task<Result<()>>> {
self.hide_mouse_cursor(&HideMouseCursorOrigin::TypingAction);
let actions_menu =
if let CodeContextMenu::CodeActions(menu) = self.hide_context_menu(window, cx)? {
menu
let (action, buffer) = if action.from_mouse_context_menu {
if let Some(menu) = self.mouse_context_menu.take() {
let code_action = menu.code_action?;
let index = action.item_ix?;
let action = code_action.actions.get(index)?;
(action, code_action.buffer)
} else {
return None;
};
}
} else {
if let CodeContextMenu::CodeActions(menu) = self.hide_context_menu(window, cx)? {
let action_ix = action.item_ix.unwrap_or(menu.selected_item);
let action = menu.actions.get(action_ix)?;
let buffer = menu.buffer;
(action, buffer)
} else {
return None;
}
};
let action_ix = action.item_ix.unwrap_or(actions_menu.selected_item);
let action = actions_menu.actions.get(action_ix)?;
let title = action.label();
let buffer = actions_menu.buffer;
let workspace = self.workspace()?;
match action {
@ -8803,6 +8842,7 @@ impl Editor {
self,
source,
clicked_point,
None,
context_menu,
window,
cx,