use std::collections::BTreeMap;
use oxc_span::Span;
pub type Trivias = Vec<(u32, u32, CommentKind)>;
#[derive(Debug, Default)]
pub struct TriviasMap {
comments: BTreeMap<u32, Comment>,
}
impl From<Trivias> for TriviasMap {
fn from(trivias: Trivias) -> Self {
Self { comments: trivias.iter().map(|t| (t.0, Comment::new(t.1, t.2))).collect() }
}
}
#[derive(Debug, Clone, Copy)]
#[allow(unused)]
pub struct Comment {
kind: CommentKind,
end: u32,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CommentKind {
SingleLine,
MultiLine,
}
impl Comment {
pub fn new(end: u32, kind: CommentKind) -> Self {
Self { kind, end }
}
pub fn end(self) -> u32 {
self.end
}
pub fn is_single_line(self) -> bool {
matches!(self.kind, CommentKind::SingleLine)
}
pub fn is_multi_line(self) -> bool {
matches!(self.kind, CommentKind::MultiLine)
}
}
impl TriviasMap {
pub fn comments(&self) -> &BTreeMap<u32, Comment> {
&self.comments
}
pub fn has_comments_between(&self, span: Span) -> bool {
self.comments.range(span.start..span.end).count() > 0
}
pub fn add_single_line_comment(&mut self, span: Span) {
let comment = Comment::new(span.end, CommentKind::SingleLine);
self.comments.insert(span.start, comment);
}
pub fn add_multi_line_comment(&mut self, span: Span) {
let comment = Comment::new(span.end, CommentKind::MultiLine);
self.comments.insert(span.start, comment);
}
}