Skip to main content

veryl_parser/
doc_comment_table.rs

1use crate::resource_table::{PathId, StrId};
2use std::cell::RefCell;
3use std::collections::HashMap;
4
5#[derive(Clone, Debug, Default)]
6pub struct DocCommentTable {
7    table: HashMap<(PathId, u32), StrId>,
8}
9
10impl DocCommentTable {
11    pub fn insert(&mut self, path: PathId, line: u32, text: StrId) {
12        self.table.insert((path, line), text);
13    }
14
15    pub fn get(&self, path: PathId, line: u32) -> Option<StrId> {
16        self.table.get(&(path, line)).cloned()
17    }
18
19    pub fn export_by_path(&self, path: PathId) -> Vec<(u32, StrId)> {
20        let mut ret: Vec<_> = self
21            .table
22            .iter()
23            .filter(|((p, _), _)| *p == path)
24            .map(|((_, line), text)| (*line, *text))
25            .collect();
26        ret.sort_unstable_by_key(|(line, _)| *line);
27        ret
28    }
29
30    pub fn clear(&mut self) {
31        self.table.clear();
32    }
33}
34
35thread_local!(static DOC_COMMENT_TABLE: RefCell<DocCommentTable> = RefCell::new(DocCommentTable::default()));
36
37pub fn insert(path: PathId, line: u32, text: StrId) {
38    DOC_COMMENT_TABLE.with(|f| f.borrow_mut().insert(path, line, text))
39}
40
41pub fn get(path: PathId, line: u32) -> Option<StrId> {
42    DOC_COMMENT_TABLE.with(|f| f.borrow().get(path, line))
43}
44
45/// Exports all doc comments belonging to one file, sorted by line.
46/// Used by fragment caching.
47pub fn export_by_path(path: PathId) -> Vec<(u32, StrId)> {
48    DOC_COMMENT_TABLE.with(|f| f.borrow().export_by_path(path))
49}
50
51pub fn clear() {
52    DOC_COMMENT_TABLE.with(|f| f.borrow_mut().clear())
53}