Skip to content

Commit 2096db9

Browse files
bors[bot]adamrk
andauthored
Merge #4184
4184: Treat comments beginning with four slashes as regular line comments r=kjeremy a=adamrk Addresses #4040 Co-authored-by: adamrk <[email protected]>
2 parents 9230ae5 + 0bd7d81 commit 2096db9

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

crates/ra_syntax/src/ast.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,21 @@ fn test_comments_preserve_trailing_whitespace() {
242242
);
243243
}
244244

245+
#[test]
246+
fn test_four_slash_line_comment() {
247+
let file = SourceFile::parse(
248+
r#"
249+
//// too many slashes to be a doc comment
250+
/// doc comment
251+
mod foo {}
252+
"#,
253+
)
254+
.ok()
255+
.unwrap();
256+
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
257+
assert_eq!("doc comment", module.doc_comment_text().unwrap());
258+
}
259+
245260
#[test]
246261
fn test_where_predicates() {
247262
fn assert_bound(text: &str, bound: Option<TypeBound>) {

crates/ra_syntax/src/ast/tokens.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ impl Comment {
1313
}
1414

1515
pub fn prefix(&self) -> &'static str {
16-
prefix_by_kind(self.kind())
16+
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
17+
if *k == self.kind() && self.text().starts_with(prefix) {
18+
return prefix;
19+
}
20+
}
21+
unreachable!()
1722
}
1823
}
1924

@@ -48,6 +53,7 @@ pub enum CommentPlacement {
4853
const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
4954
use {CommentPlacement::*, CommentShape::*};
5055
&[
56+
("////", CommentKind { shape: Line, doc: None }),
5157
("///", CommentKind { shape: Line, doc: Some(Outer) }),
5258
("//!", CommentKind { shape: Line, doc: Some(Inner) }),
5359
("/**", CommentKind { shape: Block, doc: Some(Outer) }),
@@ -69,15 +75,6 @@ fn kind_by_prefix(text: &str) -> CommentKind {
6975
panic!("bad comment text: {:?}", text)
7076
}
7177

72-
fn prefix_by_kind(kind: CommentKind) -> &'static str {
73-
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
74-
if *k == kind {
75-
return prefix;
76-
}
77-
}
78-
unreachable!()
79-
}
80-
8178
impl Whitespace {
8279
pub fn spans_multiple_lines(&self) -> bool {
8380
let text = self.text();

0 commit comments

Comments
 (0)