feat(workspace): allow alternative actions to open files and symbols in split

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Alex Viscreanu 2023-07-14 21:41:23 +02:00
parent 369ccc725c
commit c7669317ec
No known key found for this signature in database
25 changed files with 277 additions and 80 deletions

View file

@ -271,7 +271,9 @@ actions!(
SelectLargerSyntaxNode,
SelectSmallerSyntaxNode,
GoToDefinition,
GoToDefinitionSplit,
GoToTypeDefinition,
GoToTypeDefinitionSplit,
MoveToEnclosingBracket,
UndoSelection,
RedoSelection,
@ -407,7 +409,9 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(Editor::go_to_hunk);
cx.add_action(Editor::go_to_prev_hunk);
cx.add_action(Editor::go_to_definition);
cx.add_action(Editor::go_to_definition_split);
cx.add_action(Editor::go_to_type_definition);
cx.add_action(Editor::go_to_type_definition_split);
cx.add_action(Editor::fold);
cx.add_action(Editor::fold_at);
cx.add_action(Editor::unfold_lines);
@ -6185,14 +6189,31 @@ impl Editor {
}
pub fn go_to_definition(&mut self, _: &GoToDefinition, cx: &mut ViewContext<Self>) {
self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, cx);
self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, false, cx);
}
pub fn go_to_type_definition(&mut self, _: &GoToTypeDefinition, cx: &mut ViewContext<Self>) {
self.go_to_definition_of_kind(GotoDefinitionKind::Type, cx);
self.go_to_definition_of_kind(GotoDefinitionKind::Type, false, cx);
}
fn go_to_definition_of_kind(&mut self, kind: GotoDefinitionKind, cx: &mut ViewContext<Self>) {
pub fn go_to_definition_split(&mut self, _: &GoToDefinitionSplit, cx: &mut ViewContext<Self>) {
self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, true, cx);
}
pub fn go_to_type_definition_split(
&mut self,
_: &GoToTypeDefinitionSplit,
cx: &mut ViewContext<Self>,
) {
self.go_to_definition_of_kind(GotoDefinitionKind::Type, true, cx);
}
fn go_to_definition_of_kind(
&mut self,
kind: GotoDefinitionKind,
split: bool,
cx: &mut ViewContext<Self>,
) {
let Some(workspace) = self.workspace(cx) else { return };
let buffer = self.buffer.read(cx);
let head = self.selections.newest::<usize>(cx).head();
@ -6211,7 +6232,7 @@ impl Editor {
cx.spawn_labeled("Fetching Definition...", |editor, mut cx| async move {
let definitions = definitions.await?;
editor.update(&mut cx, |editor, cx| {
editor.navigate_to_definitions(definitions, cx);
editor.navigate_to_definitions(definitions, split, cx);
})?;
Ok::<(), anyhow::Error>(())
})
@ -6221,6 +6242,7 @@ impl Editor {
pub fn navigate_to_definitions(
&mut self,
mut definitions: Vec<LocationLink>,
split: bool,
cx: &mut ViewContext<Editor>,
) {
let Some(workspace) = self.workspace(cx) else { return };
@ -6240,7 +6262,11 @@ impl Editor {
} else {
cx.window_context().defer(move |cx| {
let target_editor: ViewHandle<Self> = workspace.update(cx, |workspace, cx| {
workspace.open_project_item(definition.target.buffer.clone(), cx)
if split {
workspace.split_project_item(definition.target.buffer.clone(), cx)
} else {
workspace.open_project_item(definition.target.buffer.clone(), cx)
}
});
target_editor.update(cx, |target_editor, cx| {
// When selecting a definition in a different buffer, disable the nav history
@ -6276,7 +6302,9 @@ impl Editor {
.map(|definition| definition.target)
.collect();
workspace.update(cx, |workspace, cx| {
Self::open_locations_in_multibuffer(workspace, locations, replica_id, title, cx)
Self::open_locations_in_multibuffer(
workspace, locations, replica_id, title, split, cx,
)
});
});
}
@ -6321,7 +6349,7 @@ impl Editor {
})
.unwrap();
Self::open_locations_in_multibuffer(
workspace, locations, replica_id, title, cx,
workspace, locations, replica_id, title, false, cx,
);
})?;
@ -6336,6 +6364,7 @@ impl Editor {
mut locations: Vec<Location>,
replica_id: ReplicaId,
title: String,
split: bool,
cx: &mut ViewContext<Workspace>,
) {
// If there are multiple definitions, open them in a multibuffer
@ -6382,7 +6411,11 @@ impl Editor {
cx,
);
});
workspace.add_item(Box::new(editor), cx);
if split {
workspace.split_item(Box::new(editor), cx);
} else {
workspace.add_item(Box::new(editor), cx);
}
}
pub fn rename(&mut self, _: &Rename, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {

View file

@ -156,6 +156,7 @@ impl EditorElement {
event.position,
event.cmd,
event.shift,
event.alt,
position_map.as_ref(),
text_bounds,
cx,
@ -308,6 +309,7 @@ impl EditorElement {
position: Vector2F,
cmd: bool,
shift: bool,
alt: bool,
position_map: &PositionMap,
text_bounds: RectF,
cx: &mut EventContext<Editor>,
@ -324,9 +326,9 @@ impl EditorElement {
if point == target_point {
if shift {
go_to_fetched_type_definition(editor, point, cx);
go_to_fetched_type_definition(editor, point, alt, cx);
} else {
go_to_fetched_definition(editor, point, cx);
go_to_fetched_definition(editor, point, alt, cx);
}
return true;

View file

@ -246,23 +246,26 @@ pub fn hide_link_definition(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
pub fn go_to_fetched_definition(
editor: &mut Editor,
point: DisplayPoint,
split: bool,
cx: &mut ViewContext<Editor>,
) {
go_to_fetched_definition_of_kind(LinkDefinitionKind::Symbol, editor, point, cx);
go_to_fetched_definition_of_kind(LinkDefinitionKind::Symbol, editor, point, split, cx);
}
pub fn go_to_fetched_type_definition(
editor: &mut Editor,
point: DisplayPoint,
split: bool,
cx: &mut ViewContext<Editor>,
) {
go_to_fetched_definition_of_kind(LinkDefinitionKind::Type, editor, point, cx);
go_to_fetched_definition_of_kind(LinkDefinitionKind::Type, editor, point, split, cx);
}
fn go_to_fetched_definition_of_kind(
kind: LinkDefinitionKind,
editor: &mut Editor,
point: DisplayPoint,
split: bool,
cx: &mut ViewContext<Editor>,
) {
let cached_definitions = editor.link_go_to_definition_state.definitions.clone();
@ -275,7 +278,7 @@ fn go_to_fetched_definition_of_kind(
cx.focus_self();
}
editor.navigate_to_definitions(cached_definitions, cx);
editor.navigate_to_definitions(cached_definitions, split, cx);
} else {
editor.select(
SelectPhase::Begin {
@ -403,7 +406,7 @@ mod tests {
});
cx.update_editor(|editor, cx| {
go_to_fetched_type_definition(editor, hover_point, cx);
go_to_fetched_type_definition(editor, hover_point, false, cx);
});
requests.next().await;
cx.foreground().run_until_parked();
@ -614,7 +617,7 @@ mod tests {
// Cmd click with existing definition doesn't re-request and dismisses highlight
cx.update_editor(|editor, cx| {
go_to_fetched_definition(editor, hover_point, cx);
go_to_fetched_definition(editor, hover_point, false, cx);
});
// Assert selection moved to to definition
cx.lsp
@ -655,7 +658,7 @@ mod tests {
])))
});
cx.update_editor(|editor, cx| {
go_to_fetched_definition(editor, hover_point, cx);
go_to_fetched_definition(editor, hover_point, false, cx);
});
requests.next().await;
cx.foreground().run_until_parked();