diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-02 10:34:23 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-02 10:34:23 +0100 |
commit | 370dbc7867719662f26db5684ac82cfc25b5b8cb (patch) | |
tree | 70c74389382cd3c8ce72dc7739dbe5502f9f7c8b /crates/ra_syntax/src/ast/tokens.rs | |
parent | d21a677715196c46b73017acbae0105ef554284d (diff) | |
parent | 5287a2506c19b7f886556f8a73510f57dd234857 (diff) |
Merge #1087
1087: More future-proof comment kind r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast/tokens.rs')
-rw-r--r-- | crates/ra_syntax/src/ast/tokens.rs | 83 |
1 files changed, 51 insertions, 32 deletions
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 76a12cd64..7c30ff15c 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs | |||
@@ -21,52 +21,71 @@ impl<'a> AstToken<'a> for Comment<'a> { | |||
21 | } | 21 | } |
22 | 22 | ||
23 | impl<'a> Comment<'a> { | 23 | impl<'a> Comment<'a> { |
24 | pub fn flavor(&self) -> CommentFlavor { | 24 | pub fn kind(&self) -> CommentKind { |
25 | let text = self.text(); | 25 | kind_by_prefix(self.text()) |
26 | if text.starts_with("///") { | ||
27 | CommentFlavor::OuterDoc | ||
28 | } else if text.starts_with("//!") { | ||
29 | CommentFlavor::InnerDoc | ||
30 | } else if text.starts_with("//") { | ||
31 | CommentFlavor::Line | ||
32 | } else { | ||
33 | CommentFlavor::Multiline | ||
34 | } | ||
35 | } | ||
36 | |||
37 | pub fn is_doc_comment(&self) -> bool { | ||
38 | self.flavor().is_doc_comment() | ||
39 | } | 26 | } |
40 | 27 | ||
41 | pub fn prefix(&self) -> &'static str { | 28 | pub fn prefix(&self) -> &'static str { |
42 | self.flavor().prefix() | 29 | prefix_by_kind(self.kind()) |
43 | } | 30 | } |
44 | } | 31 | } |
45 | 32 | ||
46 | #[derive(Debug, PartialEq, Eq)] | 33 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
47 | pub enum CommentFlavor { | 34 | pub struct CommentKind { |
35 | pub shape: CommentShape, | ||
36 | pub doc: Option<CommentPlacement>, | ||
37 | } | ||
38 | |||
39 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
40 | pub enum CommentShape { | ||
48 | Line, | 41 | Line, |
49 | OuterDoc, | 42 | Block, |
50 | InnerDoc, | ||
51 | Multiline, | ||
52 | } | 43 | } |
53 | 44 | ||
54 | impl CommentFlavor { | 45 | impl CommentShape { |
55 | pub fn prefix(&self) -> &'static str { | 46 | pub fn is_line(self) -> bool { |
56 | match *self { | 47 | self == CommentShape::Line |
57 | CommentFlavor::Line => "//", | 48 | } |
58 | CommentFlavor::OuterDoc => "///", | 49 | |
59 | CommentFlavor::InnerDoc => "//!", | 50 | pub fn is_block(self) -> bool { |
60 | CommentFlavor::Multiline => "/*", | 51 | self == CommentShape::Block |
52 | } | ||
53 | } | ||
54 | |||
55 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
56 | pub enum CommentPlacement { | ||
57 | Inner, | ||
58 | Outer, | ||
59 | } | ||
60 | |||
61 | const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = { | ||
62 | use {CommentShape::*, CommentPlacement::*}; | ||
63 | &[ | ||
64 | ("///", CommentKind { shape: Line, doc: Some(Outer) }), | ||
65 | ("//!", CommentKind { shape: Line, doc: Some(Inner) }), | ||
66 | ("/**", CommentKind { shape: Block, doc: Some(Outer) }), | ||
67 | ("/**", CommentKind { shape: Block, doc: Some(Inner) }), | ||
68 | ("//", CommentKind { shape: Line, doc: None }), | ||
69 | ("/*", CommentKind { shape: Block, doc: None }), | ||
70 | ] | ||
71 | }; | ||
72 | |||
73 | fn kind_by_prefix(text: &str) -> CommentKind { | ||
74 | for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() { | ||
75 | if text.starts_with(prefix) { | ||
76 | return *kind; | ||
61 | } | 77 | } |
62 | } | 78 | } |
79 | panic!("bad comment text: {:?}", text) | ||
80 | } | ||
63 | 81 | ||
64 | pub fn is_doc_comment(&self) -> bool { | 82 | fn prefix_by_kind(kind: CommentKind) -> &'static str { |
65 | match self { | 83 | for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() { |
66 | CommentFlavor::OuterDoc | CommentFlavor::InnerDoc => true, | 84 | if *k == kind { |
67 | _ => false, | 85 | return prefix; |
68 | } | 86 | } |
69 | } | 87 | } |
88 | unreachable!() | ||
70 | } | 89 | } |
71 | 90 | ||
72 | pub struct Whitespace<'a>(SyntaxToken<'a>); | 91 | pub struct Whitespace<'a>(SyntaxToken<'a>); |