mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
## Why A single-line jump doesn't show where a referenced block of code ends. ## What changed Recognize file ranges such as `src/search.ts:42-44` and `src/search.ts#L42-L44` in Markdown links and bare prose. Opening a range in the sidebar jumps to its start and highlights every line through its end, including wrapped rows and blank lines. If the link specifies a starting column, mark it too. Pressing `Esc` can clear the highlight, and it stays cleared across reloads. Keep each wrapped link as one keyboard navigation stop, even when fragments from different links share a table row. Preserve hidden range destinations when copying Markdown. ## Testing Add coverage for malformed and out-of-bounds ranges, mouse and keyboard navigation across wrapped links, Markdown copying, and highlight rendering across terminal palettes. GitOrigin-RevId: 1171533053e92a61de01f90f248c58b059b3ae32
153 lines
5.4 KiB
Rust
153 lines
5.4 KiB
Rust
//! Render source locations in the file reader using the transcript's wrapped rows.
|
|
|
|
use ratatui::style::Color;
|
|
use ratatui::style::Modifier;
|
|
use ratatui::style::Style;
|
|
use ratatui::style::Stylize;
|
|
use ratatui::text::Line;
|
|
use ratatui::widgets::Widget;
|
|
|
|
use super::*;
|
|
|
|
/// A document can be replaced on disk; retain the offset, not the old rendered cells.
|
|
#[derive(Clone, Copy)]
|
|
pub(crate) enum DocumentPosition {
|
|
Offset(usize),
|
|
Latest,
|
|
}
|
|
|
|
/// Resolved logical lines and the grapheme-aligned byte offset used for scrolling.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(crate) struct DocumentTarget {
|
|
pub(crate) first_line: usize,
|
|
pub(crate) last_line: usize,
|
|
pub(crate) offset: usize,
|
|
pub(crate) mark_column: bool,
|
|
}
|
|
|
|
/// A terminal may not report its background. Pair the syntax theme's canvas with
|
|
/// its text color for unstyled glyphs, and preserve syntax-specific foregrounds.
|
|
fn source_highlight() -> (Style, Option<Color>) {
|
|
use crate::render::highlight::convert_syntect_color;
|
|
let (background, foreground) = match crate::terminal_palette::default_bg() {
|
|
Some(bg) => (Some(bg), None),
|
|
None => crate::render::highlight::with_syntax_theme(|theme| {
|
|
let background = match theme.settings.background.and_then(convert_syntect_color) {
|
|
Some(Color::Rgb(r, g, b)) => Some((r, g, b)),
|
|
_ => None,
|
|
};
|
|
(
|
|
background,
|
|
theme.settings.foreground.and_then(convert_syntect_color),
|
|
)
|
|
}),
|
|
};
|
|
let style = crate::style::user_message_style_for(background);
|
|
if matches!(style.bg, None | Some(Color::Reset)) {
|
|
(crate::style::selection_style(), None)
|
|
} else {
|
|
(style, foreground)
|
|
}
|
|
}
|
|
|
|
impl TranscriptView {
|
|
/// During a live resize, wrap the already rendered file instead of parsing it at every width.
|
|
pub(crate) fn set_document_resize_preview(&mut self, active: bool) {
|
|
self.cache.set_document_resize_preview(active);
|
|
}
|
|
|
|
pub(crate) fn document_position(&self) -> DocumentPosition {
|
|
match self.position {
|
|
Position::Reading(anchor) => DocumentPosition::Offset(anchor.offset),
|
|
Position::Latest => DocumentPosition::Latest,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn render_document(
|
|
&mut self,
|
|
area: Rect,
|
|
buf: &mut Buffer,
|
|
cells: &[Arc<dyn HistoryCell>],
|
|
line_starts: &[usize],
|
|
target: Option<DocumentTarget>,
|
|
) {
|
|
let digits = line_starts
|
|
.len()
|
|
.checked_ilog10()
|
|
.map_or(0, |n| n as u16 + 1);
|
|
let gutter = if digits > 0 && area.width > 40 + digits {
|
|
digits + 1
|
|
} else {
|
|
0
|
|
};
|
|
let text = Rect::new(area.x + gutter, area.y, area.width - gutter, area.height);
|
|
self.render(text, buf, cells);
|
|
if line_starts.is_empty() {
|
|
return;
|
|
}
|
|
let highlight = target.map(|target| (target, source_highlight()));
|
|
for (y, visible) in (area.top()..area.bottom()).zip(&self.visible) {
|
|
let offset = visible.layout.position_at(visible.row, /*column*/ 0);
|
|
let line = line_starts.partition_point(|start| *start <= offset);
|
|
if gutter > 0 && line > 0 && line_starts[line - 1] == offset {
|
|
Line::from(line.to_string())
|
|
.dim()
|
|
.right_aligned()
|
|
.render(Rect::new(area.x, y, digits, /*height*/ 1), buf);
|
|
}
|
|
if let Some((target, (style, foreground))) = highlight
|
|
&& (target.first_line..=target.last_line).contains(&line)
|
|
{
|
|
for x in text.left()..text.right() {
|
|
let cell = &mut buf[(x, y)];
|
|
cell.set_style(style);
|
|
if let Some(fg) = foreground
|
|
&& cell.fg == Color::Reset
|
|
{
|
|
cell.set_fg(fg);
|
|
}
|
|
}
|
|
if target.mark_column
|
|
&& line == target.first_line
|
|
&& visible.row == visible.layout.row_for_offset(target.offset)
|
|
{
|
|
let x = text
|
|
.x
|
|
.saturating_add(visible.layout.column_for_offset(target.offset));
|
|
if x < text.right() {
|
|
let cell = &mut buf[(x, y)];
|
|
let style = cell.style();
|
|
cell.set_style(if style.add_modifier.contains(Modifier::REVERSED) {
|
|
style.not_reversed()
|
|
} else {
|
|
style.reversed()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn paint_document_scroll_indicators(
|
|
&mut self,
|
|
area: Rect,
|
|
buf: &mut Buffer,
|
|
cells: &[Arc<dyn HistoryCell>],
|
|
) {
|
|
if area.height < 2 || area.width < 3 {
|
|
return;
|
|
}
|
|
let start = self.start(cells);
|
|
for (visible, row, arrow) in [
|
|
(start > (0, 0), area.top(), "↑"),
|
|
(start < self.bottom_start(cells), area.bottom() - 1, "↓"),
|
|
] {
|
|
if visible {
|
|
buf[(area.right() - 1, row)]
|
|
.set_symbol(arrow)
|
|
.set_style(crate::style::secondary_text_style());
|
|
}
|
|
}
|
|
}
|
|
}
|