foundry_common/comments/
comment.rs

1//! Modified from [`rustc_ast::util::comments`](https://github.com/rust-lang/rust/blob/07d3fd1d9b9c1f07475b96a9d168564bf528db68/compiler/rustc_ast/src/util/comments.rs).
2
3use solar::parse::{
4    ast::{CommentKind, Span},
5    interface::BytePos,
6};
7
8#[derive(Clone, Copy, PartialEq, Debug)]
9pub enum CommentStyle {
10    /// No code on either side of each line of the comment
11    Isolated,
12    /// Code exists to the left of the comment
13    Trailing,
14    /// Code before /* foo */ and after the comment
15    Mixed,
16    /// Just a manual blank line "\n\n", for layout
17    BlankLine,
18}
19
20impl CommentStyle {
21    pub fn is_mixed(&self) -> bool {
22        matches!(self, Self::Mixed)
23    }
24    pub fn is_trailing(&self) -> bool {
25        matches!(self, Self::Trailing)
26    }
27    pub fn is_isolated(&self) -> bool {
28        matches!(self, Self::Isolated)
29    }
30    pub fn is_blank(&self) -> bool {
31        matches!(self, Self::BlankLine)
32    }
33}
34
35#[derive(Clone, Debug)]
36pub struct Comment {
37    pub lines: Vec<String>,
38    pub span: Span,
39    pub style: CommentStyle,
40    pub is_doc: bool,
41    pub kind: CommentKind,
42}
43
44impl Comment {
45    pub fn pos(&self) -> BytePos {
46        self.span.lo()
47    }
48
49    pub fn prefix(&self) -> Option<&'static str> {
50        if self.lines.is_empty() {
51            return None;
52        }
53        Some(match (self.kind, self.is_doc) {
54            (CommentKind::Line, false) => "//",
55            (CommentKind::Line, true) => "///",
56            (CommentKind::Block, false) => "/*",
57            (CommentKind::Block, true) => "/**",
58        })
59    }
60
61    pub fn suffix(&self) -> Option<&'static str> {
62        if self.lines.is_empty() {
63            return None;
64        }
65        match self.kind {
66            CommentKind::Line => None,
67            CommentKind::Block => Some("*/"),
68        }
69    }
70}