Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Handle lens navigation targets similar to code actions (#54664)

Kirill Bulatov052184e01ea6
diff --git a/crates/editor/src/code_lens.rs b/crates/editor/src/code_lens.rs
index c1bf2525d9e..87d2426878e 100644
--- a/crates/editor/src/code_lens.rs
+++ b/crates/editor/src/code_lens.rs
@@ -1,4 +1,4 @@
-use std::{iter, ops::Range, sync::Arc};
+use std::sync::Arc;
use collections::{HashMap, HashSet};
use futures::future::join_all;
@@ -7,17 +7,15 @@ use itertools::Itertools;
use language::{BufferId, ClientCommand};
use multi_buffer::{Anchor, MultiBufferRow, MultiBufferSnapshot, ToPoint as _};
use project::{CodeAction, TaskSourceKind};
-use settings::Settings as _;
use task::TaskContext;
-use text::Point;
use ui::{Context, Window, div, prelude::*};
-use workspace::PreviewTabsSettings;
use crate::{
- Editor, LSP_REQUEST_DEBOUNCE_TIMEOUT, MultibufferSelectionMode, SelectionEffects,
+ Editor, LSP_REQUEST_DEBOUNCE_TIMEOUT, SelectionEffects,
actions::ToggleCodeLens,
display_map::{BlockPlacement, BlockProperties, BlockStyle, CustomBlockId},
+ hover_links::HoverLink,
};
#[derive(Clone, Debug)]
@@ -78,6 +76,7 @@ fn group_lenses_by_row(
}
fn render_code_lens_line(
+ buffer_id: BufferId,
line_number: usize,
lens: CodeLensLine,
editor: WeakEntity<Editor>,
@@ -104,11 +103,11 @@ fn render_code_lens_line(
let action = item.action.clone();
let editor_handle = editor.clone();
let position = lens.position;
- let id = (line_number as u64) << 32 | (i as u64);
+ let id = SharedString::from(format!("{buffer_id}:{line_number}:{i}"));
children.push(
div()
- .id(ElementId::Integer(id))
+ .id(ElementId::Name(id))
.font(font.clone())
.text_size(font_size)
.text_color(cx.app.theme().colors().text_muted)
@@ -206,7 +205,7 @@ pub(super) fn try_handle_client_command(
schedule_task(task_template, action, editor, workspace, window, cx)
}
Some(ClientCommand::ShowLocations) => {
- try_show_references(arguments, action, workspace, window, cx)
+ try_show_references(arguments, action, editor, window, cx)
}
None => false,
}
@@ -261,7 +260,7 @@ fn schedule_task(
fn try_show_references(
arguments: &[serde_json::Value],
action: &CodeAction,
- workspace: &gpui::Entity<workspace::Workspace>,
+ editor: &mut Editor,
window: &mut Window,
cx: &mut Context<Editor>,
) -> bool {
@@ -276,73 +275,18 @@ fn try_show_references(
}
let server_id = action.server_id;
- let project = workspace.read(cx).project().clone();
- let workspace = workspace.clone();
-
- cx.spawn_in(window, async move |_editor, cx| {
- let mut buffer_locations = std::collections::HashMap::default();
-
- for location in &locations {
- let open_task = cx.update(|_, cx| {
- project.update(cx, |project, cx| {
- let uri: lsp::Uri = location.uri.clone();
- project.open_local_buffer_via_lsp(uri, server_id, cx)
- })
- })?;
- let buffer = open_task.await?;
-
- let range = range_from_lsp(location.range);
- buffer_locations
- .entry(buffer)
- .or_insert_with(Vec::new)
- .push(range);
- }
-
- workspace.update_in(cx, |workspace, window, cx| {
- let target = buffer_locations
- .iter()
- .flat_map(|(k, v)| iter::repeat(k.clone()).zip(v))
- .map(|(buffer, location)| {
- buffer
- .read(cx)
- .text_for_range(location.clone())
- .collect::<String>()
- })
- .filter(|text| !text.contains('\n'))
- .unique()
- .take(3)
- .join(", ");
- let title = if target.is_empty() {
- "References".to_owned()
- } else {
- format!("References to {target}")
- };
- let allow_preview =
- PreviewTabsSettings::get_global(cx).enable_preview_multibuffer_from_code_navigation;
- Editor::open_locations_in_multibuffer(
- workspace,
- buffer_locations,
- title,
- false,
- allow_preview,
- MultibufferSelectionMode::First,
- window,
- cx,
- );
- })?;
- anyhow::Ok(())
- })
- .detach_and_log_err(cx);
+ let nav_entry = editor.navigation_entry(editor.selections.newest_anchor().head(), cx);
+ let links = locations
+ .into_iter()
+ .map(|location| HoverLink::InlayHint(location, server_id))
+ .collect();
+ editor
+ .navigate_to_hover_links(None, links, nav_entry, false, window, cx)
+ .detach_and_log_err(cx);
true
}
-fn range_from_lsp(range: lsp::Range) -> Range<Point> {
- let start = Point::new(range.start.line, range.start.character);
- let end = Point::new(range.end.line, range.end.character);
- start..end
-}
-
impl Editor {
pub(super) fn refresh_code_lenses(
&mut self,
@@ -469,6 +413,7 @@ impl Editor {
height: Some(1),
style: BlockStyle::Flex,
render: Arc::new(render_code_lens_line(
+ buffer_id,
line_number,
lens_line,
editor_handle.clone(),
@@ -609,6 +554,7 @@ impl Editor {
height: Some(1),
style: BlockStyle::Flex,
render: Arc::new(render_code_lens_line(
+ buffer_id,
line_number,
lens_line,
editor_handle.clone(),
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 302708c7864..2c70d4c1fc0 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -16007,7 +16007,7 @@ impl Editor {
}
}
- fn navigation_entry(
+ pub(crate) fn navigation_entry(
&self,
cursor_anchor: Anchor,
cx: &mut Context<Self>,