Cotton code
87 lines · 3.4 KB · RustRaw
//! FOLDS — where a note can fold: a heading folds its section (to the next//! heading of the same or a higher level), a list item folds its deeper//! items. The core says where; which are folded is the phone's to keep.//!//! Also the note's CONTENTS: its headings in order, for Info.use serde::{Deserialize, Serialize};use super::style::{self, Kind};use super::Offsets;#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct Fold {    /// `heading` or `list`.    pub kind: String,    /// Where the folding line starts (UTF-16).    pub line: u32,    /// What folding hides: from the end of the folding line to the end of    /// its section (UTF-16).    pub start: u32,    pub end: u32,    #[serde(default, skip_serializing_if = "Option::is_none")]    pub level: Option<u8>,}/// Every fold of `text`, in order of the folding line.pub fn folds(text: &str) -> Vec<Fold> {    let offsets = Offsets::new(text);    let lines = style::lines(text);    let mut out = Vec::new();    for (k, line) in lines.iter().enumerate() {        match line.kind {            Kind::Heading => {                let level = line.level.unwrap_or(1);                let next = lines[k + 1..].iter().position(|later| later.kind == Kind::Heading && later.level.unwrap_or(1) <= level).map(|n| k + 1 + n);                // The section ends at the last line with words before the next heading.                let last = lines[k + 1..next.unwrap_or(lines.len())].iter().rposition(|l| l.kind != Kind::Blank).map(|n| k + 1 + n);                if let Some(last) = last {                    out.push(Fold { kind: "heading".into(), line: offsets.u16(line.start), start: offsets.u16(line.end), end: offsets.u16(lines[last].end), level: Some(level) });                }            }            kind if kind.is_item() => {                let mut last = None;                for later in lines.iter().skip(k + 1) {                    if later.kind.is_item() && later.depth > line.depth || later.kind == Kind::Paragraph && later.depth > line.depth {                        last = Some(later.end);                    } else {                        break;                    }                }                if let Some(end) = last {                    out.push(Fold { kind: "list".into(), line: offsets.u16(line.start), start: offsets.u16(line.end), end: offsets.u16(end), level: None });                }            }            _ => {}        }    }    out}/// One heading of a note's contents.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct Heading {    pub level: u8,    pub words: String,    pub start: u32,    pub end: u32,}/// The headings of `text`, in order, each its plain words.pub fn contents(text: &str) -> Vec<Heading> {    let offsets = Offsets::new(text);    style::lines(text)        .iter()        .filter(|line| line.kind == Kind::Heading)        .filter_map(|line| {            let words = super::derive::plain_words(text, line);            (!words.is_empty()).then(|| Heading { level: line.level.unwrap_or(1), words, start: offsets.u16(line.start), end: offsets.u16(line.end) })        })        .collect()}/// The UTF-16 start of the first heading whose words are `heading` (any case).pub fn heading_at(text: &str, heading: &str) -> Option<u32> {    let wanted = super::links::title_key(heading);    contents(text).into_iter().find(|h| super::links::title_key(&h.words) == wanted).map(|h| h.start)}