Files
codex/codex-rs/tui/src/history_cell/markdown_render_cache.rs
T
Felipe Coury 3a51865c8c Open source file links at line and column targets in the TUI (#61890)
## What changed

- Extend sidebar previews beyond Markdown to source files, including extensionless files. Preserve line and column targets such as `./Makefile#L12C3`, scroll to the target, and highlight its line and character.
- Render source previews with syntax highlighting and line numbers when space permits. Keep plain Markdown links rendered as Markdown, and show source when a location is specified.
- Show local link labels without appended destinations when workspace previews are available, and emphasize links on hover. Refresh rendering when preview availability changes.
- Limit source previews to 1 MiB and 10,000 lines, and disable document previews for Windows workspaces.

## Testing

Add coverage for extensionless link parsing, matching hover and click targets, streamed file citation metadata, and source navigation with CRLF input, tab reuse, and Escape handling.

GitOrigin-RevId: c2629b122628a4f5afa17df1890f63bd1c9bbcf6
2026-11-16 21:50:03 +00:00

56 lines
2.0 KiB
Rust

//! Single-width render cache shared by finalized source-backed markdown history cells.
use crate::markdown_render::ListSpacing;
use crate::terminal_hyperlinks::HyperlinkLine;
use std::sync::Mutex;
use std::sync::PoisonError;
#[derive(Debug, Default)]
pub(super) struct MarkdownRenderCache {
pub(super) cached: Mutex<Option<(MarkdownRenderCacheKey, Vec<HyperlinkLine>)>>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct MarkdownRenderCacheKey {
pub(super) width: u16,
pub(super) list_spacing: ListSpacing,
pub(super) syntax_theme_revision: u64,
pub(super) terminal_fg: Option<(u8, u8, u8)>,
pub(super) terminal_bg: Option<(u8, u8, u8)>,
pub(super) color_level: crate::terminal_palette::StdoutColorLevel,
pub(super) document_links: bool,
}
impl MarkdownRenderCache {
/// Return lines cached for this width and terminal render state, rendering on a cache miss.
///
/// Only the most recent entry is retained, so changing width, syntax theme, or terminal colors
/// replaces the cached render.
pub(super) fn render(
&self,
width: u16,
list_spacing: ListSpacing,
render: impl FnOnce() -> Vec<HyperlinkLine>,
) -> Vec<HyperlinkLine> {
let key = MarkdownRenderCacheKey {
width,
list_spacing,
syntax_theme_revision: crate::render::highlight::syntax_theme_revision(),
terminal_fg: crate::terminal_palette::default_fg(),
terminal_bg: crate::terminal_palette::default_bg(),
color_level: crate::terminal_palette::stdout_color_level(),
document_links: crate::markdown_render::preferences::document_links_enabled(),
};
let mut cached = self.cached.lock().unwrap_or_else(PoisonError::into_inner);
if let Some((cached_key, lines)) = cached.as_ref()
&& *cached_key == key
{
return lines.clone();
}
let lines = render();
*cached = Some((key, lines.clone()));
lines
}
}