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
use std::{
    io,
    path::Path,
    sync::atomic::{AtomicPtr, Ordering},
};

use crate::{buffer_position::BufferPosition, ResourceFile};

pub const HELP_PREFIX: &str = "help://";

pub struct HelpPages {
    pages: &'static [ResourceFile],
    next: AtomicPtr<HelpPages>,
}
impl HelpPages {
    pub const fn new(pages: &'static [ResourceFile]) -> Self {
        Self {
            pages,
            next: AtomicPtr::new(std::ptr::null_mut()),
        }
    }
}

static HELP_PAGES: HelpPages = HelpPages::new(&[
    ResourceFile {
        name: "command_reference.md",
        content: include_str!("../rc/command_reference.md"),
    },
    ResourceFile {
        name: "bindings.md",
        content: include_str!("../rc/bindings.md"),
    },
    ResourceFile {
        name: "language_syntax_definitions.md",
        content: include_str!("../rc/language_syntax_definitions.md"),
    },
    ResourceFile {
        name: "config_recipes.md",
        content: include_str!("../rc/config_recipes.md"),
    },
    ResourceFile {
        name: "help.md",
        content: include_str!("../rc/help.md"),
    },
]);

pub(crate) fn add_help_pages(pages: &'static HelpPages) {
    let pages = pages as *const _ as *mut _;
    let mut current = &HELP_PAGES;
    loop {
        match current.next.compare_exchange(
            std::ptr::null_mut(),
            pages,
            Ordering::Relaxed,
            Ordering::Relaxed,
        ) {
            Ok(_) => break,
            Err(next) => current = unsafe { &*next },
        }
    }
}

pub(crate) fn main_help_name() -> &'static str {
    HELP_PAGES.pages[HELP_PAGES.pages.len() - 1].name
}

pub(crate) fn open(path: &Path) -> Option<impl io::BufRead> {
    let path = match path.to_str().and_then(|p| p.strip_prefix(HELP_PREFIX)) {
        Some(path) => path,
        None => return None,
    };
    for page in HelpPageIterator::new() {
        if path == page.name {
            return Some(io::Cursor::new(page.content));
        }
    }
    None
}

pub(crate) fn search(keyword: &str) -> Option<(&'static str, BufferPosition)> {
    let mut last_match = None;
    for page in HelpPageIterator::new() {
        if keyword == page.name.trim_end_matches(".md") {
            return Some((page.name, BufferPosition::zero()));
        }

        for (line_index, line) in page.content.lines().enumerate() {
            if let Some(column_index) = line.find(keyword) {
                let position = BufferPosition::line_col(line_index as _, column_index as _);
                if line.starts_with('#') {
                    return Some((page.name, position));
                } else {
                    last_match = Some((page.name, position));
                }
            }
        }
    }

    last_match
}

struct HelpPageIterator {
    current: &'static HelpPages,
    index: usize,
}
impl HelpPageIterator {
    pub fn new() -> Self {
        Self {
            current: &HELP_PAGES,
            index: 0,
        }
    }
}
impl Iterator for HelpPageIterator {
    type Item = ResourceFile;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.index < self.current.pages.len() {
                let page = self.current.pages[self.index];
                self.index += 1;
                break Some(page);
            } else {
                let next = self.current.next.load(Ordering::Relaxed);
                if next.is_null() {
                    break None;
                } else {
                    self.current = unsafe { &*next };
                }
            }
        }
    }
}