aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-04-02 10:34:23 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-04-02 10:34:23 +0100
commit370dbc7867719662f26db5684ac82cfc25b5b8cb (patch)
tree70c74389382cd3c8ce72dc7739dbe5502f9f7c8b /crates/ra_syntax/src
parentd21a677715196c46b73017acbae0105ef554284d (diff)
parent5287a2506c19b7f886556f8a73510f57dd234857 (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')
-rw-r--r--crates/ra_syntax/src/ast/tokens.rs83
-rw-r--r--crates/ra_syntax/src/ast/traits.rs2
2 files changed, 52 insertions, 33 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
23impl<'a> Comment<'a> { 23impl<'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)]
47pub enum CommentFlavor { 34pub struct CommentKind {
35 pub shape: CommentShape,
36 pub doc: Option<CommentPlacement>,
37}
38
39#[derive(Debug, PartialEq, Eq, Clone, Copy)]
40pub enum CommentShape {
48 Line, 41 Line,
49 OuterDoc, 42 Block,
50 InnerDoc,
51 Multiline,
52} 43}
53 44
54impl CommentFlavor { 45impl 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)]
56pub enum CommentPlacement {
57 Inner,
58 Outer,
59}
60
61const 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
73fn 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 { 82fn 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
72pub struct Whitespace<'a>(SyntaxToken<'a>); 91pub struct Whitespace<'a>(SyntaxToken<'a>);
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs
index 43d1509fa..98aa22085 100644
--- a/crates/ra_syntax/src/ast/traits.rs
+++ b/crates/ra_syntax/src/ast/traits.rs
@@ -111,7 +111,7 @@ pub trait DocCommentsOwner: AstNode {
111 let mut has_comments = false; 111 let mut has_comments = false;
112 let docs = self 112 let docs = self
113 .doc_comments() 113 .doc_comments()
114 .filter(|comment| comment.is_doc_comment()) 114 .filter(|comment| comment.kind().doc.is_some())
115 .map(|comment| { 115 .map(|comment| {
116 has_comments = true; 116 has_comments = true;
117 let prefix_len = comment.prefix().len(); 117 let prefix_len = comment.prefix().len();