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
20#[derive(Clone, Debug)]
21pub struct Comment {
22    pub lines: Vec<String>,
23    pub span: Span,
24    pub style: CommentStyle,
25    pub is_doc: bool,
26    pub kind: CommentKind,
27}
28
29impl Comment {
30    pub fn pos(&self) -> BytePos {
31        self.span.lo()
32    }
33
34    pub fn prefix(&self) -> Option<&'static str> {
35        if self.lines.is_empty() {
36            return None;
37        }
38        Some(match (self.kind, self.is_doc) {
39            (CommentKind::Line, false) => "//",
40            (CommentKind::Line, true) => "///",
41            (CommentKind::Block, false) => "/*",
42            (CommentKind::Block, true) => "/**",
43        })
44    }
45
46    pub fn suffix(&self) -> Option<&'static str> {
47        if self.lines.is_empty() {
48            return None;
49        }
50        match self.kind {
51            CommentKind::Line => None,
52            CommentKind::Block => Some("*/"),
53        }
54    }
55}