Compute folding ranges for the given file content. Re-parses the source with `mago_syntax` (the raw AST is not cached) and walks every statement/expression to emit `FoldingRange` entries.
(&self, content: &str)
| 20 | /// Re-parses the source with `mago_syntax` (the raw AST is not cached) |
| 21 | /// and walks every statement/expression to emit `FoldingRange` entries. |
| 22 | pub fn handle_folding_range(&self, content: &str) -> Option<Vec<FoldingRange>> { |
| 23 | let arena = Bump::new(); |
| 24 | let file_id = mago_database::file::FileId::new("input.php"); |
| 25 | let program = mago_syntax::parser::parse_file_content(&arena, file_id, content); |
| 26 | |
| 27 | let mut ranges: Vec<FoldingRange> = Vec::new(); |
| 28 | |
| 29 | // ── AST walk ── |
| 30 | for stmt in program.statements.iter() { |
| 31 | collect_from_statement(stmt, content, &mut ranges); |
| 32 | } |
| 33 | |
| 34 | // ── Trivia (comments) ── |
| 35 | collect_comment_ranges(&program.trivia, content, &mut ranges); |
| 36 | |
| 37 | // Filter out single-line ranges and sort by start position. |
| 38 | ranges.retain(|r| r.start_line < r.end_line); |
| 39 | ranges.sort_by(|a, b| { |
| 40 | a.start_line |
| 41 | .cmp(&b.start_line) |
| 42 | .then(a.end_line.cmp(&b.end_line)) |
| 43 | }); |
| 44 | |
| 45 | Some(ranges) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // ─── Helpers ──────────────────────────────────────────────────────────────── |
no test coverage detected