use std::ops::Range;
use rustaco_core::Rope;
use crate::HeightMap;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Fold {
pub start: usize,
pub end: usize,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FoldMarker {
pub line: usize,
pub y: f32,
pub height: f32,
pub folded: bool,
}
#[derive(Clone, Debug, Default)]
pub(crate) struct Hidden {
pub ranges: Vec<Range<usize>>,
shown_top: Vec<f64>,
hidden_through: Vec<f64>,
}
impl Hidden {
pub fn new(mut ranges: Vec<Range<usize>>, heights: &HeightMap) -> Self {
ranges.sort_by_key(|r| r.start);
let mut merged: Vec<Range<usize>> = Vec::new();
for r in ranges {
match merged.last_mut() {
Some(last) if r.start <= last.end => last.end = last.end.max(r.end),
_ => merged.push(r),
}
}
let mut hidden = Self { ranges: Vec::new(), shown_top: Vec::new(), hidden_through: Vec::new() };
hidden.ranges = merged;
hidden.measure(heights);
hidden
}
pub fn measure(&mut self, heights: &HeightMap) {
let len = heights.len();
self.ranges.retain(|r| r.start < len);
for r in &mut self.ranges {
r.end = r.end.min(len);
}
self.shown_top.clear();
self.hidden_through.clear();
let mut acc = 0.0;
for r in &self.ranges {
let top = heights.top(r.start);
self.shown_top.push(top - acc);
acc += heights.top(r.end) - top;
self.hidden_through.push(acc);
}
}
pub fn run_of(&self, line: usize) -> Option<usize> {
let i = self.ranges.partition_point(|r| r.end <= line);
self.ranges.get(i).filter(|r| r.start <= line).map(|_| i)
}
pub fn top(&self, heights: &HeightMap, line: usize) -> f64 {
let i = self.ranges.partition_point(|r| r.end <= line);
if self.ranges.get(i).is_some_and(|r| r.start <= line) {
return self.shown_top[i];
}
heights.top(line) - if i == 0 { 0.0 } else { self.hidden_through[i - 1] }
}
pub fn line_at(&self, heights: &HeightMap, y: f64) -> usize {
let i = self.shown_top.partition_point(|&t| t <= y);
let natural = y + if i == 0 { 0.0 } else { self.hidden_through[i - 1] };
heights.line_at(natural)
}
pub fn total(&self, heights: &HeightMap) -> f64 {
heights.total() - self.hidden_through.last().copied().unwrap_or(0.0)
}
}
pub(crate) fn shift_foldable(foldable: &mut Vec<(usize, usize)>, a: usize, old: usize, new: usize) {
let shift = |l: usize| if l > a + old { l + new - old } else { l.min(a + new) };
foldable.retain_mut(|(s, e)| {
if *s > a && *s <= a + old {
return false;
}
*s = shift(*s);
*e = shift(*e);
*e > *s
});
}
pub(crate) fn shift_lines(r: &mut Range<usize>, a: usize, old: usize, new: usize) {
let shift = |l: usize| if l > a + old { l + new - old } else { l.min(a + new) };
r.start = shift(r.start);
r.end = shift(r.end.saturating_sub(1)) + 1;
}
pub fn indent_fold_ranges(rope: &Rope, tab: usize) -> Vec<(usize, usize)> {
let tab = tab.max(1);
let total = rope.len_lines();
let mut out = Vec::new();
let mut open: Vec<(usize, usize)> = Vec::new();
let mut last_nonblank = 0;
for line in 0..total {
let Some(cells) = indent_cells(rope, line, tab) else { continue };
while let Some(&(h, ind)) = open.last() {
if cells > ind {
break;
}
open.pop();
if last_nonblank > h {
out.push((h, last_nonblank));
}
}
open.push((line, cells));
last_nonblank = line;
}
while let Some((h, _)) = open.pop() {
if last_nonblank > h {
out.push((h, last_nonblank));
}
}
out.sort_unstable();
out
}
fn indent_cells(rope: &Rope, line: usize, tab: usize) -> Option<usize> {
let mut cells = 0;
for c in rope.line(line).chars() {
match c {
' ' => cells += 1,
'\t' => cells += tab - cells % tab,
'\n' | '\r' => return None,
_ => return Some(cells),
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hidden_runs_take_no_space() {
let heights = HeightMap::new(10, 20.0);
let hidden = Hidden::new(vec![3..5, 4..7], &heights);
assert_eq!(hidden.ranges, vec![3..7]);
assert_eq!(hidden.top(&heights, 2), 40.0);
assert_eq!(hidden.top(&heights, 5), 60.0);
assert_eq!(hidden.top(&heights, 7), 60.0);
assert_eq!(hidden.top(&heights, 8), 80.0);
assert_eq!(hidden.line_at(&heights, 59.0), 2);
assert_eq!(hidden.line_at(&heights, 60.0), 7);
assert_eq!(hidden.total(&heights), 120.0);
}
#[test]
fn indentation_gives_fold_ranges() {
let rope = Rope::from_str("a\n b\n\n c\n d\ne\n f\n");
assert_eq!(indent_fold_ranges(&rope, 4), vec![(0, 4), (3, 4), (5, 6)]);
}
}