Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crates/ra_syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ fn test_comments_preserve_trailing_whitespace() {
);
}

#[test]
fn test_four_slash_line_comment() {
let file = SourceFile::parse(
r#"
//// too many slashes to be a doc comment
/// doc comment
mod foo {}
"#,
)
.ok()
.unwrap();
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!("doc comment", module.doc_comment_text().unwrap());
}

#[test]
fn test_where_predicates() {
fn assert_bound(text: &str, bound: Option<TypeBound>) {
Expand Down
17 changes: 7 additions & 10 deletions crates/ra_syntax/src/ast/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ impl Comment {
}

pub fn prefix(&self) -> &'static str {
prefix_by_kind(self.kind())
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
if *k == self.kind() && self.text().starts_with(prefix) {
return prefix;
}
}
unreachable!()
}
}

Expand Down Expand Up @@ -48,6 +53,7 @@ pub enum CommentPlacement {
const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
use {CommentPlacement::*, CommentShape::*};
&[
("////", CommentKind { shape: Line, doc: None }),
Copy link
Contributor

@kjeremy kjeremy Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is //// specifically called out in rust? If not I think the code that returns the text should be changed instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it explicitly says a line comment begins with either // or ////: https://doc.rust-lang.org/reference/comments.html
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too!

("///", CommentKind { shape: Line, doc: Some(Outer) }),
("//!", CommentKind { shape: Line, doc: Some(Inner) }),
("/**", CommentKind { shape: Block, doc: Some(Outer) }),
Expand All @@ -69,15 +75,6 @@ fn kind_by_prefix(text: &str) -> CommentKind {
panic!("bad comment text: {:?}", text)
}

fn prefix_by_kind(kind: CommentKind) -> &'static str {
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
if *k == kind {
return prefix;
}
}
unreachable!()
}

impl Whitespace {
pub fn spans_multiple_lines(&self) -> bool {
let text = self.text();
Expand Down