foundry_common/comments/
comment.rs1use solar::parse::{
4 ast::{CommentKind, Span},
5 interface::BytePos,
6};
7
8#[derive(Clone, Copy, PartialEq, Debug)]
9pub enum CommentStyle {
10 Isolated,
12 Trailing,
14 Mixed,
16 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}