edit_prediction_context.rs

  1use crate::assemble_excerpts::assemble_excerpt_ranges;
  2use anyhow::Result;
  3use collections::HashMap;
  4use futures::{FutureExt, StreamExt as _, channel::mpsc, future};
  5use gpui::{App, AppContext, AsyncApp, Context, Entity, EntityId, EventEmitter, Task, WeakEntity};
  6use language::{Anchor, Buffer, BufferSnapshot, OffsetRangeExt as _, Point, ToOffset as _};
  7use project::{LocationLink, Project, ProjectPath};
  8use smallvec::SmallVec;
  9use std::{
 10    collections::hash_map,
 11    ops::Range,
 12    path::Path,
 13    sync::Arc,
 14    time::{Duration, Instant},
 15};
 16use util::paths::PathStyle;
 17use util::rel_path::RelPath;
 18use util::{RangeExt as _, ResultExt};
 19
 20mod assemble_excerpts;
 21#[cfg(test)]
 22mod edit_prediction_context_tests;
 23#[cfg(test)]
 24mod fake_definition_lsp;
 25
 26pub use zeta_prompt::{RelatedExcerpt, RelatedFile};
 27
 28const IDENTIFIER_LINE_COUNT: u32 = 3;
 29
 30pub struct RelatedExcerptStore {
 31    project: WeakEntity<Project>,
 32    related_buffers: Vec<RelatedBuffer>,
 33    cache: HashMap<Identifier, Arc<CacheEntry>>,
 34    update_tx: mpsc::UnboundedSender<(Entity<Buffer>, Anchor)>,
 35    identifier_line_count: u32,
 36}
 37
 38struct RelatedBuffer {
 39    buffer: Entity<Buffer>,
 40    path: Arc<Path>,
 41    anchor_ranges: Vec<Range<Anchor>>,
 42    cached_file: Option<CachedRelatedFile>,
 43}
 44
 45struct CachedRelatedFile {
 46    excerpts: Vec<RelatedExcerpt>,
 47    buffer_version: clock::Global,
 48}
 49
 50pub enum RelatedExcerptStoreEvent {
 51    StartedRefresh,
 52    FinishedRefresh {
 53        cache_hit_count: usize,
 54        cache_miss_count: usize,
 55        mean_definition_latency: Duration,
 56        max_definition_latency: Duration,
 57    },
 58}
 59
 60#[derive(Clone, Debug, PartialEq, Eq, Hash)]
 61struct Identifier {
 62    pub name: String,
 63    pub range: Range<Anchor>,
 64}
 65
 66enum DefinitionTask {
 67    CacheHit(Arc<CacheEntry>),
 68    CacheMiss(Task<Result<Option<Vec<LocationLink>>>>),
 69}
 70
 71#[derive(Debug)]
 72struct CacheEntry {
 73    definitions: SmallVec<[CachedDefinition; 1]>,
 74}
 75
 76#[derive(Clone, Debug)]
 77struct CachedDefinition {
 78    path: ProjectPath,
 79    buffer: Entity<Buffer>,
 80    anchor_range: Range<Anchor>,
 81}
 82
 83const DEBOUNCE_DURATION: Duration = Duration::from_millis(100);
 84
 85impl EventEmitter<RelatedExcerptStoreEvent> for RelatedExcerptStore {}
 86
 87impl RelatedExcerptStore {
 88    pub fn new(project: &Entity<Project>, cx: &mut Context<Self>) -> Self {
 89        let (update_tx, mut update_rx) = mpsc::unbounded::<(Entity<Buffer>, Anchor)>();
 90        cx.spawn(async move |this, cx| {
 91            let executor = cx.background_executor().clone();
 92            while let Some((mut buffer, mut position)) = update_rx.next().await {
 93                let mut timer = executor.timer(DEBOUNCE_DURATION).fuse();
 94                loop {
 95                    futures::select_biased! {
 96                        next = update_rx.next() => {
 97                            if let Some((new_buffer, new_position)) = next {
 98                                buffer = new_buffer;
 99                                position = new_position;
100                                timer = executor.timer(DEBOUNCE_DURATION).fuse();
101                            } else {
102                                return anyhow::Ok(());
103                            }
104                        }
105                        _ = timer => break,
106                    }
107                }
108
109                Self::fetch_excerpts(this.clone(), buffer, position, cx).await?;
110            }
111            anyhow::Ok(())
112        })
113        .detach_and_log_err(cx);
114
115        RelatedExcerptStore {
116            project: project.downgrade(),
117            update_tx,
118            related_buffers: Vec::new(),
119            cache: Default::default(),
120            identifier_line_count: IDENTIFIER_LINE_COUNT,
121        }
122    }
123
124    pub fn set_identifier_line_count(&mut self, count: u32) {
125        self.identifier_line_count = count;
126    }
127
128    pub fn refresh(&mut self, buffer: Entity<Buffer>, position: Anchor, _: &mut Context<Self>) {
129        self.update_tx.unbounded_send((buffer, position)).ok();
130    }
131
132    pub fn related_files(&mut self, cx: &App) -> Vec<RelatedFile> {
133        self.related_buffers
134            .iter_mut()
135            .map(|related| related.related_file(cx))
136            .collect()
137    }
138
139    pub fn related_files_with_buffers(
140        &mut self,
141        cx: &App,
142    ) -> impl Iterator<Item = (RelatedFile, Entity<Buffer>)> {
143        self.related_buffers
144            .iter_mut()
145            .map(|related| (related.related_file(cx), related.buffer.clone()))
146    }
147
148    pub fn set_related_files(&mut self, files: Vec<RelatedFile>, cx: &App) {
149        self.related_buffers = files
150            .into_iter()
151            .filter_map(|file| {
152                let project = self.project.upgrade()?;
153                let project = project.read(cx);
154                let worktree = project.worktrees(cx).find(|wt| {
155                    let root_name = wt.read(cx).root_name().as_unix_str();
156                    file.path
157                        .components()
158                        .next()
159                        .is_some_and(|c| c.as_os_str() == root_name)
160                })?;
161                let worktree = worktree.read(cx);
162                let relative_path = file
163                    .path
164                    .strip_prefix(worktree.root_name().as_unix_str())
165                    .ok()?;
166                let relative_path = RelPath::new(relative_path, PathStyle::Posix).ok()?;
167                let project_path = ProjectPath {
168                    worktree_id: worktree.id(),
169                    path: relative_path.into_owned().into(),
170                };
171                let buffer = project.get_open_buffer(&project_path, cx)?;
172                let snapshot = buffer.read(cx).snapshot();
173                let anchor_ranges = file
174                    .excerpts
175                    .iter()
176                    .map(|excerpt| {
177                        let start = snapshot.anchor_before(Point::new(excerpt.row_range.start, 0));
178                        let end_row = excerpt.row_range.end;
179                        let end_col = snapshot.line_len(end_row);
180                        let end = snapshot.anchor_after(Point::new(end_row, end_col));
181                        start..end
182                    })
183                    .collect();
184                Some(RelatedBuffer {
185                    buffer,
186                    path: file.path.clone(),
187                    anchor_ranges,
188                    cached_file: None,
189                })
190            })
191            .collect();
192    }
193
194    async fn fetch_excerpts(
195        this: WeakEntity<Self>,
196        buffer: Entity<Buffer>,
197        position: Anchor,
198        cx: &mut AsyncApp,
199    ) -> Result<()> {
200        let (project, snapshot, identifier_line_count) = this.read_with(cx, |this, cx| {
201            (
202                this.project.upgrade(),
203                buffer.read(cx).snapshot(),
204                this.identifier_line_count,
205            )
206        })?;
207        let Some(project) = project else {
208            return Ok(());
209        };
210
211        let file = snapshot.file().cloned();
212        if let Some(file) = &file {
213            log::debug!("retrieving_context buffer:{}", file.path().as_unix_str());
214        }
215
216        this.update(cx, |_, cx| {
217            cx.emit(RelatedExcerptStoreEvent::StartedRefresh);
218        })?;
219
220        let identifiers = cx
221            .background_spawn(async move {
222                identifiers_for_position(&snapshot, position, identifier_line_count)
223            })
224            .await;
225
226        let async_cx = cx.clone();
227        let start_time = Instant::now();
228        let futures = this.update(cx, |this, cx| {
229            identifiers
230                .into_iter()
231                .filter_map(|identifier| {
232                    let task = if let Some(entry) = this.cache.get(&identifier) {
233                        DefinitionTask::CacheHit(entry.clone())
234                    } else {
235                        DefinitionTask::CacheMiss(
236                            this.project
237                                .update(cx, |project, cx| {
238                                    project.definitions(&buffer, identifier.range.start, cx)
239                                })
240                                .ok()?,
241                        )
242                    };
243
244                    let cx = async_cx.clone();
245                    let project = project.clone();
246                    Some(async move {
247                        match task {
248                            DefinitionTask::CacheHit(cache_entry) => {
249                                Some((identifier, cache_entry, None))
250                            }
251                            DefinitionTask::CacheMiss(task) => {
252                                let locations = task.await.log_err()??;
253                                let duration = start_time.elapsed();
254                                Some(cx.update(|cx| {
255                                    (
256                                        identifier,
257                                        Arc::new(CacheEntry {
258                                            definitions: locations
259                                                .into_iter()
260                                                .filter_map(|location| {
261                                                    process_definition(location, &project, cx)
262                                                })
263                                                .collect(),
264                                        }),
265                                        Some(duration),
266                                    )
267                                }))
268                            }
269                        }
270                    })
271                })
272                .collect::<Vec<_>>()
273        })?;
274
275        let mut cache_hit_count = 0;
276        let mut cache_miss_count = 0;
277        let mut mean_definition_latency = Duration::ZERO;
278        let mut max_definition_latency = Duration::ZERO;
279        let mut new_cache = HashMap::default();
280        new_cache.reserve(futures.len());
281        for (identifier, entry, duration) in future::join_all(futures).await.into_iter().flatten() {
282            new_cache.insert(identifier, entry);
283            if let Some(duration) = duration {
284                cache_miss_count += 1;
285                mean_definition_latency += duration;
286                max_definition_latency = max_definition_latency.max(duration);
287            } else {
288                cache_hit_count += 1;
289            }
290        }
291        mean_definition_latency /= cache_miss_count.max(1) as u32;
292
293        let (new_cache, related_buffers) = rebuild_related_files(&project, new_cache, cx).await?;
294
295        if let Some(file) = &file {
296            log::debug!(
297                "finished retrieving context buffer:{}, latency:{:?}",
298                file.path().as_unix_str(),
299                start_time.elapsed()
300            );
301        }
302
303        this.update(cx, |this, cx| {
304            this.cache = new_cache;
305            this.related_buffers = related_buffers;
306            cx.emit(RelatedExcerptStoreEvent::FinishedRefresh {
307                cache_hit_count,
308                cache_miss_count,
309                mean_definition_latency,
310                max_definition_latency,
311            });
312        })?;
313
314        anyhow::Ok(())
315    }
316}
317
318async fn rebuild_related_files(
319    project: &Entity<Project>,
320    mut new_entries: HashMap<Identifier, Arc<CacheEntry>>,
321    cx: &mut AsyncApp,
322) -> Result<(HashMap<Identifier, Arc<CacheEntry>>, Vec<RelatedBuffer>)> {
323    let mut snapshots = HashMap::default();
324    let mut worktree_root_names = HashMap::default();
325    for entry in new_entries.values() {
326        for definition in &entry.definitions {
327            if let hash_map::Entry::Vacant(e) = snapshots.entry(definition.buffer.entity_id()) {
328                definition
329                    .buffer
330                    .read_with(cx, |buffer, _| buffer.parsing_idle())
331                    .await;
332                e.insert(
333                    definition
334                        .buffer
335                        .read_with(cx, |buffer, _| buffer.snapshot()),
336                );
337            }
338            let worktree_id = definition.path.worktree_id;
339            if let hash_map::Entry::Vacant(e) =
340                worktree_root_names.entry(definition.path.worktree_id)
341            {
342                project.read_with(cx, |project, cx| {
343                    if let Some(worktree) = project.worktree_for_id(worktree_id, cx) {
344                        e.insert(worktree.read(cx).root_name().as_unix_str().to_string());
345                    }
346                });
347            }
348        }
349    }
350
351    Ok(cx
352        .background_spawn(async move {
353            let mut ranges_by_buffer =
354                HashMap::<EntityId, (Entity<Buffer>, Vec<Range<Point>>)>::default();
355            let mut paths_by_buffer = HashMap::default();
356            for entry in new_entries.values_mut() {
357                for definition in &entry.definitions {
358                    let Some(snapshot) = snapshots.get(&definition.buffer.entity_id()) else {
359                        continue;
360                    };
361                    paths_by_buffer.insert(definition.buffer.entity_id(), definition.path.clone());
362
363                    ranges_by_buffer
364                        .entry(definition.buffer.entity_id())
365                        .or_insert_with(|| (definition.buffer.clone(), Vec::new()))
366                        .1
367                        .push(definition.anchor_range.to_point(snapshot));
368                }
369            }
370
371            let mut related_buffers: Vec<RelatedBuffer> = ranges_by_buffer
372                .into_iter()
373                .filter_map(|(entity_id, (buffer, ranges))| {
374                    let snapshot = snapshots.get(&entity_id)?;
375                    let project_path = paths_by_buffer.get(&entity_id)?;
376                    let row_ranges = assemble_excerpt_ranges(snapshot, ranges);
377                    let root_name = worktree_root_names.get(&project_path.worktree_id)?;
378
379                    let path: Arc<Path> = Path::new(&format!(
380                        "{}/{}",
381                        root_name,
382                        project_path.path.as_unix_str()
383                    ))
384                    .into();
385
386                    let anchor_ranges = row_ranges
387                        .into_iter()
388                        .map(|row_range| {
389                            let start = snapshot.anchor_before(Point::new(row_range.start, 0));
390                            let end_col = snapshot.line_len(row_range.end);
391                            let end = snapshot.anchor_after(Point::new(row_range.end, end_col));
392                            start..end
393                        })
394                        .collect();
395
396                    let mut related_buffer = RelatedBuffer {
397                        buffer,
398                        path,
399                        anchor_ranges,
400                        cached_file: None,
401                    };
402                    related_buffer.fill_cache(snapshot);
403                    Some(related_buffer)
404                })
405                .collect();
406
407            related_buffers.sort_by_key(|related| related.path.clone());
408
409            (new_entries, related_buffers)
410        })
411        .await)
412}
413
414impl RelatedBuffer {
415    fn related_file(&mut self, cx: &App) -> RelatedFile {
416        let buffer = self.buffer.read(cx);
417        let path = self.path.clone();
418        let cached = if let Some(cached) = &self.cached_file
419            && buffer.version() == cached.buffer_version
420        {
421            cached
422        } else {
423            self.fill_cache(buffer)
424        };
425        let related_file = RelatedFile {
426            path,
427            excerpts: cached.excerpts.clone(),
428            max_row: buffer.max_point().row,
429            in_open_source_repo: false,
430        };
431        return related_file;
432    }
433
434    fn fill_cache(&mut self, buffer: &text::BufferSnapshot) -> &CachedRelatedFile {
435        let excerpts = self
436            .anchor_ranges
437            .iter()
438            .map(|range| {
439                let start = range.start.to_point(buffer);
440                let end = range.end.to_point(buffer);
441                RelatedExcerpt {
442                    row_range: start.row..end.row,
443                    text: buffer.text_for_range(start..end).collect::<String>().into(),
444                }
445            })
446            .collect::<Vec<_>>();
447        self.cached_file = Some(CachedRelatedFile {
448            excerpts: excerpts,
449            buffer_version: buffer.version().clone(),
450        });
451        self.cached_file.as_ref().unwrap()
452    }
453}
454
455use language::ToPoint as _;
456
457const MAX_TARGET_LEN: usize = 128;
458
459fn process_definition(
460    location: LocationLink,
461    project: &Entity<Project>,
462    cx: &mut App,
463) -> Option<CachedDefinition> {
464    let buffer = location.target.buffer.read(cx);
465    let anchor_range = location.target.range;
466    let file = buffer.file()?;
467    let worktree = project.read(cx).worktree_for_id(file.worktree_id(cx), cx)?;
468    if worktree.read(cx).is_single_file() {
469        return None;
470    }
471
472    // If the target range is large, it likely means we requested the definition of an entire module.
473    // For individual definitions, the target range should be small as it only covers the symbol.
474    let buffer = location.target.buffer.read(cx);
475    let target_len = anchor_range.to_offset(&buffer).len();
476    if target_len > MAX_TARGET_LEN {
477        return None;
478    }
479
480    Some(CachedDefinition {
481        path: ProjectPath {
482            worktree_id: file.worktree_id(cx),
483            path: file.path().clone(),
484        },
485        buffer: location.target.buffer,
486        anchor_range,
487    })
488}
489
490/// Gets all of the identifiers that are present in the given line, and its containing
491/// outline items.
492fn identifiers_for_position(
493    buffer: &BufferSnapshot,
494    position: Anchor,
495    identifier_line_count: u32,
496) -> Vec<Identifier> {
497    let offset = position.to_offset(buffer);
498    let point = buffer.offset_to_point(offset);
499
500    // Search for identifiers on lines adjacent to the cursor.
501    let start = Point::new(point.row.saturating_sub(identifier_line_count), 0);
502    let end = Point::new(point.row + identifier_line_count + 1, 0).min(buffer.max_point());
503    let line_range = start..end;
504    let mut ranges = vec![line_range.to_offset(&buffer)];
505
506    // Search for identifiers mentioned in headers/signatures of containing outline items.
507    let outline_items = buffer.outline_items_as_offsets_containing(offset..offset, false, None);
508    for item in outline_items {
509        if let Some(body_range) = item.body_range(&buffer) {
510            ranges.push(item.range.start..body_range.start.to_offset(&buffer));
511        } else {
512            ranges.push(item.range.clone());
513        }
514    }
515
516    ranges.sort_by(|a, b| a.start.cmp(&b.start).then(b.end.cmp(&a.end)));
517    ranges.dedup_by(|a, b| {
518        if a.start <= b.end {
519            b.start = b.start.min(a.start);
520            b.end = b.end.max(a.end);
521            true
522        } else {
523            false
524        }
525    });
526
527    let mut identifiers = Vec::new();
528    let outer_range =
529        ranges.first().map_or(0, |r| r.start)..ranges.last().map_or(buffer.len(), |r| r.end);
530
531    let mut captures = buffer
532        .syntax
533        .captures(outer_range.clone(), &buffer.text, |grammar| {
534            grammar
535                .highlights_config
536                .as_ref()
537                .map(|config| &config.query)
538        });
539
540    for range in ranges {
541        captures.set_byte_range(range.start..outer_range.end);
542
543        let mut last_range = None;
544        while let Some(capture) = captures.peek() {
545            let node_range = capture.node.byte_range();
546            if node_range.start > range.end {
547                break;
548            }
549            let config = captures.grammars()[capture.grammar_index]
550                .highlights_config
551                .as_ref();
552
553            if let Some(config) = config
554                && config.identifier_capture_indices.contains(&capture.index)
555                && range.contains_inclusive(&node_range)
556                && Some(&node_range) != last_range.as_ref()
557            {
558                let name = buffer.text_for_range(node_range.clone()).collect();
559                identifiers.push(Identifier {
560                    range: buffer.anchor_after(node_range.start)
561                        ..buffer.anchor_before(node_range.end),
562                    name,
563                });
564                last_range = Some(node_range);
565            }
566
567            captures.advance();
568        }
569    }
570
571    identifiers
572}