edit prediction: Initial implementation of Tree-sitter index (not yet used) (#38301)

Release Notes:

- N/A

---------

Co-authored-by: Agus <agus@zed.dev>
Co-authored-by: oleksiy <oleksiy@zed.dev>
This commit is contained in:
Michael Sloan
2025-09-17 07:25:14 +00:00
committed by GitHub
co-authored by Agus oleksiy
parent 5d561aa494
commit 64d362cbce
13 changed files with 1186 additions and 18 deletions
+9 -3
View File
@@ -145,7 +145,7 @@ struct BufferBranchState {
/// state of a buffer.
pub struct BufferSnapshot {
pub text: text::BufferSnapshot,
pub(crate) syntax: SyntaxSnapshot,
pub syntax: SyntaxSnapshot,
file: Option<Arc<dyn File>>,
diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
remote_selections: TreeMap<ReplicaId, SelectionSet>,
@@ -660,7 +660,10 @@ impl HighlightedTextBuilder {
syntax_snapshot: &'a SyntaxSnapshot,
) -> BufferChunks<'a> {
let captures = syntax_snapshot.captures(range.clone(), snapshot, |grammar| {
grammar.highlights_query.as_ref()
grammar
.highlights_config
.as_ref()
.map(|config| &config.query)
});
let highlight_maps = captures
@@ -3246,7 +3249,10 @@ impl BufferSnapshot {
fn get_highlights(&self, range: Range<usize>) -> (SyntaxMapCaptures<'_>, Vec<HighlightMap>) {
let captures = self.syntax.captures(range, &self.text, |grammar| {
grammar.highlights_query.as_ref()
grammar
.highlights_config
.as_ref()
.map(|config| &config.query)
});
let highlight_maps = captures
.grammars()
+41 -8
View File
@@ -81,7 +81,9 @@ pub use language_registry::{
};
pub use lsp::{LanguageServerId, LanguageServerName};
pub use outline::*;
pub use syntax_map::{OwnedSyntaxLayer, SyntaxLayer, ToTreeSitterPoint, TreeSitterOptions};
pub use syntax_map::{
OwnedSyntaxLayer, SyntaxLayer, SyntaxMapMatches, ToTreeSitterPoint, TreeSitterOptions,
};
pub use text::{AnchorRangeExt, LineEnding};
pub use tree_sitter::{Node, Parser, Tree, TreeCursor};
@@ -1154,7 +1156,7 @@ pub struct Grammar {
id: GrammarId,
pub ts_language: tree_sitter::Language,
pub(crate) error_query: Option<Query>,
pub(crate) highlights_query: Option<Query>,
pub highlights_config: Option<HighlightsConfig>,
pub(crate) brackets_config: Option<BracketsConfig>,
pub(crate) redactions_config: Option<RedactionConfig>,
pub(crate) runnable_config: Option<RunnableConfig>,
@@ -1168,6 +1170,11 @@ pub struct Grammar {
pub(crate) highlight_map: Mutex<HighlightMap>,
}
pub struct HighlightsConfig {
pub query: Query,
pub identifier_capture_indices: Vec<u32>,
}
struct IndentConfig {
query: Query,
indent_capture_ix: u32,
@@ -1332,7 +1339,7 @@ impl Language {
grammar: ts_language.map(|ts_language| {
Arc::new(Grammar {
id: GrammarId::new(),
highlights_query: None,
highlights_config: None,
brackets_config: None,
outline_config: None,
text_object_config: None,
@@ -1430,7 +1437,29 @@ impl Language {
pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
let grammar = self.grammar_mut()?;
grammar.highlights_query = Some(Query::new(&grammar.ts_language, source)?);
let query = Query::new(&grammar.ts_language, source)?;
let mut identifier_capture_indices = Vec::new();
for name in [
"variable",
"constant",
"constructor",
"function",
"function.method",
"function.method.call",
"function.special",
"property",
"type",
"type.interface",
] {
identifier_capture_indices.extend(query.capture_index_for_name(name));
}
grammar.highlights_config = Some(HighlightsConfig {
query,
identifier_capture_indices,
});
Ok(self)
}
@@ -1856,7 +1885,10 @@ impl Language {
let tree = grammar.parse_text(text, None);
let captures =
SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
grammar.highlights_query.as_ref()
grammar
.highlights_config
.as_ref()
.map(|config| &config.query)
});
let highlight_maps = vec![grammar.highlight_map()];
let mut offset = 0;
@@ -1885,10 +1917,10 @@ impl Language {
pub fn set_theme(&self, theme: &SyntaxTheme) {
if let Some(grammar) = self.grammar.as_ref()
&& let Some(highlights_query) = &grammar.highlights_query
&& let Some(highlights_config) = &grammar.highlights_config
{
*grammar.highlight_map.lock() =
HighlightMap::new(highlights_query.capture_names(), theme);
HighlightMap::new(highlights_config.query.capture_names(), theme);
}
}
@@ -2103,8 +2135,9 @@ impl Grammar {
pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
let capture_id = self
.highlights_query
.highlights_config
.as_ref()?
.query
.capture_index_for_name(name)?;
Some(self.highlight_map.lock().get(capture_id))
}
@@ -1409,12 +1409,15 @@ fn assert_capture_ranges(
) {
let mut actual_ranges = Vec::<Range<usize>>::new();
let captures = syntax_map.captures(0..buffer.len(), buffer, |grammar| {
grammar.highlights_query.as_ref()
grammar
.highlights_config
.as_ref()
.map(|config| &config.query)
});
let queries = captures
.grammars()
.iter()
.map(|grammar| grammar.highlights_query.as_ref().unwrap())
.map(|grammar| &grammar.highlights_config.as_ref().unwrap().query)
.collect::<Vec<_>>();
for capture in captures {
let name = &queries[capture.grammar_index].capture_names()[capture.index as usize];