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
use crate::{
    pos::Spanned,
    syntax_pos::{BytePos, Span},
};
use dashmap::{mapref::one::Ref, DashMap};
use fxhash::FxBuildHasher;

pub type CommentMap = DashMap<BytePos, Vec<Comment>, FxBuildHasher>;

#[derive(Debug)]
pub struct Comments {
    leading: CommentMap,
    trailing: CommentMap,
}

impl Default for Comments {
    fn default() -> Self {
        fn mk() -> CommentMap {
            DashMap::with_hasher(Default::default())
        }

        Comments {
            leading: mk(),
            trailing: mk(),
        }
    }
}

impl Comments {
    pub fn add_leading(&self, pos: BytePos, cmt: Vec<Comment>) {
        self.leading.entry(pos).or_default().extend(cmt);
    }

    pub fn add_trailing(&self, pos: BytePos, cmt: Comment) {
        self.trailing.entry(pos).or_default().push(cmt);
    }

    pub fn take_trailing_comments(&self, pos: BytePos) -> Option<Vec<Comment>> {
        self.trailing.remove(&pos).map(|v| v.1)
    }

    pub fn trailing_comments(
        &self,
        pos: BytePos,
    ) -> Option<Ref<'_, BytePos, Vec<Comment>, FxBuildHasher>> {
        self.trailing.get(&pos)
    }

    pub fn take_leading_comments(&self, pos: BytePos) -> Option<Vec<Comment>> {
        self.leading.remove(&pos).map(|v| v.1)
    }

    pub fn leading_comments(
        &self,
        pos: BytePos,
    ) -> Option<Ref<'_, BytePos, Vec<Comment>, FxBuildHasher>> {
        self.leading.get(&pos)
    }

    pub fn move_leading(&self, from: BytePos, to: BytePos) {
        let cmt = self.leading.remove(&from);

        if let Some(cmt) = cmt {
            self.leading.entry(to).or_default().extend(cmt.1);
        }
    }

    pub fn move_trailing(&self, from: BytePos, to: BytePos) {
        let cmt = self.trailing.remove(&from);

        if let Some(cmt) = cmt {
            self.trailing.entry(to).or_default().extend(cmt.1);
        }
    }

    pub fn retain_leading(&self, f: impl FnMut(&BytePos, &mut Vec<Comment>) -> bool) {
        self.leading.retain(f);
    }

    pub fn retain_trailing(&self, f: impl FnMut(&BytePos, &mut Vec<Comment>) -> bool) {
        self.trailing.retain(f);
    }

    /// Takes all the comments as (leading, trailing).
    pub fn take_all(self) -> (CommentMap, CommentMap) {
        (self.leading, self.trailing)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Comment {
    pub kind: CommentKind,
    pub span: Span,
    pub text: String,
}

impl Spanned for Comment {
    fn span(&self) -> Span {
        self.span
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommentKind {
    Line,
    Block,
}