Skip to main content

Module folds

Module folds 

Source
Expand description

Computed folds (DESIGN.md §5.1, §15:18; user-facing reference at docs/user/folding.md).

Folds derived automatically from the buffer’s structure. v1 providers:

  1. Indent – fold spans any line whose successor indents deeper, ending at the last line whose indent is strictly greater than the start. Universal across languages.
  2. Markdown – ^#+ headings define the fold tree. A # H1 folds until the next # H1; a ## H2 folds until the next same-or-higher heading.
  3. Syntax (tree-sitter) – runs the language’s compiled folds.scm query against the parse tree owned by lattice_syntax::Syntax. Each @fold capture becomes a fold spanning the captured node’s lines. Falls back to the Markdown / Indent providers for languages that don’t ship a folds.scm yet.

Manual folds (created via zf from a Visual selection) and computed folds coexist in [crate::app::App::folds] with no distinction at the storage layer; the :set foldmethod option decides which side feeds in.

Fold identity (Fold::identity) is the SHA-style hash of the trimmed start-line text plus indent depth (for indent / markdown providers) or (node_kind, trimmed start-line text) (for the syntax provider). When the buffer changes and folds recompute, we match new folds to old ones by identity and transfer the closed-state – so adding a line to one section doesn’t reopen the closed section above. Manual folds carry identity = None (their stable identity is the line range itself).

Structs§

FoldIndex
O(log folds) lookup index built once per frame (or per publish) over a snapshot of the active document’s folds.
IndentPrimary
LspPrimary
ManualPrimary
MarkdownPrimary
SyntaxPrimary

Enums§

FoldMarker
A gutter fold marker’s state: the head row of an open (expanded) fold versus a closed (collapsed) one. Renderers map this to their glyph + themed colour (gutter.fold.open / gutter.fold.closed).
FoldRecomputeCause
Why crate::editor::Editor::recompute_folds_because is running, and so whether foldlevel gets to seed the folds it has not seen before.

Functions§

apply_fold_level
Close every fold deeper than level, open the rest.
apply_fold_level_to_new
Close new folds that sit deeper than foldlevel, leaving folds whose state was carried over untouched.
compute_code_block_lines
MC.2: the source lines that lie inside a fenced (or indented) code block, for the full-width syntax.code_block background tint (markdown and any grammar exposing those node kinds).
compute_fold_hash
Phase 5.8.AF.5 / Slice X2: hoisted host-side from lattice-ui-tui::app::folds::compute_fold_hash so dispatch’s publish_render_state can populate SyntaxRenderState::fold_hash without depending on the renderer crate.
compute_indent_folds
Run the indent-based fold algorithm against buffer and return every fold it discovers. All produced folds are open (closed = false) by default – vim’s foldlevelstart would override that, but v1 doesn’t model the level option yet.
compute_markdown_folds
Markdown heading-based fold provider (DESIGN.md §15:18, docs/user/folding.md). Walks the buffer for ATX headings (^#+\s) and emits one fold per heading whose body has at least one row. Heading depth (the number of #s) determines nesting: a ## H2 ends at the next same-or-shallower heading (# H1 or another ## H2 or end-of-buffer).
compute_syntax_folds
Tree-sitter-driven fold provider. Runs the language’s compiled folds.scm query against lattice_syntax::Syntax’s parse tree and emits one Fold per @fold capture spanning more than a single line.
count_visible_rows_between
Count how many visible (non-fold-hidden) rows exist between from_line (inclusive) and to_line (exclusive). Closed fold bodies are skipped. Both lines must be visible (not inside a closed fold body) — callers ensure this before calling. Returns the number of display rows spanning the range.
fold_aware_visible_end
The exclusive buffer-line bound a height-row viewport starting at scroll actually reaches.
fold_aware_visible_end_of
fold_aware_visible_end from a raw fold slice, skipping the FoldIndex build when there is no closed fold to walk over — the overwhelmingly common case, and one that runs per worker tick.
fold_levels
Hash the user-visible signature of the current fold set.
fold_summary_text
The ⋯ N lines summary text trailing a collapsed head row, where n is folded_line_span’s count. One function so the TUI and GPUI peers cannot drift on spacing, glyph, or pluralisation — the TUI grew this as an inline format! and GPUI had no summary at all, which is exactly how two renderers end up disagreeing.
folded_line_span
max_fold_level
Deepest level present, or 0 when there are no folds. zR’s equivalent foldlevel value.
nth_visible_line_backward
Walk backward from start_line and return the line at offset visible rows above it. Walking backward hops over closed fold bodies to their visible heads, then continues from the preceding line. Returns 0 when the walk hits BOF before consuming offset rows.
nth_visible_line_forward
Walk forward from start_line and return the buffer line at offset visible (non-fold-hidden) display rows below it. Closed fold bodies are skipped — only fold heads (start lines) and non-folded lines count as visible rows. When the walk reaches the last addressable line before consuming offset rows, that last line is returned (the caller’s clamp).
visible_fold_edge
VM.3i: vim’s zj / zk edge from line — the nearest fold START after it (forward) or fold END before it — among edges that are VISIBLE.
visible_source_lines
Number of buffer lines collapsed onto the visible head row of the closed fold (start_line, end_line) — the count both renderers show in the ⋯ N lines fold summary. Walks forward from end_line + 1 through any sibling closed folds whose heading is itself hidden by the region already collapsed (start < probe <= end), so overlapping folds (e.g. (1,3)+(3,5) from foldmethod=indent) report their combined span. Folds that merely ABUT — the next fold starts at end + 1, the first visible line after this one — are NOT chained: that fold has an on-screen heading and is a separate fold with its own summary. Shared by the TUI and GPUI renderers so the count stays identical. The source lines a pane actually shows, in paint order: walk from scroll collecting lines until height VISIBLE ones are gathered, skipping every line hidden inside a closed fold and stepping over a closed fold’s body in one jump (its head row stands for the whole range).