edit prediction: Context debug view (#38435)

Adds a `dev: open edit prediction context` action that opens a new
workspace pane that displays the excerpts and snippets that would be
included in the edit prediction request.

Release Notes:

- N/A

---------

Co-authored-by: Bennet <bennet@zed.dev>
This commit is contained in:
Agus Zubiaga
2025-09-18 15:09:44 -03:00
committed by GitHub
co-authored by Bennet
parent 55d130a166
commit df50b5c14a
15 changed files with 583 additions and 19 deletions
@@ -41,14 +41,14 @@ impl Declaration {
}
}
pub fn project_entry_id(&self) -> Option<ProjectEntryId> {
pub fn project_entry_id(&self) -> ProjectEntryId {
match self {
Declaration::File {
project_entry_id, ..
} => Some(*project_entry_id),
} => *project_entry_id,
Declaration::Buffer {
project_entry_id, ..
} => Some(*project_entry_id),
} => *project_entry_id,
}
}
@@ -119,15 +119,13 @@ pub fn scored_snippets(
)
})
} else {
// TODO should we prefer the current file instead?
Some((false, 0, declaration))
Some((false, u32::MAX, declaration))
}
}
Declaration::File { .. } => {
// TODO should we prefer the current file instead?
// We can assume that a file declaration is in a different file,
// because the current one must be open
Some((false, 0, declaration))
Some((false, u32::MAX, declaration))
}
})
.sorted_by_key(|&(_, distance, _)| distance)
@@ -6,8 +6,12 @@ mod reference;
mod syntax_index;
mod text_similarity;
use std::time::Instant;
pub use declaration::{BufferDeclaration, Declaration, FileDeclaration, Identifier};
pub use declaration_scoring::SnippetStyle;
pub use excerpt::{EditPredictionExcerpt, EditPredictionExcerptOptions, EditPredictionExcerptText};
use gpui::{App, AppContext as _, Entity, Task};
use language::BufferSnapshot;
pub use reference::references_in_excerpt;
@@ -16,10 +20,12 @@ use text::{Point, ToOffset as _};
use crate::declaration_scoring::{ScoredSnippet, scored_snippets};
#[derive(Debug)]
pub struct EditPredictionContext {
pub excerpt: EditPredictionExcerpt,
pub excerpt_text: EditPredictionExcerptText,
pub snippets: Vec<ScoredSnippet>,
pub retrieval_duration: std::time::Duration,
}
impl EditPredictionContext {
@@ -29,14 +35,14 @@ impl EditPredictionContext {
excerpt_options: EditPredictionExcerptOptions,
syntax_index: Entity<SyntaxIndex>,
cx: &mut App,
) -> Task<Self> {
) -> Task<Option<Self>> {
let start = Instant::now();
let index_state = syntax_index.read_with(cx, |index, _cx| index.state().clone());
cx.background_spawn(async move {
let index_state = index_state.lock().await;
let excerpt =
EditPredictionExcerpt::select_from_buffer(cursor_point, &buffer, &excerpt_options)
.unwrap();
EditPredictionExcerpt::select_from_buffer(cursor_point, &buffer, &excerpt_options)?;
let excerpt_text = excerpt.text(&buffer);
let references = references_in_excerpt(&excerpt, &excerpt_text, &buffer);
let cursor_offset = cursor_point.to_offset(&buffer);
@@ -50,11 +56,12 @@ impl EditPredictionContext {
&buffer,
);
Self {
Some(Self {
excerpt,
excerpt_text,
snippets,
}
retrieval_duration: start.elapsed(),
})
})
}
}
@@ -107,7 +114,8 @@ mod tests {
cx,
)
})
.await;
.await
.unwrap();
assert_eq!(context.snippets.len(), 1);
assert_eq!(context.snippets[0].identifier.name.as_ref(), "process_data");
@@ -38,7 +38,7 @@ pub struct EditPredictionExcerpt {
pub size: usize,
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct EditPredictionExcerptText {
pub body: String,
pub parent_signatures: Vec<String>,
@@ -89,7 +89,8 @@ pub fn identifiers_in_range(
}
let identifier_text =
&range_text[node_range.start - range.start..node_range.end - range.start];
// TODO we changed this to saturating_sub for now, but we should fix the actually issue
&range_text[node_range.start.saturating_sub(range.start)..node_range.end.saturating_sub(range.start)];
references.push(Reference {
identifier: Identifier {
name: identifier_text.into(),
@@ -9,7 +9,7 @@ use project::worktree_store::{WorktreeStore, WorktreeStoreEvent};
use project::{PathChange, Project, ProjectEntryId, ProjectPath};
use slotmap::SlotMap;
use text::BufferId;
use util::{ResultExt as _, debug_panic, some_or_debug_panic};
use util::{debug_panic, some_or_debug_panic};
use crate::declaration::{
BufferDeclaration, Declaration, DeclarationId, FileDeclaration, Identifier,
@@ -332,7 +332,7 @@ impl SyntaxIndex {
let language = language_registry
.language_for_file_path(&project_path.path)
.await
.log_err();
.ok();
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local(loaded_file.text, cx);