rho-coding-agent 3.9.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The history cache's side of transcript path links: which workspace it
//! links into, handing queued mentions to a check and its result back, and
//! the links on the rows a frame shows.

use std::{collections::HashSet, path::Path};

use super::{
    CachedEntry, EntryImageResolver, HistoryLineCache, HistoryLineSlice, HistoryRenderSettings,
};
use crate::tui::{
    path_links::{CheckedPaths, PathCheck, PathIndex, PathLinker},
    text_links::RowLink,
    Entry,
};

/// The linker an entry renders with: linking into `paths`, with icons when
/// `settings` show them, or off without `paths`.
pub(super) fn entry_linker(
    paths: Option<&PathIndex>,
    settings: HistoryRenderSettings,
) -> PathLinker<'_> {
    match paths {
        Some(paths) if settings.path_icons => PathLinker::new(paths).with_icons(),
        Some(paths) => PathLinker::new(paths),
        None => PathLinker::off(),
    }
}

impl CachedEntry {
    /// Whether rendering linked paths in this entry, or queued some for a
    /// check: then its rows cannot be extended in place.
    pub(super) fn rendered_linked(&self) -> bool {
        !self.links.is_empty() || !self.unlinked_paths.is_empty()
    }
}

impl HistoryLineCache {
    /// Links paths into `workspace` from now on. Moving to another
    /// workspace forgets what was known and re-renders every measured entry.
    pub(in crate::tui) fn set_path_workspace(&mut self, workspace: &Path) {
        if self
            .paths
            .as_ref()
            .is_some_and(|paths| paths.workspace() == workspace)
        {
            return;
        }
        self.paths = Some(PathIndex::new(workspace.to_path_buf()));
        self.invalidate_from(self.measured_from);
    }

    /// The mentions rendering queued since the last call, as one check.
    pub(in crate::tui) fn take_path_check(&mut self) -> Option<PathCheck> {
        self.paths.as_mut()?.take_check()
    }

    /// Records `checked` and re-renders the entries that mention a file it found.
    pub(in crate::tui) fn apply_path_check(&mut self, checked: CheckedPaths) {
        let Some(paths) = self.paths.as_mut() else {
            return;
        };
        let found: HashSet<String> = paths.apply(checked).into_iter().collect();
        if found.is_empty() {
            return;
        }
        let mentioning: Vec<usize> = self
            .entries
            .iter()
            .enumerate()
            .filter(|(_, entry)| entry.unlinked_paths.iter().any(|path| found.contains(path)))
            .map(|(index, _)| self.measured_from + index)
            .collect();
        self.resplice_entries(mentioning);
    }

    /// Checks again the paths found missing: the turn that just ended may
    /// have written them.
    pub(in crate::tui) fn recheck_missing_paths(&mut self) {
        if let Some(paths) = self.paths.as_mut() {
            paths.recheck_missing();
        }
    }

    /// The links on transcript rows `slice`, by row from `slice.start`.
    pub(in crate::tui) fn visible_links(
        &mut self,
        entries: &[Entry],
        settings: HistoryRenderSettings,
        slice: HistoryLineSlice,
        image_resolver: EntryImageResolver<'_>,
    ) -> Vec<RowLink> {
        self.ensure_current(entries, settings, image_resolver);
        let end = slice.start.saturating_add(slice.count);
        let first = self
            .entry_ranges
            .partition_point(|range| range.end <= slice.start);
        let mut visible = Vec::new();
        for (entry, range) in self.entries[first..]
            .iter()
            .zip(&self.entry_ranges[first..])
        {
            if range.start >= end {
                break;
            }
            for link in &entry.links {
                let row = range.start + link.row;
                if (slice.start..end).contains(&row) {
                    visible.push(RowLink {
                        row: row - slice.start,
                        columns: link.columns.clone(),
                        url: link.url.clone(),
                    });
                }
            }
        }
        visible
    }
}