//! Browse Structure as a prose-preserving document surface + annotation bands. //! //! See `doc/design/view-document-annotations.md` · `doc/design/view-execution-trace.md`. use std::collections::{HashMap, HashSet}; use crate::ast::{Function, Module, Stmt}; use crate::host::writeback; use crate::lex::{classify_source, ClassifiedLine, LineKind}; use crate::parse::{scan_prose_line, ProseBit}; use crate::view::annot::{ annots_by_line, bit_at, bit_kind_class, prose_bit_end, render_annot_band, ViewAnnot, }; use crate::view::html::escape; use crate::view::render::{ comment_markdown_to_html, render_code_assign_card, render_formula_assign_card, }; const MAX_VISIBLE_ANNOTS: usize = 4; /// Render Browse Structure: narration kept, executable spans marked, annot bands attached. pub fn render_module_document(module: &Module, source: &str, annots: &[ViewAnnot]) -> String { let lines = classify_source(source); let by_line = annots_by_line(annots); let fun_by_line = index_functions(module); let special = index_special_assigns(module, source); let skip_body = special_body_lines(&special); let stmt_lines = index_stmt_lines(module); let occluded_lines = index_occluded_lines(module); let writebacks = writeback::writeback_map(source); let taken_arms = taken_arms_by_branch_line(annots); let mut out = String::from(r#"
blob.
if crate::foreign::is_fence_opener(trimmed)
|| (trimmed.starts_with("```") && !crate::foreign::is_fence_closer(trimmed))
{
flush_para(&mut para, &mut out);
let (html, next) = render_narrative_fence(&lines, i);
out.push_str(&html);
i = next;
continue;
}
if crate::foreign::is_fence_closer(trimmed) {
// Orphan closer — skip.
flush_para(&mut para, &mut out);
i += 1;
continue;
}
let bits = scan_prose_line(text);
let is_stmt = stmt_lines.contains(&l.line_no);
let has_annots = by_line
.get(&l.line_no)
.map(|v| !v.is_empty())
.unwrap_or(false);
// Decl alone stays in Markdown prose (keep links). Open a marked
// line block when there is executable prose, a stmt, or chips.
let needs_annot = is_stmt
|| has_annots
|| bits.iter().any(|b| {
matches!(b, ProseBit::BoldCode { .. } | ProseBit::ItalicReturn { .. })
});
if needs_annot {
flush_para(&mut para, &mut out);
out.push_str(&render_line_block(
l.line_no,
text,
&bits,
by_line.get(&l.line_no).map(|v| v.as_slice()).unwrap_or(&[]),
special.get(&l.line_no),
writebacks.get(&l.line_no),
None,
&occluded_lines,
));
} else if !trimmed.is_empty() {
para.push((l.line_no, trimmed.to_string()));
}
i += 1;
}
LineKind::Code => {
flush_para(&mut para, &mut out);
let text = l.text.trim_end_matches(['\r', '\n']);
if let Some(fun) = fun_by_line.get(&l.line_no) {
out.push_str(&render_fun_heading(fun, annots));
if let Some(list) = by_line.get(&l.line_no) {
if !list.is_empty() {
let owned: Vec = list.iter().map(|a| (*a).clone()).collect();
out.push_str(&render_annot_band(&owned, MAX_VISIBLE_ANNOTS));
}
}
i += 1;
continue;
}
let trimmed = text.trim_start();
if let Some((kind, marker_len)) = list_marker(trimmed) {
let (html, next) = render_list_block(
&lines,
i,
kind,
marker_len,
&by_line,
&special,
&writebacks,
&taken_arms,
annots,
&occluded_lines,
);
out.push_str(&html);
i = next;
continue;
}
let bits = scan_prose_line(text);
out.push_str(&render_line_block(
l.line_no,
text,
&bits,
by_line.get(&l.line_no).map(|v| v.as_slice()).unwrap_or(&[]),
special.get(&l.line_no),
writebacks.get(&l.line_no),
None,
&occluded_lines,
));
i += 1;
}
}
}
flush_para(&mut para, &mut out);
out.push_str(" | {cell} | ")); } html.push_str("
|---|
| {} | ", escape(cell))); } html.push_str("
| {cell} | ")); } html.push_str("