use std::{cell::RefCell, rc::Rc, sync::atomic::AtomicU32};
use swc_common::{
BytePos, DUMMY_SP, Span,
comments::{Comment, CommentKind, Comments, SingleThreadedComments},
};
static COMMENT_TICKER: AtomicU32 = AtomicU32::new(0);
thread_local! {
static COMMENTS_MANAGER: RefCell<Rc<SingleThreadedComments>> = RefCell::new(Rc::new(SingleThreadedComments::default()));
}
pub fn create_comment_span(
comment: Option<&str>,
) -> Span {
let Some(comment) = comment else {
return DUMMY_SP;
};
let id = COMMENT_TICKER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let span = Span::new(BytePos(id), BytePos(id + 1));
COMMENTS_MANAGER.with_borrow_mut(|comments| {
comments.add_leading(
span.lo,
Comment {
kind: CommentKind::Line,
span,
text: comment.into(),
},
);
});
span
}
pub fn get_comments() -> Rc<SingleThreadedComments> {
COMMENTS_MANAGER.with_borrow(Rc::clone)
}