Lutra/crates/syntax/src/folding.rs
danny a5236acd7e M2.1: tree-sitter PHP in crates/syntax
Parser (parse, incremental reparse from lutra_text edits), outline with
UTF-16 ranges and short signatures, folding ranges, 8 MiB parse cap.
Snapshot tests for five PHP 8.3 fixtures, proptests (random bytes,
reparse equals fresh parse, undo round trip), ignored release timing
test. Docs: architecture Syntax section, parity evidence, G4 note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 22:06:21 +02:00

112 lines
3.7 KiB
Rust

//! Folding ranges of a PHP file.
//!
//! A region folds when it spans at least two lines. Foldable regions are
//! blocks in braces (class, interface, trait and enum bodies, function and
//! method bodies, control-flow blocks, `switch` and `match` bodies), array
//! literals, comments, heredoc and nowdoc strings, grouped `use` imports
//! (`use A\{B, C}`) and runs of two or more consecutive `use` statements.
//!
//! Ranges cover the whole region, delimiters included (`{` to `}`, `/*` to
//! `*/`); the editor decides which part stays visible when folded.
use tree_sitter::{Language, Node};
use crate::{Range, Tree};
/// Node kinds that fold as a whole.
const FOLDABLE: &[&str] = &[
"compound_statement",
"declaration_list",
"enum_declaration_list",
"switch_block",
"match_block",
"array_creation_expression",
"comment",
"heredoc",
"nowdoc",
"namespace_use_group",
];
/// The folding ranges of `tree`, sorted by start, without duplicates.
///
/// Empty for a [`Tree::truncated`] tree. Error nodes never fold, but
/// regions inside them do. The walk is iterative, so deeply nested input
/// cannot exhaust the stack, and it skips the inside of single-line nodes,
/// which cannot contain a multi-line region.
pub fn folding(tree: &Tree) -> Vec<Range> {
let Some(root) = tree.root() else {
return Vec::new();
};
let language: Language = tree_sitter_php::LANGUAGE_PHP.into();
let foldable: Vec<u16> = FOLDABLE
.iter()
.map(|kind| language.id_for_node_kind(kind, true))
.collect();
let use_parents = [
language.id_for_node_kind("program", true),
language.id_for_node_kind("compound_statement", true),
];
let mut bytes = Vec::new();
let mut cursor = root.walk();
'walk: loop {
let node = cursor.node();
if node.end_position().row > node.start_position().row {
let kind = node.kind_id();
if foldable.contains(&kind) && !node.is_error() {
bytes.push((node.start_byte(), node.end_byte()));
}
if use_parents.contains(&kind) {
use_runs(&mut bytes, node);
}
if cursor.goto_first_child() {
continue;
}
}
while !cursor.goto_next_sibling() {
if !cursor.goto_parent() {
break 'walk;
}
}
}
let mut ranges: Vec<Range> = bytes
.into_iter()
.filter_map(|(start, end)| Range::from_bytes(tree.text(), start, end))
.collect();
ranges.sort();
ranges.dedup();
ranges
}
/// Adds the runs of two or more consecutive `use` statements among the
/// children of `node`.
fn use_runs(bytes: &mut Vec<(usize, usize)>, node: Node<'_>) {
let mut cursor = node.walk();
let mut run: Option<(Node<'_>, Node<'_>)> = None;
for child in node.named_children(&mut cursor) {
if child.kind() == "namespace_use_declaration" {
run = Some(match run {
Some((first, _)) => (first, child),
None => (child, child),
});
continue;
}
if let Some((first, last)) = run.take()
&& first.id() != last.id()
{
push_multiline(bytes, first, last);
}
}
if let Some((first, last)) = run
&& first.id() != last.id()
{
push_multiline(bytes, first, last);
}
}
/// Adds the byte range from the start of `first` to the end of `last` when
/// it spans more than one line.
fn push_multiline(bytes: &mut Vec<(usize, usize)>, first: Node<'_>, last: Node<'_>) {
if last.end_position().row > first.start_position().row {
bytes.push((first.start_byte(), last.end_byte()));
}
}