Compare commits
4 Commits
fix-evals
...
code-actio
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71fa6d599e | ||
|
|
20c5ae3729 | ||
|
|
62760c518d | ||
|
|
93a841431f |
@@ -811,6 +811,7 @@ pub struct Editor {
|
|||||||
next_completion_id: CompletionId,
|
next_completion_id: CompletionId,
|
||||||
available_code_actions: Option<(Location, Rc<[AvailableCodeAction]>)>,
|
available_code_actions: Option<(Location, Rc<[AvailableCodeAction]>)>,
|
||||||
code_actions_task: Option<Task<Result<()>>>,
|
code_actions_task: Option<Task<Result<()>>>,
|
||||||
|
force_code_action_task: bool,
|
||||||
selection_highlight_task: Option<Task<()>>,
|
selection_highlight_task: Option<Task<()>>,
|
||||||
document_highlights_task: Option<Task<()>>,
|
document_highlights_task: Option<Task<()>>,
|
||||||
linked_editing_range_task: Option<Task<Option<()>>>,
|
linked_editing_range_task: Option<Task<Option<()>>>,
|
||||||
@@ -1590,6 +1591,7 @@ impl Editor {
|
|||||||
code_action_providers,
|
code_action_providers,
|
||||||
available_code_actions: Default::default(),
|
available_code_actions: Default::default(),
|
||||||
code_actions_task: Default::default(),
|
code_actions_task: Default::default(),
|
||||||
|
force_code_action_task: false,
|
||||||
selection_highlight_task: Default::default(),
|
selection_highlight_task: Default::default(),
|
||||||
document_highlights_task: Default::default(),
|
document_highlights_task: Default::default(),
|
||||||
linked_editing_range_task: Default::default(),
|
linked_editing_range_task: Default::default(),
|
||||||
@@ -4932,89 +4934,6 @@ 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(
|
pub fn toggle_code_actions(
|
||||||
&mut self,
|
&mut self,
|
||||||
action: &ToggleCodeActions,
|
action: &ToggleCodeActions,
|
||||||
@@ -5035,48 +4954,106 @@ impl Editor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
drop(context_menu);
|
drop(context_menu);
|
||||||
|
let snapshot = self.snapshot(window, cx);
|
||||||
let deployed_from_indicator = action.deployed_from_indicator;
|
let deployed_from_indicator = action.deployed_from_indicator;
|
||||||
let mut task = self.code_actions_task.take();
|
let mut task = self.code_actions_task.take();
|
||||||
let action = action.clone();
|
let action = action.clone();
|
||||||
|
|
||||||
cx.spawn_in(window, async move |editor, cx| {
|
cx.spawn_in(window, async move |editor, cx| {
|
||||||
while let Some(prev_task) = task {
|
while let Some(prev_task) = task {
|
||||||
prev_task.await.log_err();
|
prev_task.await.log_err();
|
||||||
task = editor.update(cx, |this, _| this.code_actions_task.take())?;
|
task = editor.update(cx, |this, _| this.code_actions_task.take())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let context_menu_task = editor.update_in(cx, |editor, window, cx| {
|
let spawned_test_task = editor.update_in(cx, |editor, window, cx| {
|
||||||
if !editor.focus_handle.is_focused(window) {
|
if editor.focus_handle.is_focused(window) {
|
||||||
return Some(Task::ready(Ok(())));
|
let multibuffer_point = action
|
||||||
}
|
.deployed_from_indicator
|
||||||
let debugger_flag = cx.has_flag::<Debugger>();
|
.map(|row| DisplayPoint::new(row, 0).to_point(&snapshot))
|
||||||
let code_actions_task = editor.prepare_code_actions_task(&action, window, cx);
|
.unwrap_or_else(|| editor.selections.newest::<Point>(cx).head());
|
||||||
Some(cx.spawn_in(window, async move |editor, cx| {
|
let (buffer, buffer_row) = snapshot
|
||||||
if let Some((buffer, code_action_contents)) = code_actions_task.await {
|
.buffer_snapshot
|
||||||
let spawn_straight_away =
|
.buffer_line_for_row(MultiBufferRow(multibuffer_point.row))
|
||||||
code_action_contents.tasks.as_ref().map_or(false, |tasks| {
|
.and_then(|(buffer_snapshot, range)| {
|
||||||
tasks
|
editor
|
||||||
.templates
|
.buffer
|
||||||
.iter()
|
.read(cx)
|
||||||
.filter(|task| {
|
.buffer(buffer_snapshot.remote_id())
|
||||||
if matches!(task.1.task_type(), task::TaskType::Debug(_)) {
|
.map(|buffer| (buffer, range.start.row))
|
||||||
debugger_flag
|
})?;
|
||||||
} else {
|
let (_, code_actions) = editor
|
||||||
true
|
.available_code_actions
|
||||||
}
|
.clone()
|
||||||
})
|
.and_then(|(location, code_actions)| {
|
||||||
.count()
|
let snapshot = location.buffer.read(cx).snapshot();
|
||||||
== 1
|
let point_range = location.range.to_point(&snapshot);
|
||||||
}) && code_action_contents
|
let point_range = point_range.start.row..=point_range.end.row;
|
||||||
.actions
|
if point_range.contains(&buffer_row) {
|
||||||
.as_ref()
|
Some((location, code_actions))
|
||||||
.map_or(true, |actions| actions.is_empty());
|
} 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());
|
||||||
if let Ok(task) = editor.update_in(cx, |editor, window, cx| {
|
if let Ok(task) = editor.update_in(cx, |editor, window, cx| {
|
||||||
*editor.context_menu.borrow_mut() =
|
*editor.context_menu.borrow_mut() =
|
||||||
Some(CodeContextMenu::CodeActions(CodeActionsMenu {
|
Some(CodeContextMenu::CodeActions(CodeActionsMenu {
|
||||||
buffer,
|
buffer,
|
||||||
actions: code_action_contents,
|
actions: CodeActionContents {
|
||||||
|
tasks: resolved_tasks,
|
||||||
|
actions: code_actions,
|
||||||
|
},
|
||||||
selected_item: Default::default(),
|
selected_item: Default::default(),
|
||||||
scroll_handle: UniformListScrollHandle::default(),
|
scroll_handle: UniformListScrollHandle::default(),
|
||||||
deployed_from_indicator,
|
deployed_from_indicator,
|
||||||
@@ -5101,12 +5078,12 @@ impl Editor {
|
|||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
} else {
|
}))
|
||||||
Ok(())
|
} else {
|
||||||
}
|
Some(Task::ready(Ok(())))
|
||||||
}))
|
}
|
||||||
})?;
|
})?;
|
||||||
if let Some(task) = context_menu_task {
|
if let Some(task) = spawned_test_task {
|
||||||
task.await?;
|
task.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5125,10 +5102,10 @@ impl Editor {
|
|||||||
|
|
||||||
let (action, buffer) = if action.from_mouse_context_menu {
|
let (action, buffer) = if action.from_mouse_context_menu {
|
||||||
if let Some(menu) = self.mouse_context_menu.take() {
|
if let Some(menu) = self.mouse_context_menu.take() {
|
||||||
let code_action = menu.code_action?;
|
let code_action_state = menu.code_action_state?;
|
||||||
let index = action.item_ix?;
|
let index = action.item_ix?;
|
||||||
let action = code_action.actions.get(index)?;
|
let action = code_action_state.contents.get(index)?;
|
||||||
(action, code_action.buffer)
|
(action, code_action_state.buffer)
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -5348,11 +5325,14 @@ impl Editor {
|
|||||||
if start_buffer != end_buffer {
|
if start_buffer != end_buffer {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
let force_code_action_task = self.force_code_action_task;
|
||||||
|
|
||||||
self.code_actions_task = Some(cx.spawn_in(window, async move |this, cx| {
|
self.code_actions_task = Some(cx.spawn_in(window, async move |this, cx| {
|
||||||
cx.background_executor()
|
if !force_code_action_task {
|
||||||
.timer(CODE_ACTIONS_DEBOUNCE_TIMEOUT)
|
cx.background_executor()
|
||||||
.await;
|
.timer(CODE_ACTIONS_DEBOUNCE_TIMEOUT)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
let (providers, tasks) = this.update_in(cx, |this, window, cx| {
|
let (providers, tasks) = this.update_in(cx, |this, window, cx| {
|
||||||
let providers = this.code_action_providers.clone();
|
let providers = this.code_action_providers.clone();
|
||||||
|
|||||||
@@ -1,21 +1,18 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ConfirmCodeAction, Copy, CopyAndTrim, CopyPermalinkToLine, Cut, DebuggerEvaluateSelectedText,
|
ConfirmCodeAction, Copy, CopyAndTrim, CopyPermalinkToLine, Cut, DebuggerEvaluateSelectedText,
|
||||||
DisplayPoint, DisplaySnapshot, Editor, FindAllReferences, GoToDeclaration, GoToDefinition,
|
DisplayPoint, DisplaySnapshot, Editor, FindAllReferences, GoToDeclaration, GoToDefinition,
|
||||||
GoToImplementation, GoToTypeDefinition, Paste, Rename, RevealInFileManager, SelectMode,
|
GoToImplementation, GoToTypeDefinition, MultiBufferRow, OffsetRangeExt, Paste, Rename,
|
||||||
ToDisplayPoint, ToggleCodeActions,
|
RevealInFileManager, SelectMode, ToDisplayPoint,
|
||||||
actions::{Format, FormatSelections},
|
actions::{Format, FormatSelections},
|
||||||
code_context_menus::CodeActionContents,
|
code_context_menus::CodeActionContents,
|
||||||
selections_collection::SelectionsCollection,
|
selections_collection::SelectionsCollection,
|
||||||
};
|
};
|
||||||
use feature_flags::{Debugger, FeatureFlagAppExt as _};
|
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
Context, DismissEvent, Entity, FocusHandle, Focusable as _, Pixels, Point, Subscription, Task,
|
Context, DismissEvent, Entity, Focusable as _, Pixels, Point, Subscription, Task, Window,
|
||||||
Window,
|
|
||||||
};
|
};
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use text::PointUtf16;
|
use text::PointUtf16;
|
||||||
use ui::ContextMenu;
|
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
use workspace::OpenInTerminal;
|
use workspace::OpenInTerminal;
|
||||||
|
|
||||||
@@ -32,23 +29,18 @@ pub enum MenuPosition {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MouseCodeAction {
|
pub struct MouseCodeActionState {
|
||||||
pub actions: CodeActionContents,
|
pub contents: CodeActionContents,
|
||||||
pub buffer: Entity<language::Buffer>,
|
pub buffer: Entity<language::Buffer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MouseContextMenu {
|
pub struct MouseContextMenu {
|
||||||
pub(crate) position: MenuPosition,
|
pub(crate) position: MenuPosition,
|
||||||
pub(crate) context_menu: Entity<ui::ContextMenu>,
|
pub(crate) context_menu: Entity<ui::ContextMenu>,
|
||||||
pub(crate) code_action: Option<MouseCodeAction>,
|
pub(crate) code_action_state: Option<MouseCodeActionState>,
|
||||||
_subscription: Subscription,
|
_subscription: Subscription,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CodeActionLoadState {
|
|
||||||
Loading,
|
|
||||||
Loaded(CodeActionContents),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for MouseContextMenu {
|
impl std::fmt::Debug for MouseContextMenu {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("MouseContextMenu")
|
f.debug_struct("MouseContextMenu")
|
||||||
@@ -63,7 +55,7 @@ impl MouseContextMenu {
|
|||||||
editor: &mut Editor,
|
editor: &mut Editor,
|
||||||
source: multi_buffer::Anchor,
|
source: multi_buffer::Anchor,
|
||||||
position: Point<Pixels>,
|
position: Point<Pixels>,
|
||||||
code_action: Option<MouseCodeAction>,
|
code_action_state: Option<MouseCodeActionState>,
|
||||||
context_menu: Entity<ui::ContextMenu>,
|
context_menu: Entity<ui::ContextMenu>,
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
cx: &mut Context<Editor>,
|
cx: &mut Context<Editor>,
|
||||||
@@ -82,7 +74,7 @@ impl MouseContextMenu {
|
|||||||
return Some(MouseContextMenu::new(
|
return Some(MouseContextMenu::new(
|
||||||
menu_position,
|
menu_position,
|
||||||
context_menu,
|
context_menu,
|
||||||
code_action,
|
code_action_state,
|
||||||
window,
|
window,
|
||||||
cx,
|
cx,
|
||||||
));
|
));
|
||||||
@@ -91,7 +83,7 @@ impl MouseContextMenu {
|
|||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
position: MenuPosition,
|
position: MenuPosition,
|
||||||
context_menu: Entity<ui::ContextMenu>,
|
context_menu: Entity<ui::ContextMenu>,
|
||||||
code_action: Option<MouseCodeAction>,
|
code_action_state: Option<MouseCodeActionState>,
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
cx: &mut Context<Editor>,
|
cx: &mut Context<Editor>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@@ -112,7 +104,7 @@ impl MouseContextMenu {
|
|||||||
Self {
|
Self {
|
||||||
position,
|
position,
|
||||||
context_menu,
|
context_menu,
|
||||||
code_action,
|
code_action_state,
|
||||||
_subscription,
|
_subscription,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,241 +160,212 @@ pub fn deploy_context_menu(
|
|||||||
let buffer = &editor.snapshot(window, cx).buffer_snapshot;
|
let buffer = &editor.snapshot(window, cx).buffer_snapshot;
|
||||||
let anchor = buffer.anchor_before(point.to_point(&display_map));
|
let anchor = buffer.anchor_before(point.to_point(&display_map));
|
||||||
if !display_ranges(&display_map, &editor.selections).any(|r| r.contains(&point)) {
|
if !display_ranges(&display_map, &editor.selections).any(|r| r.contains(&point)) {
|
||||||
|
// We don't need to debounce in case of mouse click
|
||||||
|
editor.force_code_action_task = true;
|
||||||
// Move the cursor to the clicked location so that dispatched actions make sense
|
// Move the cursor to the clicked location so that dispatched actions make sense
|
||||||
editor.change_selections(None, window, cx, |s| {
|
editor.change_selections(None, window, cx, |s| {
|
||||||
s.clear_disjoint();
|
s.clear_disjoint();
|
||||||
s.set_pending_anchor_range(anchor..anchor, SelectMode::Character);
|
s.set_pending_anchor_range(anchor..anchor, SelectMode::Character);
|
||||||
});
|
});
|
||||||
|
editor.force_code_action_task = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let focus = window.focused(cx);
|
let mut code_action_task = editor.code_actions_task.take();
|
||||||
let has_reveal_target = editor.target_file(cx).is_some();
|
|
||||||
let has_selections = editor
|
|
||||||
.selections
|
|
||||||
.all::<PointUtf16>(cx)
|
|
||||||
.into_iter()
|
|
||||||
.any(|s| !s.is_empty());
|
|
||||||
let has_git_repo = anchor.buffer_id.is_some_and(|buffer_id| {
|
|
||||||
project
|
|
||||||
.read(cx)
|
|
||||||
.git_store()
|
|
||||||
.read(cx)
|
|
||||||
.repository_and_path_for_buffer_id(buffer_id, cx)
|
|
||||||
.is_some()
|
|
||||||
});
|
|
||||||
|
|
||||||
let evaluate_selection = command_palette_hooks::CommandPaletteFilter::try_global(cx)
|
|
||||||
.map_or(false, |filter| {
|
|
||||||
!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| {
|
cx.spawn_in(window, async move |editor, cx| {
|
||||||
while let Some(prev_task) = actions_task {
|
while let Some(prev_task) = code_action_task {
|
||||||
prev_task.await.log_err();
|
prev_task.await.log_err();
|
||||||
actions_task = editor.update(cx, |this, _| this.code_actions_task.take())?;
|
code_action_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 context_menu_task = editor.update_in(cx, |editor, window, cx| {
|
||||||
let code_actions_task = editor.prepare_code_actions_task(&action, window, cx);
|
let focus = window.focused(cx);
|
||||||
Some(cx.spawn_in(window, async move |editor, cx| {
|
let has_reveal_target = editor.target_file(cx).is_some();
|
||||||
let code_action_result = code_actions_task.await;
|
let has_selections = editor
|
||||||
if let Ok(editor_task) = editor.update_in(cx, |editor, window, cx| {
|
.selections
|
||||||
let Some(mouse_context_menu) = editor.mouse_context_menu.take() else {
|
.all::<PointUtf16>(cx)
|
||||||
return Task::ready(Ok::<_, anyhow::Error>(()));
|
.into_iter()
|
||||||
};
|
.any(|s| !s.is_empty());
|
||||||
if mouse_context_menu
|
let has_git_repo = anchor.buffer_id.is_some_and(|buffer_id| {
|
||||||
.context_menu
|
project
|
||||||
.focus_handle(cx)
|
.read(cx)
|
||||||
.contains_focused(window, cx)
|
.git_store()
|
||||||
{
|
.read(cx)
|
||||||
window.focus(&editor.focus_handle(cx));
|
.repository_and_path_for_buffer_id(buffer_id, cx)
|
||||||
}
|
.is_some()
|
||||||
drop(mouse_context_menu);
|
});
|
||||||
let (state, code_action) =
|
|
||||||
if let Some((buffer, actions)) = code_action_result {
|
let evaluate_selection =
|
||||||
(
|
command_palette_hooks::CommandPaletteFilter::try_global(cx)
|
||||||
CodeActionLoadState::Loaded(actions.clone()),
|
.map_or(false, |filter| {
|
||||||
Some(MouseCodeAction { actions, buffer }),
|
!filter.is_hidden(&DebuggerEvaluateSelectedText)
|
||||||
)
|
});
|
||||||
} else {
|
|
||||||
(
|
let editor_snapshot = &editor.snapshot(window, cx);
|
||||||
CodeActionLoadState::Loaded(CodeActionContents::default()),
|
let code_action_state = {
|
||||||
None,
|
if let Some((buffer, buffer_row)) = editor_snapshot
|
||||||
)
|
.buffer_snapshot
|
||||||
};
|
.buffer_line_for_row(MultiBufferRow(point.to_point(editor_snapshot).row))
|
||||||
let menu = build_context_menu(
|
.and_then(|(buffer_snapshot, range)| {
|
||||||
window.focused(cx),
|
editor
|
||||||
has_selections,
|
.buffer
|
||||||
has_reveal_target,
|
.read(cx)
|
||||||
has_git_repo,
|
.buffer(buffer_snapshot.remote_id())
|
||||||
evaluate_selection,
|
.map(|buffer| (buffer, range.start.row))
|
||||||
Some(state),
|
})
|
||||||
window,
|
{
|
||||||
cx,
|
editor.available_code_actions.clone().and_then(
|
||||||
);
|
|(location, code_actions)| {
|
||||||
set_context_menu(
|
let snapshot = location.buffer.read(cx).snapshot();
|
||||||
editor,
|
let point_range = location.range.to_point(&snapshot);
|
||||||
menu,
|
let point_range = point_range.start.row..=point_range.end.row;
|
||||||
source_anchor,
|
if point_range.contains(&buffer_row) {
|
||||||
position,
|
Some(MouseCodeActionState {
|
||||||
code_action,
|
contents: CodeActionContents {
|
||||||
window,
|
actions: Some(code_actions),
|
||||||
cx,
|
tasks: None,
|
||||||
);
|
},
|
||||||
Task::ready(Ok(()))
|
buffer,
|
||||||
}) {
|
})
|
||||||
editor_task.await
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
None
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
|
|
||||||
|
let action_labels = code_action_state
|
||||||
|
.as_ref()
|
||||||
|
.map(|state| {
|
||||||
|
if state.contents.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(
|
||||||
|
state
|
||||||
|
.contents
|
||||||
|
.iter()
|
||||||
|
.map(|item| item.label())
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
let menu = ui::ContextMenu::build(window, cx, |menu, _window, _| {
|
||||||
|
let menu = menu
|
||||||
|
.on_blur_subscription(Subscription::new(|| {}))
|
||||||
|
.when_some(action_labels, |menu, labels| {
|
||||||
|
menu.map(|menu| {
|
||||||
|
labels
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.fold(menu, |menu, (ix, label)| {
|
||||||
|
menu.action(
|
||||||
|
label,
|
||||||
|
Box::new(ConfirmCodeAction {
|
||||||
|
item_ix: Some(ix),
|
||||||
|
from_mouse_context_menu: true,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.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 {
|
||||||
|
"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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
match focus {
|
||||||
|
Some(focus) => menu.context(focus),
|
||||||
|
None => menu,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
set_context_menu(
|
||||||
|
editor,
|
||||||
|
menu,
|
||||||
|
source_anchor,
|
||||||
|
position,
|
||||||
|
code_action_state,
|
||||||
|
window,
|
||||||
|
cx,
|
||||||
|
);
|
||||||
|
|
||||||
|
Some(Task::ready(Ok::<_, anyhow::Error>(())))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if let Some(task) = context_menu_task {
|
if let Some(task) = context_menu_task {
|
||||||
task.await?;
|
task.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok::<_, anyhow::Error>(())
|
Ok::<_, anyhow::Error>(())
|
||||||
})
|
})
|
||||||
.detach_and_log_err(cx);
|
.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,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
match focus {
|
|
||||||
Some(focus) => menu.context(focus),
|
|
||||||
None => menu,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_context_menu(
|
fn set_context_menu(
|
||||||
editor: &mut Editor,
|
editor: &mut Editor,
|
||||||
context_menu: Entity<ui::ContextMenu>,
|
context_menu: Entity<ui::ContextMenu>,
|
||||||
source_anchor: multi_buffer::Anchor,
|
source_anchor: multi_buffer::Anchor,
|
||||||
position: Option<Point<Pixels>>,
|
position: Option<Point<Pixels>>,
|
||||||
code_action: Option<MouseCodeAction>,
|
code_action_state: Option<MouseCodeActionState>,
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
cx: &mut Context<Editor>,
|
cx: &mut Context<Editor>,
|
||||||
) {
|
) {
|
||||||
@@ -411,7 +374,7 @@ fn set_context_menu(
|
|||||||
editor,
|
editor,
|
||||||
source_anchor,
|
source_anchor,
|
||||||
position,
|
position,
|
||||||
code_action,
|
code_action_state,
|
||||||
context_menu,
|
context_menu,
|
||||||
window,
|
window,
|
||||||
cx,
|
cx,
|
||||||
@@ -425,7 +388,7 @@ fn set_context_menu(
|
|||||||
Some(MouseContextMenu::new(
|
Some(MouseContextMenu::new(
|
||||||
menu_position,
|
menu_position,
|
||||||
context_menu,
|
context_menu,
|
||||||
code_action,
|
code_action_state,
|
||||||
window,
|
window,
|
||||||
cx,
|
cx,
|
||||||
))
|
))
|
||||||
|
|||||||
Reference in New Issue
Block a user