1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use ratatui::{
buffer::Buffer,
layout::{Position, Rect},
};
use super::state::{DocumentLayoutCache, InteractiveState};
use crate::styled_string::NodePath;
// Baseline left margin for all content - provides breathing room and space for outdented borders
pub(super) const BASELINE_LEFT_MARGIN: u16 = 3;
impl<'a> InteractiveState<'a> {
/// Render document nodes to buffer, updating action map
pub(super) fn render_document(&mut self, _area: Rect, buf: &mut Buffer) {
self.render_cache.actions.clear();
// Layout state already initialized in render_frame with area
// Set initial position and indent
self.layout.pos = Position {
x: BASELINE_LEFT_MARGIN,
y: 0,
};
self.layout.indent = BASELINE_LEFT_MARGIN;
// Check if we need to recalculate height (cache invalid or missing)
let need_height_calc = self
.viewport
.cached_layout
.map(|cache| cache.render_width != self.layout.area.width)
.unwrap_or(true);
// Use raw pointer to avoid borrow checker issues when calling render_node
let nodes_ptr = self.document.document.nodes.as_ptr();
let node_count = self.document.document.nodes.len();
for idx in 0..node_count {
// Only short-circuit if we have a valid cache (don't need full height calculation)
if !need_height_calc
&& self.layout.pos.y >= self.layout.area.height + self.viewport.scroll_offset
{
break; // Past visible area and we already know the full height
}
// Add blank line between consecutive top-level blocks
if idx > 0 {
self.layout.pos.y += 1;
}
// Update path for this top-level node
self.layout.node_path = NodePath::new();
self.layout.node_path.push(idx);
// SAFETY: idx is bounded by node_count, and nodes_ptr is valid for the duration of this method
let node = unsafe { &*nodes_ptr.add(idx) };
self.render_node(node, buf);
}
// Update cache if we just did a full render
if need_height_calc {
self.viewport.cached_layout = Some(DocumentLayoutCache {
render_width: self.layout.area.width,
document_height: self.layout.pos.y,
});
}
}
}