debugger: Add support for inline value hints (#28656)

This PR uses Tree Sitter to show inline values while a user is in a
debug session.

We went with Tree Sitter over the LSP Inline Values request because the
LSP request isn't widely supported. Tree Sitter is easy for
languages/extensions to add support to. Tree Sitter can compute the
inline values locally, so there's no need to add extra RPC messages for
Collab. Tree Sitter also gives Zed more control over how we want to show
variables.

There's still more work to be done after this PR, namely differentiating
between global/local scoped variables, but it's a great starting point
to start iteratively improving it.

Release Notes:

- N/A

---------

Co-authored-by: Piotr Osiewicz <peterosiewicz@gmail.com>
Co-authored-by: Anthony Eid <hello@anthonyeid.me>
Co-authored-by: Cole Miller <m@cole-miller.net>
Co-authored-by: Anthony <anthony@zed.dev>
Co-authored-by: Kirill <kirill@zed.dev>
This commit is contained in:
Remco Smits 2025-04-24 00:27:27 +02:00 committed by GitHub
parent d095bab8ad
commit 218496744c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 709 additions and 54 deletions

View file

@ -41,13 +41,14 @@ use client::{
};
use clock::ReplicaId;
use dap::client::DebugAdapterClient;
use dap::{DapRegistry, client::DebugAdapterClient};
use collections::{BTreeSet, HashMap, HashSet};
use debounced_delay::DebouncedDelay;
use debugger::{
breakpoint_store::BreakpointStore,
breakpoint_store::{ActiveStackFrame, BreakpointStore},
dap_store::{DapStore, DapStoreEvent},
session::Session,
};
pub use environment::ProjectEnvironment;
#[cfg(test)]
@ -63,7 +64,7 @@ use image_store::{ImageItemEvent, ImageStoreEvent};
use ::git::{blame::Blame, status::FileStatus};
use gpui::{
AnyEntity, App, AppContext, AsyncApp, BorrowAppContext, Context, Entity, EventEmitter, Hsla,
SharedString, Task, WeakEntity, Window,
SharedString, Task, WeakEntity, Window, prelude::FluentBuilder,
};
use itertools::Itertools;
use language::{
@ -1551,6 +1552,15 @@ impl Project {
self.breakpoint_store.clone()
}
pub fn active_debug_session(&self, cx: &App) -> Option<(Entity<Session>, ActiveStackFrame)> {
let active_position = self.breakpoint_store.read(cx).active_position()?;
let session = self
.dap_store
.read(cx)
.session_by_id(active_position.session_id)?;
Some((session, active_position.clone()))
}
pub fn lsp_store(&self) -> Entity<LspStore> {
self.lsp_store.clone()
}
@ -3484,6 +3494,69 @@ impl Project {
})
}
pub fn inline_values(
&mut self,
session: Entity<Session>,
active_stack_frame: ActiveStackFrame,
buffer_handle: Entity<Buffer>,
range: Range<text::Anchor>,
cx: &mut Context<Self>,
) -> Task<anyhow::Result<Vec<InlayHint>>> {
let snapshot = buffer_handle.read(cx).snapshot();
let Some(inline_value_provider) = session
.read(cx)
.adapter_name()
.map(|adapter_name| DapRegistry::global(cx).adapter(&adapter_name))
.and_then(|adapter| adapter.inline_value_provider())
else {
return Task::ready(Err(anyhow::anyhow!("Inline value provider not found")));
};
let mut text_objects =
snapshot.text_object_ranges(range.end..range.end, Default::default());
let text_object_range = text_objects
.find(|(_, obj)| matches!(obj, language::TextObject::AroundFunction))
.map(|(range, _)| snapshot.anchor_before(range.start))
.unwrap_or(range.start);
let variable_ranges = snapshot
.debug_variable_ranges(
text_object_range.to_offset(&snapshot)..range.end.to_offset(&snapshot),
)
.filter_map(|range| {
let lsp_range = language::range_to_lsp(
range.range.start.to_point_utf16(&snapshot)
..range.range.end.to_point_utf16(&snapshot),
)
.ok()?;
Some((
snapshot.text_for_range(range.range).collect::<String>(),
lsp_range,
))
})
.collect::<Vec<_>>();
let inline_values = inline_value_provider.provide(variable_ranges);
let stack_frame_id = active_stack_frame.stack_frame_id;
cx.spawn(async move |this, cx| {
this.update(cx, |project, cx| {
project.dap_store().update(cx, |dap_store, cx| {
dap_store.resolve_inline_values(
session,
stack_frame_id,
buffer_handle,
inline_values,
cx,
)
})
})?
.await
})
}
pub fn inlay_hints<T: ToOffset>(
&mut self,
buffer_handle: Entity<Buffer>,