Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-tui): keep wrapped-term search through rules and read region text only on screen

thinhlpg743d4e112ffe
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index c3839ce84..e92f967f0 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -2769,7 +2769,7 @@ impl Transcript {
idx: Some(i),
height: row.height(width),
lines: None,
- flat: None,
+ detail: false,
}
}
}
@@ -2777,7 +2777,7 @@ impl Transcript {
idx: Some(i),
height: row.height(width),
lines: None,
- flat: None,
+ detail: false,
},
};
if find_hit.is_some_and(|h| h.row == i && !h.detail) {
@@ -2830,12 +2830,7 @@ impl Transcript {
let mut seg = Segment::eager(Some(i), detail, width);
// The lingering reasoning tail is a bounded view of the block,
// not a wrapped copy of its detail, so it has no unwrapped form.
- if find.is_some() && !(active_reasoning && !expanded) {
- let index = region_index.get_or_insert_with(|| self.region_index());
- seg.flat = self
- .region_detail_in(index, i, find::FLAT_WIDTH)
- .map(|lines| find::flat_text(&lines));
- }
+ seg.detail = !(active_reasoning && !expanded);
content_h = content_h.saturating_add(seg.height);
segs.push(seg);
}
@@ -2911,7 +2906,9 @@ impl Transcript {
fn all_lines(&self, width: u16) -> Vec<Line<'static>> {
let layout = self.layout_body(width, 0, false, true, None, Vec::new());
let height = layout.content_h;
- layout.window(&self.rows, width, 0, height, None).0
+ layout
+ .window(&self.rows, width, 0, height, None, &|_| None)
+ .0
}
/// Mark every edit/write row still awaiting a result as interrupted: the
@@ -19080,8 +19077,14 @@ fn draw_agent_detail(f: &mut Frame, area: Rect, app: &mut App) {
let max_back = layout.content_h.saturating_sub(body.height);
app.agent_detail_scroll = app.agent_detail_scroll.min(max_back);
let scroll = max_back - app.agent_detail_scroll;
- let (lines, _, offset) =
- layout.window(&panel.transcript.rows, width, scroll, body.height, None);
+ let (lines, _, offset) = layout.window(
+ &panel.transcript.rows,
+ width,
+ scroll,
+ body.height,
+ None,
+ &|_| None,
+ );
f.render_widget(
Paragraph::new(lines)
.wrap(Wrap { trim: false })
@@ -21200,10 +21203,25 @@ struct Segment {
idx: Option<usize>,
height: u16,
lines: Option<Vec<Line<'static>>>,
- /// An expanded region's text before wrapping, read only while a search is
- /// active. A committed row's is kept on the row itself; every other
- /// inline segment is searched as rendered.
- flat: Option<Vec<String>>,
+ /// Whether this is an expanded region's detail, whose unwrapped text a
+ /// search reads (see `detail_flat_text`) for the segments on screen.
+ detail: bool,
+}
+
+/// An expanded region's text before wrapping, read through a per-frame
+/// `RegionIndex` built on first use. Only called for the detail segments a
+/// search is about to paint (the ones on screen) and for the one it jumps to,
+/// so a transcript with many expanded regions does not re-render all of them
+/// every frame.
+fn detail_flat_text(
+ transcript: &Transcript,
+ index: &std::cell::OnceCell<RegionIndex>,
+ row: usize,
+) -> Option<Vec<String>> {
+ let index = index.get_or_init(|| transcript.region_index());
+ transcript
+ .region_detail_in(index, row, find::FLAT_WIDTH)
+ .map(|lines| find::flat_text(&lines))
}
impl Segment {
@@ -21212,7 +21230,7 @@ impl Segment {
idx,
height: wrapped_height_of(&lines, width),
lines: Some(lines),
- flat: None,
+ detail: false,
}
}
}
@@ -21245,7 +21263,7 @@ impl BodyLayout {
idx: None,
height: pad,
lines: Some(vec![Line::raw(""); pad as usize]),
- flat: None,
+ detail: false,
},
);
self.content_h = self.content_h.saturating_add(pad);
@@ -21268,6 +21286,7 @@ impl BodyLayout {
scroll: u16,
height: u16,
find: Option<(&str, Option<find::Hit>)>,
+ detail_flat: &dyn Fn(usize) -> Option<Vec<String>>,
) -> (Vec<Line<'static>>, Vec<Option<usize>>, u16) {
let end = scroll.saturating_add(height);
let mut visible: Vec<Line<'static>> = Vec::new();
@@ -21298,10 +21317,15 @@ impl BodyLayout {
Some((needle, hit)) => {
// Matched on the unwrapped text, so a term that wrapped
// is painted on each line it lands on. A committed row
- // keeps that text itself; an expanded region carries it.
+ // keeps that text itself; an expanded region's is read
+ // here, for the segments on screen only.
let row = seg.idx.filter(|_| committed).and_then(|i| rows.get(i));
let kept = row.map(Row::flat);
- let flat = match (&kept, &seg.flat) {
+ let region = seg
+ .idx
+ .filter(|_| seg.detail)
+ .and_then(detail_flat);
+ let flat = match (&kept, &region) {
(Some(kept), _) => Some(kept.as_slice()),
(None, Some(flat)) => Some(flat.as_slice()),
(None, None) => None,
@@ -21535,6 +21559,7 @@ fn draw(f: &mut Frame, app: &mut App) {
// may have wrapped, so its logical line is not a rendered line), measured
// with the same wrap the body uses. Only on the jump frame, so the lines
// above it are cloned once per step, not every frame.
+ let flat_index = std::cell::OnceCell::new();
let above = layout
.segs
.get(layout.find_seg.unwrap_or(usize::MAX))
@@ -21546,7 +21571,11 @@ fn draw(f: &mut Frame, app: &mut App) {
None => (Vec::new(), None),
},
};
- let flat = kept.as_deref().map(Vec::as_slice).or(seg.flat.as_deref());
+ let region = seg
+ .detail
+ .then(|| detail_flat_text(&app.transcript, &flat_index, hit.row))
+ .flatten();
+ let flat = kept.as_deref().map(Vec::as_slice).or(region.as_deref());
let first = find::occurrences(&lines, flat, needle)
.iter()
.find(|o| o.logical == hit.line)
@@ -21566,12 +21595,15 @@ fn draw(f: &mut Frame, app: &mut App) {
}
app.scrollback = app.scrollback.min(max_back);
let scroll = max_back - app.scrollback;
+ let flat_index = std::cell::OnceCell::new();
+ let detail_flat = |i: usize| detail_flat_text(&app.transcript, &flat_index, i);
let (visible, row_index, offset) = layout.window(
&app.transcript.rows,
width,
scroll,
inner_h,
find_needle.as_deref().map(|needle| (needle, find_hit)),
+ &detail_flat,
);
let body = Paragraph::new(visible)
.wrap(Wrap { trim: false })
diff --git a/src-tauri/src/core/cli/tui/find.rs b/src-tauri/src/core/cli/tui/find.rs
index 9622880b7..6d923b66e 100644
--- a/src-tauri/src/core/cli/tui/find.rs
+++ b/src-tauri/src/core/cli/tui/find.rs
@@ -249,18 +249,42 @@ impl Glyphs {
/// For each of this one's characters, the index of the same character in
/// `other`, which may carry extras (a gutter glyph repeated on every
/// continuation line). `None` when this one has text `other` does not.
+ ///
+ /// A line of only rule glyphs (a horizontal rule, a box border) is as wide
+ /// as the render it came from, so this one has far more of them than a
+ /// narrower `other`. Such a line is mapped as a unit onto the matching line
+ /// of `other`, its surplus glyphs landing on that line's last one.
fn onto(&self, other: &Glyphs) -> Option<Vec<usize>> {
let mut map = Vec::with_capacity(self.chars.len());
let mut next = 0;
- for c in &self.chars {
- let skip = other.chars[next..].iter().position(|o| o == c)?;
- map.push(next + skip);
- next += skip + 1;
+ for line in 0..self.starts.len() - 1 {
+ let (from, to) = (self.starts[line], self.starts[line + 1]);
+ let own = &self.chars[from..to];
+ if !own.is_empty() && own.iter().all(|&c| is_rule_glyph(c)) {
+ if let Some(&(shown, _)) = other.at.get(next) {
+ let (a, b) = (other.starts[shown], other.starts[shown + 1]);
+ if a == next && other.chars[a..b].iter().all(|&c| is_rule_glyph(c)) {
+ map.extend((0..own.len()).map(|k| a + k.min(b - a - 1)));
+ next = b;
+ continue;
+ }
+ }
+ }
+ for c in own {
+ let skip = other.chars[next..].iter().position(|o| o == c)?;
+ map.push(next + skip);
+ next += skip + 1;
+ }
}
Some(map)
}
}
+/// Glyphs a horizontal rule or a box border is drawn with.
+fn is_rule_glyph(c: char) -> bool {
+ matches!(c, '-' | '+' | '|' | '=' | '_') || ('\u{2500}'..='\u{257F}').contains(&c)
+}
+
/// `line` with `ranges` painted over its own styles, in order. Spans are split
/// at the edges, so a range that straddles two styled spans is still covered
/// whole. A `true` range is the match the viewport jumped to and gets the
@@ -601,6 +625,37 @@ mod tests {
assert_eq!(found[2].logical, 2);
}
+ // A rule or a box border is as wide as the render it is in, so the
+ // unwrapped text carries far more of its glyphs than the narrow render.
+ // That must not cost the rest of the region its wrap-aware matching.
+ #[test]
+ fn a_rule_or_box_border_does_not_stop_a_wrapped_term_being_found() {
+ let rule = "-".repeat(40);
+ let border = format!("+{}+", "-".repeat(38));
+ let shown = [
+ "the quick brown",
+ "fox jumps",
+ "-----",
+ "+---+",
+ "| code |",
+ "+---+",
+ ];
+ let whole = [
+ "the quick brown fox jumps".to_string(),
+ rule,
+ border.clone(),
+ "| code |".to_string(),
+ border,
+ ];
+ let found = occurrences(&rows(&shown), Some(&whole), "brown fox");
+ assert_eq!(found.len(), 1);
+ assert_eq!(covered(&shown, &found[0]), vec!["brown", "fox"]);
+ let found = occurrences(&rows(&shown), Some(&whole), "code");
+ assert_eq!(found.len(), 1);
+ assert_eq!(found[0].logical, 3);
+ assert_eq!(covered(&shown, &found[0]), vec!["code"]);
+ }
+
#[test]
fn a_wrapped_term_is_painted_on_both_lines() {
let shown = ["say hel", "lo there"];