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

@ -1,12 +1,6 @@
pub use crate::{
Grammar, Language, LanguageRegistry,
diagnostic_set::DiagnosticSet,
highlight_map::{HighlightId, HighlightMap},
proto,
};
use crate::{
LanguageScope, Outline, OutlineConfig, RunnableCapture, RunnableTag, TextObject,
TreeSitterOptions,
DebugVariableCapture, LanguageScope, Outline, OutlineConfig, RunnableCapture, RunnableTag,
TextObject, TreeSitterOptions,
diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
language_settings::{LanguageSettings, language_settings},
outline::OutlineItem,
@ -17,6 +11,12 @@ use crate::{
task_context::RunnableRange,
text_diff::text_diff,
};
pub use crate::{
Grammar, Language, LanguageRegistry,
diagnostic_set::DiagnosticSet,
highlight_map::{HighlightId, HighlightMap},
proto,
};
use anyhow::{Context as _, Result, anyhow};
use async_watch as watch;
use clock::Lamport;
@ -73,6 +73,12 @@ pub use {tree_sitter_rust, tree_sitter_typescript};
pub use lsp::DiagnosticSeverity;
#[derive(Debug)]
pub struct DebugVariableRanges {
pub buffer_id: BufferId,
pub range: Range<usize>,
}
/// A label for the background task spawned by the buffer to compute
/// a diff against the contents of its file.
pub static BUFFER_DIFF_TASK: LazyLock<TaskLabel> = LazyLock::new(TaskLabel::new);
@ -3888,6 +3894,79 @@ impl BufferSnapshot {
})
}
pub fn debug_variable_ranges(
&self,
offset_range: Range<usize>,
) -> impl Iterator<Item = DebugVariableRanges> + '_ {
let mut syntax_matches = self.syntax.matches(offset_range, self, |grammar| {
grammar
.debug_variables_config
.as_ref()
.map(|config| &config.query)
});
let configs = syntax_matches
.grammars()
.iter()
.map(|grammar| grammar.debug_variables_config.as_ref())
.collect::<Vec<_>>();
iter::from_fn(move || {
loop {
let mat = syntax_matches.peek()?;
let variable_ranges = configs[mat.grammar_index].and_then(|config| {
let full_range = mat.captures.iter().fold(
Range {
start: usize::MAX,
end: 0,
},
|mut acc, next| {
let byte_range = next.node.byte_range();
if acc.start > byte_range.start {
acc.start = byte_range.start;
}
if acc.end < byte_range.end {
acc.end = byte_range.end;
}
acc
},
);
if full_range.start > full_range.end {
// We did not find a full spanning range of this match.
return None;
}
let captures = mat.captures.iter().filter_map(|capture| {
Some((
capture,
config.captures.get(capture.index as usize).cloned()?,
))
});
let mut variable_range = None;
for (query, capture) in captures {
if let DebugVariableCapture::Variable = capture {
let _ = variable_range.insert(query.node.byte_range());
}
}
Some(DebugVariableRanges {
buffer_id: self.remote_id(),
range: variable_range?,
})
});
syntax_matches.advance();
if variable_ranges.is_some() {
// It's fine for us to short-circuit on .peek()? returning None. We don't want to return None from this iter if we
// had a capture that did not contain a run marker, hence we'll just loop around for the next capture.
return variable_ranges;
}
}
})
}
pub fn runnable_ranges(
&self,
offset_range: Range<usize>,