feat: Implement heading-based nesting outlines in Outline window

- Add depth and path fields to Beat struct to track heading hierarchy
- Rewrite beats() parser to use heading stack and process_headings() helper
- Add BeatTree enum with Heading and Beat variants for tree structure
- Add build_beat_tree() to convert flat beats to nested tree
- Add render_beat_tree() for recursive tree rendering with collapsible headers
- Add count_beat_tree() helper to count beats in subtree
- Replace flat list rendering with tree-based rendering in outline_window()

The implementation currently groups beats by section, with infrastructure
in place for full hierarchical nesting based on beat.path.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
landon
2026-09-15 14:56:35 -05:00
parent 921f3e74c5
commit cd8663349a
2 changed files with 156 additions and 32 deletions
+53 -11
View File
@@ -25,29 +25,36 @@ pub struct Beat {
pub name: String,
/// Whether any prose has been written for it.
pub filled: bool,
/// Nesting depth based on markdown headings (0 = no heading, 1 = #, 2 = ##, etc.)
pub depth: usize,
/// Full path of parent headings, e.g., ["Act 1", "Chapter 1"] for a scene in Act 1, Chapter 1
pub path: Vec<String>,
}
/// Read an outline file's beats and their state.
///
/// A beat is "filled" when there is ordinary text between its prompt comment
/// and the next prompt or heading — which is exactly where you would type it.
///
/// Heading hierarchy is tracked to support nesting. Each heading level (#, ##, ###)
/// increases the nesting depth, and beats inherit the path of their parent headings.
pub fn beats(markdown: &str) -> Vec<Beat> {
let mut out: Vec<Beat> = Vec::new();
let mut section = String::new();
let mut rest = markdown;
// Track heading hierarchy: stack of (heading_text, depth) pairs
let mut heading_stack: Vec<(String, usize)> = Vec::new();
// Text seen since the last prompt, which decides whether it is filled.
let mut pending_prose = String::new();
while let Some(start) = rest.find("<!--") {
let before = &rest[..start];
// Process any headings in the text before this comment
process_headings(before, &mut heading_stack);
// Anything before this comment belongs to the previous beat.
pending_prose.push_str(before);
if let Some(heading) = last_heading(before) {
// A heading closes the previous beat and opens a new section.
settle(&mut out, &mut pending_prose);
section = heading;
}
settle(&mut out, &mut pending_prose);
let Some(end) = rest[start + 4..].find("-->") else {
@@ -55,23 +62,58 @@ pub fn beats(markdown: &str) -> Vec<Beat> {
};
let body = &rest[start + 4..start + 4 + end];
if let Some(name) = prompt_name(body) {
// Build path from heading stack
let path: Vec<String> = heading_stack.iter().map(|(h, _)| h.clone()).collect();
let depth = heading_stack.len();
let section = if !heading_stack.is_empty() {
heading_stack.last().unwrap().0.clone()
} else {
String::new()
};
out.push(Beat {
section: section.clone(),
name,
filled: false,
depth,
path,
});
}
rest = &rest[start + 4 + end + 3..];
}
pending_prose.push_str(rest);
if let Some(heading) = last_heading(rest) {
settle(&mut out, &mut pending_prose);
let _ = heading; // a trailing heading opens nothing
}
// Process any remaining headings
process_headings(rest, &mut heading_stack);
settle(&mut out, &mut pending_prose);
out
}
/// Process headings in text and update the heading stack.
fn process_headings(text: &str, heading_stack: &mut Vec<(String, usize)>) {
for line in text.lines() {
let trimmed = line.trim();
if !trimmed.starts_with('#') {
continue;
}
let hashes = trimmed.len() - trimmed.trim_start_matches('#').len();
if hashes == 0 || hashes > 6 {
continue;
}
let name = trimmed[hashes..].trim();
if name.is_empty() {
continue;
}
// Pop headings that are deeper than current level
while heading_stack.len() >= hashes {
heading_stack.pop();
}
// Push current heading
heading_stack.push((name.to_string(), hashes));
}
}
/// Credit whatever prose has accumulated to the most recent beat, then clear it.
fn settle(beats: &mut [Beat], pending: &mut String) {
if has_prose(pending) {