1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/// FoldMap: Folding projection layer (Wrap rows → Display rows).
///
/// This module manages code folding by:
/// - Filtering out wrap rows that belong to folded regions
/// - Maintaining bidirectional mapping: wrap_row ↔ display_row
/// - Handling fold state changes and rebuilding the projection
use std::ops::Range;
use super::folding::FoldRange;
use super::wrap_map::WrapMap;
/// A run of wrap rows hidden by folding.
#[derive(Debug, Clone, PartialEq, Eq)]
struct HiddenRows {
wrap_rows: Range<usize>,
/// How many wrap rows the runs before this one hide.
hidden_before: usize,
}
impl HiddenRows {
/// The display row of the first visible wrap row after this run.
fn display_start(&self) -> usize {
self.wrap_rows.start - self.hidden_before
}
}
/// FoldMap projects wrap rows to display rows by hiding folded regions.
pub(super) struct FoldMap {
/// The wrap rows hidden by folding: sorted, disjoint and non-adjacent.
///
/// The projection is kept as these runs rather than a table per wrap row,
/// so rebuilding it after an edit costs the number of folds, not the
/// length of the document, and both directions are a binary search.
hidden: Vec<HiddenRows>,
/// How many wrap rows `hidden` hides in total.
total_hidden: usize,
/// The wrap row count the projection was last built for.
projected_wrap_row_count: usize,
/// Candidate fold ranges (from tree-sitter/LSP)
/// Sorted by start_line, unique start_line
candidates: Vec<FoldRange>,
/// Currently folded ranges
/// Subset of candidates, sorted by start_line
folded: Vec<FoldRange>,
/// Flag indicating if the fold projection needs rebuilding
/// Used for lazy evaluation to avoid expensive rebuilds on every text change
needs_rebuild: bool,
/// Cached wrap_row_count from last rebuild
/// Used to detect if WrapMap changed and rebuild is needed
cached_wrap_row_count: usize,
}
impl FoldMap {
pub(super) fn new() -> Self {
Self {
hidden: Vec::new(),
total_hidden: 0,
projected_wrap_row_count: 0,
candidates: Vec::new(),
folded: Vec::new(),
needs_rebuild: true,
cached_wrap_row_count: 0,
}
}
/// Update cached wrap_row_count without full rebuild.
/// Used when no folds are active (identity mapping assumed).
pub(super) fn mark_dirty_with_wrap_count(&mut self, wrap_row_count: usize) {
self.needs_rebuild = true;
self.cached_wrap_row_count = wrap_row_count;
}
/// Get total number of visible display rows
pub(super) fn display_row_count(&self) -> usize {
if self.folded.is_empty() {
return self.cached_wrap_row_count;
}
self.projected_wrap_row_count - self.total_hidden
}
/// How many wrap rows the runs before `index` hide.
fn hidden_before(&self, index: usize) -> usize {
self.hidden
.get(index)
.map_or(self.total_hidden, |run| run.hidden_before)
}
/// Convert wrap_row to display_row
/// Returns None if the wrap_row is hidden by folding
pub(super) fn wrap_row_to_display_row(&self, wrap_row: usize) -> Option<usize> {
if self.folded.is_empty() {
return if wrap_row < self.cached_wrap_row_count {
Some(wrap_row)
} else {
None
};
}
if wrap_row >= self.projected_wrap_row_count {
return None;
}
let index = self
.hidden
.partition_point(|run| run.wrap_rows.end <= wrap_row);
if self
.hidden
.get(index)
.is_some_and(|run| run.wrap_rows.start <= wrap_row)
{
return None;
}
Some(wrap_row - self.hidden_before(index))
}
/// Convert display_row to wrap_row
pub(super) fn display_row_to_wrap_row(&self, display_row: usize) -> Option<usize> {
if self.folded.is_empty() {
return if display_row < self.cached_wrap_row_count {
Some(display_row)
} else {
None
};
}
if display_row >= self.display_row_count() {
return None;
}
let index = self
.hidden
.partition_point(|run| run.display_start() <= display_row);
Some(display_row + self.hidden_before(index))
}
/// Find the nearest visible display_row for a given wrap_row
pub(super) fn nearest_visible_display_row(&self, wrap_row: usize) -> usize {
if self.folded.is_empty() {
return wrap_row.min(self.cached_wrap_row_count.saturating_sub(1));
}
if let Some(dr) = self.wrap_row_to_display_row(wrap_row) {
return dr;
}
// A hidden row maps to the last visible row before it.
let visible_before = if wrap_row >= self.projected_wrap_row_count {
self.display_row_count()
} else {
let index = self
.hidden
.partition_point(|run| run.wrap_rows.end <= wrap_row);
self.hidden
.get(index)
.map_or(self.display_row_count(), HiddenRows::display_start)
};
visible_before.saturating_sub(1)
}
/// Set fold candidates (from tree-sitter/LSP), full replacement.
pub(super) fn set_candidates(&mut self, mut candidates: Vec<FoldRange>) {
// Sort and deduplicate by start_line
candidates.sort_by_key(|r| r.start_line);
candidates.dedup_by_key(|r| r.start_line);
self.candidates = candidates;
// Remove any folded ranges that are no longer in candidates
self.folded.retain(|fold| {
self.candidates
.iter()
.any(|c| c.start_line == fold.start_line)
});
}
/// Merge new candidates extracted from an edited region into existing candidates.
///
/// Replaces candidates within [edit_start_line, edit_end_line] with `new_candidates`,
/// keeping candidates outside the edit range intact.
pub(super) fn merge_candidates_for_edit(
&mut self,
edit_start_line: usize,
edit_end_line: usize,
new_candidates: Vec<FoldRange>,
) {
// Remove old candidates within the edit range (already done by adjust_folds_for_edit)
// But do it again in case adjust wasn't called or range differs
self.candidates
.retain(|c| c.start_line < edit_start_line || c.start_line > edit_end_line);
// Add new candidates
self.candidates.extend(new_candidates);
self.candidates.sort_by_key(|r| r.start_line);
self.candidates.dedup_by_key(|r| r.start_line);
}
/// Set a fold at the given start_line (must be in candidates)
pub(super) fn set_folded(&mut self, start_line: usize, folded: bool) {
if folded {
// Find the candidate range for this start_line
if let Some(candidate) = self.candidates.iter().find(|c| c.start_line == start_line) {
// Add to folded if not already present
if !self.folded.iter().any(|f| f.start_line == start_line) {
self.folded.push(*candidate);
self.folded.sort_by_key(|r| r.start_line);
self.needs_rebuild = true;
}
}
} else {
// Remove from folded
self.folded.retain(|f| f.start_line != start_line);
self.needs_rebuild = true;
}
}
/// Toggle fold at the given start_line
pub(super) fn toggle_fold(&mut self, start_line: usize) {
let is_folded = self.is_folded_at(start_line);
self.set_folded(start_line, !is_folded);
}
/// Check if a line is currently folded
pub(super) fn is_folded_at(&self, start_line: usize) -> bool {
self.folded.iter().any(|f| f.start_line == start_line)
}
/// Check if a line is a fold candidate
pub(super) fn is_fold_candidate(&self, start_line: usize) -> bool {
self.candidates.iter().any(|c| c.start_line == start_line)
}
/// Get all fold candidates
#[inline]
pub(super) fn fold_candidates(&self) -> &[FoldRange] {
&self.candidates
}
/// Get all currently folded ranges
#[inline]
pub(super) fn folded_ranges(&self) -> &[FoldRange] {
&self.folded
}
/// Clear all folds
#[inline]
pub(super) fn clear_folds(&mut self) {
self.folded.clear();
}
/// Adjust folds and candidates after a text edit.
///
/// - Folds/candidates overlapping the edited line range are removed
/// - Folds/candidates after the edit are shifted by line_delta
///
/// This avoids expensive full tree traversal on every keystroke.
pub(super) fn adjust_folds_for_edit(
&mut self,
edit_start_line: usize,
edit_end_line: usize,
line_delta: isize,
) {
// Adjust folded ranges
if !self.folded.is_empty() {
self.folded.retain(|fold| {
!(fold.start_line <= edit_end_line && fold.end_line >= edit_start_line)
});
if line_delta != 0 {
for fold in &mut self.folded {
if fold.start_line > edit_end_line {
fold.start_line = (fold.start_line as isize + line_delta).max(0) as usize;
fold.end_line = (fold.end_line as isize + line_delta).max(0) as usize;
}
}
}
}
// Adjust candidates the same way
if !self.candidates.is_empty() {
self.candidates
.retain(|c| !(c.start_line <= edit_end_line && c.end_line >= edit_start_line));
if line_delta != 0 {
for c in &mut self.candidates {
if c.start_line > edit_end_line {
c.start_line = (c.start_line as isize + line_delta).max(0) as usize;
c.end_line = (c.end_line as isize + line_delta).max(0) as usize;
}
}
}
}
self.needs_rebuild = true;
}
/// Rebuild the fold mapping after wrap_map or fold state changes
///
/// This is the core algorithm that projects wrap rows to display rows.
pub(super) fn rebuild(&mut self, wrap_map: &WrapMap) {
let wrap_row_count = wrap_map.wrap_row_count();
// Performance optimization: skip rebuild if nothing changed
if !self.needs_rebuild && wrap_row_count == self.cached_wrap_row_count {
return;
}
self.cached_wrap_row_count = wrap_row_count;
if self.folded.is_empty() {
// Fast path: no folds, all wrap rows are visible
self.set_hidden_rows(wrap_row_count, Vec::new());
self.needs_rebuild = false;
return;
}
// Build set of hidden wrap_row ranges from folded buffer lines
let mut hidden_ranges = Vec::new();
for fold in &self.folded {
// Hide wrap rows from (start_line + 1) to (end_line - 1) (inclusive)
// Both the first line and last line of the fold remain visible
let hide_start_line = fold.start_line + 1;
let hide_end_line = fold.end_line.saturating_sub(1);
if hide_start_line > hide_end_line {
continue; // No middle lines to hide (0 or 1 lines between start and end)
}
// Get wrap_row ranges for the hidden buffer lines
let start_wrap_row = wrap_map.buffer_line_to_first_wrap_row(hide_start_line);
let end_wrap_row = if hide_end_line + 1 < wrap_map.buffer_line_count() {
wrap_map.buffer_line_to_first_wrap_row(hide_end_line + 1)
} else {
wrap_row_count
};
if start_wrap_row < end_wrap_row {
hidden_ranges.push(start_wrap_row..end_wrap_row);
}
}
self.set_hidden_rows(wrap_row_count, hidden_ranges);
self.needs_rebuild = false;
}
/// Install the projection for `wrap_row_count` wrap rows with
/// `hidden_ranges` hidden, merging overlapping and adjacent ranges.
fn set_hidden_rows(&mut self, wrap_row_count: usize, mut hidden_ranges: Vec<Range<usize>>) {
hidden_ranges.sort_by_key(|range| range.start);
self.hidden.clear();
self.total_hidden = 0;
self.projected_wrap_row_count = wrap_row_count;
for range in hidden_ranges {
let range = range.start..range.end.min(wrap_row_count);
if range.is_empty() {
continue;
}
if let Some(last) = self.hidden.last_mut()
&& range.start <= last.wrap_rows.end
{
// Overlapping or adjacent, merge
if range.end > last.wrap_rows.end {
self.total_hidden += range.end - last.wrap_rows.end;
last.wrap_rows.end = range.end;
}
continue;
}
self.total_hidden += range.len();
self.hidden.push(HiddenRows {
hidden_before: self.total_hidden - range.len(),
wrap_rows: range,
});
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The per-row tables the projection used to be stored as: display row to
/// wrap row, and wrap row to display row.
fn dense_projection(
wrap_row_count: usize,
hidden: &[Range<usize>],
) -> (Vec<usize>, Vec<Option<usize>>) {
let mut visible = Vec::new();
let mut display_rows = vec![None; wrap_row_count];
for (wrap_row, display_row) in display_rows.iter_mut().enumerate() {
if !hidden.iter().any(|range| range.contains(&wrap_row)) {
*display_row = Some(visible.len());
visible.push(wrap_row);
}
}
(visible, display_rows)
}
#[test]
fn hidden_runs_match_the_dense_projection() {
let wrap_row_count = 10;
let cases: &[&[Range<usize>]] = &[
&[],
&[0..3],
&[2..5],
&[7..10],
&[2..4, 4..6],
&[1..5, 3..8],
&[6..9, 1..3],
&[0..2, 3..4, 8..20],
&[0..10],
];
for &hidden in cases {
let mut fold_map = FoldMap::new();
fold_map.folded.push(FoldRange::new(0, 1));
fold_map.set_hidden_rows(wrap_row_count, hidden.to_vec());
let (visible, display_rows) = dense_projection(wrap_row_count, hidden);
assert_eq!(fold_map.display_row_count(), visible.len(), "{hidden:?}");
for wrap_row in 0..wrap_row_count + 2 {
let display_row = display_rows.get(wrap_row).copied().flatten();
assert_eq!(
fold_map.wrap_row_to_display_row(wrap_row),
display_row,
"{hidden:?}, wrap row {wrap_row}"
);
let nearest = match visible.binary_search(&wrap_row) {
Ok(index) => index,
Err(index) => index.saturating_sub(1),
};
assert_eq!(
fold_map.nearest_visible_display_row(wrap_row),
nearest,
"{hidden:?}, wrap row {wrap_row}"
);
}
for display_row in 0..wrap_row_count + 2 {
assert_eq!(
fold_map.display_row_to_wrap_row(display_row),
visible.get(display_row).copied(),
"{hidden:?}, display row {display_row}"
);
}
}
}
}