editor: Revert flattening of code actions in mouse context menu (#28988)

In light of making context not move dynamically, reverting back these
changes.

- Doing it async will lead to a loading state, which moves the context
menu.
- Doing it sync introduces noticeable lag in opening the context menu.
   
Future idea is to introduce fixed code actions like refactor, rewrite,
etc depending on code action kind [(see
more)](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeActionKind)
which will use submenus.
 
Release Notes:

- N/A
This commit is contained in:
Smit Barmase 2025-04-17 23:48:51 +05:30 committed by GitHub
parent 7e928dd615
commit ba588161d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 170 additions and 393 deletions

View file

@ -1,22 +1,15 @@
use crate::{
ConfirmCodeAction, Copy, CopyAndTrim, CopyPermalinkToLine, Cut, DebuggerEvaluateSelectedText,
DisplayPoint, DisplaySnapshot, Editor, FindAllReferences, GoToDeclaration, GoToDefinition,
Copy, CopyAndTrim, CopyPermalinkToLine, Cut, DebuggerEvaluateSelectedText, DisplayPoint,
DisplaySnapshot, Editor, FindAllReferences, GoToDeclaration, GoToDefinition,
GoToImplementation, GoToTypeDefinition, Paste, Rename, RevealInFileManager, SelectMode,
SelectionExt, ToDisplayPoint, ToggleCodeActions,
actions::{Format, FormatSelections},
code_context_menus::CodeActionContents,
selections_collection::SelectionsCollection,
};
use feature_flags::{Debugger, FeatureFlagAppExt as _};
use gpui::prelude::FluentBuilder;
use gpui::{
Context, DismissEvent, Entity, FocusHandle, Focusable as _, Pixels, Point, Subscription, Task,
Window,
};
use gpui::{Context, DismissEvent, Entity, Focusable as _, Pixels, Point, Subscription, Window};
use std::ops::Range;
use text::PointUtf16;
use ui::ContextMenu;
use util::ResultExt;
use workspace::OpenInTerminal;
#[derive(Debug)]
@ -32,24 +25,13 @@ pub enum MenuPosition {
},
}
pub struct MouseCodeAction {
pub actions: CodeActionContents,
pub buffer: Entity<language::Buffer>,
}
pub struct MouseContextMenu {
pub(crate) position: MenuPosition,
pub(crate) context_menu: Entity<ui::ContextMenu>,
pub(crate) code_action: Option<MouseCodeAction>,
_dismiss_subscription: Subscription,
_cursor_move_subscription: Subscription,
}
enum CodeActionLoadState {
Loading,
Loaded(CodeActionContents),
}
impl std::fmt::Debug for MouseContextMenu {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MouseContextMenu")
@ -64,7 +46,6 @@ impl MouseContextMenu {
editor: &mut Editor,
source: multi_buffer::Anchor,
position: Point<Pixels>,
code_action: Option<MouseCodeAction>,
context_menu: Entity<ui::ContextMenu>,
window: &mut Window,
cx: &mut Context<Editor>,
@ -84,7 +65,6 @@ impl MouseContextMenu {
editor,
menu_position,
context_menu,
code_action,
window,
cx,
));
@ -94,7 +74,6 @@ impl MouseContextMenu {
editor: &Editor,
position: MenuPosition,
context_menu: Entity<ui::ContextMenu>,
code_action: Option<MouseCodeAction>,
window: &mut Window,
cx: &mut Context<Editor>,
) -> Self {
@ -141,7 +120,6 @@ impl MouseContextMenu {
Self {
position,
context_menu,
code_action,
_dismiss_subscription,
_cursor_move_subscription,
}
@ -181,13 +159,13 @@ pub fn deploy_context_menu(
let display_map = editor.selections.display_map(cx);
let source_anchor = display_map.display_point_to_anchor(point, text::Bias::Right);
if let Some(custom) = editor.custom_context_menu.take() {
let context_menu = if let Some(custom) = editor.custom_context_menu.take() {
let menu = custom(editor, point, window, cx);
editor.custom_context_menu = Some(custom);
let Some(menu) = menu else {
return;
};
set_context_menu(editor, menu, source_anchor, position, None, window, cx);
menu
} else {
// Don't show the context menu if there isn't a project associated with this editor
let Some(project) = editor.project.clone() else {
@ -226,222 +204,75 @@ pub fn deploy_context_menu(
!filter.is_hidden(&DebuggerEvaluateSelectedText)
});
let menu = build_context_menu(
focus,
has_selections,
has_reveal_target,
has_git_repo,
evaluate_selection,
Some(CodeActionLoadState::Loading),
window,
cx,
);
set_context_menu(editor, menu, source_anchor, position, None, window, cx);
let mut actions_task = editor.code_actions_task.take();
cx.spawn_in(window, async move |editor, cx| {
while let Some(prev_task) = actions_task {
prev_task.await.log_err();
actions_task = editor.update(cx, |this, _| this.code_actions_task.take())?;
}
let action = ToggleCodeActions {
deployed_from_indicator: Some(point.row()),
};
let context_menu_task = editor.update_in(cx, |editor, window, cx| {
let code_actions_task = editor.prepare_code_actions_task(&action, window, cx);
Some(cx.spawn_in(window, async move |editor, cx| {
let code_action_result = code_actions_task.await;
if let Ok(editor_task) = editor.update_in(cx, |editor, window, cx| {
let Some(mouse_context_menu) = editor.mouse_context_menu.take() else {
return Task::ready(Ok::<_, anyhow::Error>(()));
};
if mouse_context_menu
.context_menu
.focus_handle(cx)
.contains_focused(window, cx)
{
window.focus(&editor.focus_handle(cx));
}
drop(mouse_context_menu);
let (state, code_action) =
if let Some((buffer, actions)) = code_action_result {
(
CodeActionLoadState::Loaded(actions.clone()),
Some(MouseCodeAction { actions, buffer }),
)
} else {
(
CodeActionLoadState::Loaded(CodeActionContents::default()),
None,
)
};
let menu = build_context_menu(
window.focused(cx),
has_selections,
has_reveal_target,
has_git_repo,
evaluate_selection,
Some(state),
window,
cx,
);
set_context_menu(
editor,
menu,
source_anchor,
position,
code_action,
window,
cx,
);
Task::ready(Ok(()))
}) {
editor_task.await
ui::ContextMenu::build(window, cx, |menu, _window, _cx| {
let builder = menu
.on_blur_subscription(Subscription::new(|| {}))
.action(
"Show Code Actions",
Box::new(ToggleCodeActions {
deployed_from_indicator: None,
}),
)
.separator()
.when(evaluate_selection && has_selections, |builder| {
builder
.action("Evaluate Selection", Box::new(DebuggerEvaluateSelectedText))
.separator()
})
.action("Go to Definition", Box::new(GoToDefinition))
.action("Go to Declaration", Box::new(GoToDeclaration))
.action("Go to Type Definition", Box::new(GoToTypeDefinition))
.action("Go to Implementation", Box::new(GoToImplementation))
.action("Find All References", Box::new(FindAllReferences))
.separator()
.action("Rename Symbol", Box::new(Rename))
.action("Format Buffer", Box::new(Format))
.when(has_selections, |cx| {
cx.action("Format Selections", Box::new(FormatSelections))
})
.separator()
.action("Cut", Box::new(Cut))
.action("Copy", Box::new(Copy))
.action("Copy and trim", Box::new(CopyAndTrim))
.action("Paste", Box::new(Paste))
.separator()
.map(|builder| {
let reveal_in_finder_label = if cfg!(target_os = "macos") {
"Reveal in Finder"
} else {
Ok(())
}
}))
})?;
if let Some(task) = context_menu_task {
task.await?;
}
Ok::<_, anyhow::Error>(())
})
.detach_and_log_err(cx);
};
}
fn build_context_menu(
focus: Option<FocusHandle>,
has_selections: bool,
has_reveal_target: bool,
has_git_repo: bool,
evaluate_selection: bool,
code_action_load_state: Option<CodeActionLoadState>,
window: &mut Window,
cx: &mut Context<Editor>,
) -> Entity<ContextMenu> {
ui::ContextMenu::build(window, cx, |menu, _window, cx| {
let menu = menu
.on_blur_subscription(Subscription::new(|| {}))
.when(evaluate_selection && has_selections, |builder| {
builder
.action("Evaluate Selection", Box::new(DebuggerEvaluateSelectedText))
.separator()
})
.action("Go to Definition", Box::new(GoToDefinition))
.action("Go to Declaration", Box::new(GoToDeclaration))
.action("Go to Type Definition", Box::new(GoToTypeDefinition))
.action("Go to Implementation", Box::new(GoToImplementation))
.action("Find All References", Box::new(FindAllReferences))
.separator()
.action("Rename Symbol", Box::new(Rename))
.action("Format Buffer", Box::new(Format))
.when(has_selections, |cx| {
cx.action("Format Selections", Box::new(FormatSelections))
})
.separator()
.action("Cut", Box::new(Cut))
.action("Copy", Box::new(Copy))
.action("Copy and trim", Box::new(CopyAndTrim))
.action("Paste", Box::new(Paste))
.separator()
.map(|builder| {
let reveal_in_finder_label = if cfg!(target_os = "macos") {
"Reveal in Finder"
} else {
"Reveal in File Manager"
};
const OPEN_IN_TERMINAL_LABEL: &str = "Open in Terminal";
if has_reveal_target {
builder
.action(reveal_in_finder_label, Box::new(RevealInFileManager))
.action(OPEN_IN_TERMINAL_LABEL, Box::new(OpenInTerminal))
} else {
builder
.disabled_action(reveal_in_finder_label, Box::new(RevealInFileManager))
.disabled_action(OPEN_IN_TERMINAL_LABEL, Box::new(OpenInTerminal))
}
})
.map(|builder| {
const COPY_PERMALINK_LABEL: &str = "Copy Permalink";
if has_git_repo {
builder.action(COPY_PERMALINK_LABEL, Box::new(CopyPermalinkToLine))
} else {
builder.disabled_action(COPY_PERMALINK_LABEL, Box::new(CopyPermalinkToLine))
}
})
.when_some(code_action_load_state, |menu, state| {
menu.separator().map(|menu| match state {
CodeActionLoadState::Loading => menu.disabled_action(
"Loading code actions...",
Box::new(ConfirmCodeAction {
item_ix: None,
from_mouse_context_menu: true,
}),
),
CodeActionLoadState::Loaded(actions) => {
if actions.is_empty() {
menu.disabled_action(
"No code actions available",
Box::new(ConfirmCodeAction {
item_ix: None,
from_mouse_context_menu: true,
}),
)
} else {
actions
.iter()
.filter(|action| {
if action
.as_task()
.map(|task| {
matches!(task.task_type(), task::TaskType::Debug(_))
})
.unwrap_or(false)
{
cx.has_flag::<Debugger>()
} else {
true
}
})
.enumerate()
.fold(menu, |menu, (ix, action)| {
menu.action(
action.label(),
Box::new(ConfirmCodeAction {
item_ix: Some(ix),
from_mouse_context_menu: true,
}),
)
})
}
"Reveal in File Manager"
};
const OPEN_IN_TERMINAL_LABEL: &str = "Open in Terminal";
if has_reveal_target {
builder
.action(reveal_in_finder_label, Box::new(RevealInFileManager))
.action(OPEN_IN_TERMINAL_LABEL, Box::new(OpenInTerminal))
} else {
builder
.disabled_action(reveal_in_finder_label, Box::new(RevealInFileManager))
.disabled_action(OPEN_IN_TERMINAL_LABEL, Box::new(OpenInTerminal))
}
})
});
match focus {
Some(focus) => menu.context(focus),
None => menu,
}
})
}
.map(|builder| {
const COPY_PERMALINK_LABEL: &str = "Copy Permalink";
if has_git_repo {
builder.action(COPY_PERMALINK_LABEL, Box::new(CopyPermalinkToLine))
} else {
builder.disabled_action(COPY_PERMALINK_LABEL, Box::new(CopyPermalinkToLine))
}
});
match focus {
Some(focus) => builder.context(focus),
None => builder,
}
})
};
fn set_context_menu(
editor: &mut Editor,
context_menu: Entity<ui::ContextMenu>,
source_anchor: multi_buffer::Anchor,
position: Option<Point<Pixels>>,
code_action: Option<MouseCodeAction>,
window: &mut Window,
cx: &mut Context<Editor>,
) {
editor.mouse_context_menu = match position {
Some(position) => MouseContextMenu::pinned_to_editor(
editor,
source_anchor,
position,
code_action,
context_menu,
window,
cx,
@ -456,7 +287,6 @@ fn set_context_menu(
editor,
menu_position,
context_menu,
code_action,
window,
cx,
))