pagesift-syntax 1.8.1

Code outline extraction and pure-Rust tree-sitter runtime for PageSift (re-exports yolowsyntax).
Documentation
use std::thread;

use crate::{Limits, Section, api, langs::Lang, outline_with_budget, sanitize_sections};

#[test]
fn parse_with_budget_handles_zero_deadline_and_clamped_limits() {
    let lang = Lang::Rust.table().expect("rust grammar table");
    let code = b"pub fn add(a: i32, b: i32) -> i32 { a + b }";

    // deadline_ms == Some(0) must force max_nodes: 0 -> truncated = true
    let zero_tree = api::parse_with_budget(lang, code, Limits::default(), Some(0));
    assert!(zero_tree.truncated);

    // Positive deadline_ms clamps Limits::max_millis (0 -> 500) and parses small input cleanly
    let ok_tree = api::parse_with_budget(lang, code, Limits::default(), Some(500));
    assert!(!ok_tree.truncated);
}

#[test]
fn outline_with_budget_enforces_input_size_and_zero_deadline() {
    let src = "pub fn alpha() {}\npub fn beta() {}\n";
    assert!(outline_with_budget("rust", src, src.len(), Some(0)).is_none());
    assert!(outline_with_budget("rust", "", 1024, None).is_none());
    assert!(outline_with_budget("rust", src, src.len() - 1, None).is_none());
    assert!(outline_with_budget("unknown_lang", src, 1024, None).is_none());

    let out = outline_with_budget("rust", src, 1024, Some(3000)).unwrap();
    assert!(!out.symbols.is_empty());
    assert_eq!(out.sections.first().unwrap().start, 0);
    assert_eq!(out.sections.last().unwrap().end, src.len());
}

#[test]
fn sanitize_sections_compacts_inplace_and_aligns_utf8_boundaries() {
    let s = "fn a() {} // 中文🦀\nfn b() {}";
    // Empty sections -> full span
    let mut empty = Vec::new();
    sanitize_sections(s, &mut empty);
    assert_eq!(empty.len(), 1);
    assert_eq!((empty[0].start, empty[0].end), (0, s.len()));

    // Mid-UTF-8 byte offsets and overlapping sections
    let mid_cjk = s.find('中').unwrap() + 1;
    let mid_crab = s.find('🦀').unwrap() + 2;
    let mut secs = vec![
        Section {
            start: 0,
            end: 0,
            title: Some("dropped".into()),
        },
        Section {
            start: 0,
            end: mid_cjk,
            title: Some("fn a".into()),
        },
        Section {
            start: mid_cjk,
            end: mid_cjk,
            title: Some("duplicate".into()),
        },
        Section {
            start: mid_cjk,
            end: mid_crab,
            title: Some("fn b".into()),
        },
    ];
    sanitize_sections(s, &mut secs);
    assert_eq!(secs.len(), 2);
    assert_eq!(secs[0].start, 0);
    assert!(s.is_char_boundary(secs[0].end));
    assert_eq!(secs[0].end, secs[1].start);
    assert_eq!(secs[1].end, s.len());
    assert_eq!(secs[0].title.as_deref(), Some("fn a"));
    assert_eq!(secs[1].title.as_deref(), Some("fn b"));

    // All sections <= 0 -> collapses to single 0..len section
    let mut all_dropped = vec![Section {
        start: 0,
        end: 0,
        title: Some("x".into()),
    }];
    sanitize_sections(s, &mut all_dropped);
    assert_eq!(all_dropped.len(), 1);
    assert_eq!((all_dropped[0].start, all_dropped[0].end), (0, s.len()));
    assert!(all_dropped[0].title.is_none());
}

#[test]
fn concurrent_syntax_budget_stress() {
    let lang = Lang::Rust.table().expect("rust grammar table");
    let src = "pub fn compute(x: u64) -> u64 { x.wrapping_mul(3) }\n// 注释🦀\npub struct Item { v: u64 }\n";
    let mut handles = Vec::new();
    for tid in 0..6usize {
        handles.push(thread::spawn(move || {
            for iter in 0..20usize {
                if (tid + iter) % 3 == 0 {
                    let t =
                        api::parse_with_budget(lang, src.as_bytes(), Limits::default(), Some(0));
                    assert!(t.truncated);
                } else {
                    let o = outline_with_budget("rust", src, 8192, Some(3000)).unwrap();
                    assert_eq!(o.sections.last().unwrap().end, src.len());
                }
            }
        }));
    }
    for h in handles {
        h.join().unwrap();
    }
}