Code folding with gutter fold indicators and content hiding. Supports hierarchical folding for Markdown files (headings, code blocks, lists) and indentation-based folding for code/data files.
Status: Fully functional with FerriteEditor.
| File | Purpose |
|---|---|
src/state.rs |
FoldKind, FoldRegion, FoldState data structures |
src/editor/folding.rs |
Fold region detection algorithms |
src/editor/widget.rs |
Fold state sync between Tab and FerriteEditor |
src/editor/ferrite/editor.rs |
Click handling, y-position calculation for hidden lines |
src/editor/ferrite/rendering/gutter.rs |
Fold indicator rendering (▶/▼) |
src/config/settings.rs |
Folding configuration settings |
pub enum FoldKind {
Heading(u8), // Markdown heading level 1-6
CodeBlock, // Fenced code blocks (```)
List, // List hierarchies
Indentation, // Indentation-based (JSON/YAML)
}pub struct FoldRegion {
pub id: FoldId,
pub start_line: usize, // 0-indexed
pub end_line: usize, // 0-indexed, inclusive
pub kind: FoldKind,
pub collapsed: bool,
pub preview_text: String, // First ~50 chars for display
}Manages all fold regions for a document:
regions: Vec<FoldRegion>- All detected fold regionsdirty: bool- Whether regions need recomputation- Methods for toggling, querying, and bulk operations
Detection algorithms in src/editor/folding.rs:
- Markdown Headings - Headings fold until next heading of same/higher level
- Code Blocks - Fenced code blocks (
...) - List Hierarchies - Nested list items based on indentation
- Indentation-based - For JSON/YAML/structured files
Detection is triggered when content changes (dirty flag) and preserves collapsed state across re-detection.
Visual indicators in the editor gutter:
- Expanded (▼) - Down-pointing triangle, default color
- Collapsed (▶) - Right-pointing triangle, orange highlight
Click detection on indicators toggles fold state.
In Settings struct:
pub folding_enabled: bool, // Master toggle
pub folding_show_indicators: bool, // Show gutter indicators
pub fold_headings: bool, // Detect heading folds
pub fold_code_blocks: bool, // Detect code block folds
pub fold_lists: bool, // Detect list folds
pub fold_indentation: bool, // Detect indentation folds| Shortcut | Action |
|---|---|
Ctrl+Shift+[ |
Fold all regions |
Ctrl+Shift+] |
Unfold all regions |
Ctrl+Shift+. |
Toggle fold at cursor |
- ✅ Fold region detection for all types
- ✅ Gutter fold indicators with click toggle
- ✅ Visual state change (triangle direction + color)
- ✅ Fold state persistence across content changes
- ✅ Keyboard shortcuts for fold operations
- ✅ Settings UI for enabling/disabling fold types
- ✅ Text hiding - Collapsed regions hide content and space collapses
- ✅ Fold state synced between Tab and FerriteEditor
- User clicks fold indicator in gutter
FerriteEditor.ui()detects click in fold indicator areay_to_line()converts click position to line numberfold_state.toggle_at_line()toggles the fold in FerriteEditorwidget.rssyncs fold state back to Tab:tab.fold_state = editor.fold_state().clone()
Important: The toggle happens in FerriteEditor and is synced to Tab. Do NOT toggle again in app.rs.
Hidden Line Handling
When rendering, hidden lines (inside collapsed folds) are handled by:
- Y-position calculation skips hidden lines (they don't add height)
- Render loop skips hidden lines with
continue - Hidden lines still get y-positions (for vector indexing) but don't occupy space
// In y-position calculation
if !self.fold_state.is_line_hidden(line_idx) {
y += self.view.get_line_height(line_idx);
}
// In render loop
if self.fold_state.is_line_hidden(line_idx) {
continue;
}Folds are hierarchical - outer folds contain inner folds:
- Folding an outer region hides all nested content
- Inner folds can be toggled independently when outer is expanded
Example for Rust code with indentation-based folding:
implblock (indent 0) contains entire implementationfn(indent 4) contains function bodymatch(indent 8) contains match arms- Folding the
implhides everything inside
- ⏳ Placeholder lines - "... (X lines folded)" display
- ⏳ Cursor interaction - Auto-expand when cursor enters folded region
- ⏳ Scroll accounting - Scroll position doesn't fully account for hidden lines yet
- ⏳ Bracket-based folding - More intuitive for code files than pure indentation
- Open any file (fold detection works for Markdown, JSON, YAML, etc.)
- Enable fold indicators: Settings > Editor > Code Folding > Show Fold Indicators
- Look for fold indicators (▼) in the gutter next to foldable regions
- Click an indicator to toggle collapsed state (turns ▶ when collapsed, content hides)
- Use keyboard shortcuts for bulk operations
cargo build
cargo run- Open a Markdown file with headings - fold indicators appear
- Click a fold indicator - content should collapse and space should shrink
- Click again - content should expand
- Test keyboard shortcuts (Ctrl+Shift+[/]/.)