use super::folding::FoldRegion;
use crate::canvas_editor::CodeEditor;
pub(crate) const DEFAULT_MAX_STICKY_LINES: usize = 5;
pub(crate) fn sticky_headers(
regions: &[FoldRegion],
top_line: usize,
max_lines: usize,
) -> Vec<usize> {
if max_lines == 0 {
return Vec::new();
}
regions
.iter()
.filter(|region| {
region.start_line < top_line && top_line <= region.end_line
})
.map(|region| region.start_line)
.take(max_lines)
.collect()
}
impl CodeEditor {
pub(crate) fn sticky_headroom(&self, line: usize) -> usize {
if !self.sticky_scroll_enabled {
return 0;
}
sticky_headers(&self.block_regions(), line, DEFAULT_MAX_STICKY_LINES)
.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn nested_regions() -> Vec<FoldRegion> {
vec![FoldRegion::new(0, 4), FoldRegion::new(1, 4)]
}
const NESTED_SOURCE: &str =
"fn main() {\n if a {\n b();\n c();\n }\n}";
#[test]
fn test_no_headers_without_regions() {
assert!(sticky_headers(&[], 10, DEFAULT_MAX_STICKY_LINES).is_empty());
}
#[test]
fn test_nested_headers_are_outermost_first() {
let regions = nested_regions();
assert_eq!(
sticky_headers(®ions, 3, DEFAULT_MAX_STICKY_LINES),
vec![0, 1]
);
}
#[test]
fn test_visible_header_is_not_pinned() {
let regions = nested_regions();
assert_eq!(
sticky_headers(®ions, 1, DEFAULT_MAX_STICKY_LINES),
vec![0]
);
assert!(
sticky_headers(®ions, 0, DEFAULT_MAX_STICKY_LINES).is_empty()
);
}
#[test]
fn test_line_past_region_end_is_not_enclosed() {
let regions = nested_regions();
assert!(
sticky_headers(®ions, 5, DEFAULT_MAX_STICKY_LINES).is_empty()
);
}
#[test]
fn test_last_line_of_region_is_enclosed() {
let regions = nested_regions();
assert_eq!(
sticky_headers(®ions, 4, DEFAULT_MAX_STICKY_LINES),
vec![0, 1]
);
}
#[test]
fn test_truncation_keeps_outermost_scopes() {
let regions = vec![
FoldRegion::new(0, 9),
FoldRegion::new(1, 9),
FoldRegion::new(2, 9),
FoldRegion::new(3, 9),
];
assert_eq!(sticky_headers(®ions, 8, 2), vec![0, 1]);
}
#[test]
fn test_zero_max_lines_pins_nothing() {
let regions = nested_regions();
assert!(sticky_headers(®ions, 3, 0).is_empty());
}
#[test]
fn test_headroom_is_zero_for_an_outermost_header() {
let editor = CodeEditor::new(NESTED_SOURCE, "rs");
assert_eq!(editor.sticky_headroom(0), 0);
}
#[test]
fn test_headroom_reserves_a_row_for_a_nested_header() {
let editor = CodeEditor::new(NESTED_SOURCE, "rs");
assert_eq!(editor.sticky_headroom(1), 1);
}
#[test]
fn test_headroom_counts_every_enclosing_block() {
let editor = CodeEditor::new(NESTED_SOURCE, "rs");
assert_eq!(editor.sticky_headroom(3), 2);
}
#[test]
fn test_headroom_survives_code_folding_being_disabled() {
let mut editor = CodeEditor::new(NESTED_SOURCE, "rs");
assert_eq!(editor.sticky_headroom(3), 2);
editor.set_folding_enabled(false);
assert_eq!(
editor.sticky_headroom(3),
2,
"sticky scroll must not follow the folding toggle"
);
}
#[test]
fn test_headroom_is_zero_when_sticky_scroll_is_off() {
let mut editor = CodeEditor::new(NESTED_SOURCE, "rs");
assert_eq!(editor.sticky_headroom(3), 2);
editor.set_sticky_scroll_enabled(false);
assert_eq!(editor.sticky_headroom(3), 0);
}
#[test]
fn test_sibling_region_is_not_pinned() {
let regions = vec![FoldRegion::new(0, 2), FoldRegion::new(3, 5)];
assert_eq!(
sticky_headers(®ions, 4, DEFAULT_MAX_STICKY_LINES),
vec![3]
);
}
}