Skip to content

Commit 99fa139

Browse files
bors[bot]matklad
andauthored
Merge #6534
6534: Fix attachment of inner doc comments r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 8026658 + a271866 commit 99fa139

File tree

4 files changed

+56
-47
lines changed

4 files changed

+56
-47
lines changed

crates/syntax/src/ast.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ fn test_doc_comment_none() {
115115
}
116116

117117
#[test]
118-
fn test_doc_comment_of_items() {
118+
fn test_outer_doc_comment_of_items() {
119119
let file = SourceFile::parse(
120120
r#"
121-
//! doc
121+
/// doc
122122
// non-doc
123123
mod foo {}
124124
"#,
@@ -129,6 +129,21 @@ fn test_doc_comment_of_items() {
129129
assert_eq!("doc", module.doc_comment_text().unwrap());
130130
}
131131

132+
#[test]
133+
fn test_inner_doc_comment_of_items() {
134+
let file = SourceFile::parse(
135+
r#"
136+
//! doc
137+
// non-doc
138+
mod foo {}
139+
"#,
140+
)
141+
.ok()
142+
.unwrap();
143+
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
144+
assert!(module.doc_comment_text().is_none());
145+
}
146+
132147
#[test]
133148
fn test_doc_comment_of_statics() {
134149
let file = SourceFile::parse(

crates/syntax/src/ast/token_ext.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ use crate::{
1414

1515
impl ast::Comment {
1616
pub fn kind(&self) -> CommentKind {
17-
kind_by_prefix(self.text())
17+
CommentKind::from_text(self.text())
1818
}
1919

2020
pub fn prefix(&self) -> &'static str {
21-
for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
22-
if *k == self.kind() && self.text().starts_with(prefix) {
23-
return prefix;
24-
}
25-
}
26-
unreachable!()
21+
let &(prefix, _kind) = CommentKind::BY_PREFIX
22+
.iter()
23+
.find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
24+
.unwrap();
25+
prefix
2726
}
2827
}
2928

@@ -55,29 +54,25 @@ pub enum CommentPlacement {
5554
Outer,
5655
}
5756

58-
const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
59-
use {CommentPlacement::*, CommentShape::*};
60-
&[
61-
("////", CommentKind { shape: Line, doc: None }),
62-
("///", CommentKind { shape: Line, doc: Some(Outer) }),
63-
("//!", CommentKind { shape: Line, doc: Some(Inner) }),
64-
("/**", CommentKind { shape: Block, doc: Some(Outer) }),
65-
("/*!", CommentKind { shape: Block, doc: Some(Inner) }),
66-
("//", CommentKind { shape: Line, doc: None }),
67-
("/*", CommentKind { shape: Block, doc: None }),
68-
]
69-
};
57+
impl CommentKind {
58+
const BY_PREFIX: [(&'static str, CommentKind); 8] = [
59+
("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
60+
("////", CommentKind { shape: CommentShape::Line, doc: None }),
61+
("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
62+
("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
63+
("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }),
64+
("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }),
65+
("//", CommentKind { shape: CommentShape::Line, doc: None }),
66+
("/*", CommentKind { shape: CommentShape::Block, doc: None }),
67+
];
7068

71-
fn kind_by_prefix(text: &str) -> CommentKind {
72-
if text == "/**/" {
73-
return CommentKind { shape: CommentShape::Block, doc: None };
74-
}
75-
for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() {
76-
if text.starts_with(prefix) {
77-
return *kind;
78-
}
69+
pub(crate) fn from_text(text: &str) -> CommentKind {
70+
let &(_prefix, kind) = CommentKind::BY_PREFIX
71+
.iter()
72+
.find(|&(prefix, _kind)| text.starts_with(prefix))
73+
.unwrap();
74+
kind
7975
}
80-
panic!("bad comment text: {:?}", text)
8176
}
8277

8378
impl ast::Whitespace {

crates/syntax/src/parsing/text_tree_sink.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::mem;
55
use parser::{ParseError, TreeSink};
66

77
use crate::{
8+
ast,
89
parsing::Token,
910
syntax_node::GreenNode,
1011
SmolStr, SyntaxError,
@@ -153,24 +154,22 @@ fn n_attached_trivias<'a>(
153154

154155
while let Some((i, (kind, text))) = trivias.next() {
155156
match kind {
156-
WHITESPACE => {
157-
if text.contains("\n\n") {
158-
// we check whether the next token is a doc-comment
159-
// and skip the whitespace in this case
160-
if let Some((peek_kind, peek_text)) =
161-
trivias.peek().map(|(_, pair)| pair)
162-
{
163-
if *peek_kind == COMMENT
164-
&& peek_text.starts_with("///")
165-
&& !peek_text.starts_with("////")
166-
{
167-
continue;
168-
}
157+
WHITESPACE if text.contains("\n\n") => {
158+
// we check whether the next token is a doc-comment
159+
// and skip the whitespace in this case
160+
if let Some((COMMENT, peek_text)) = trivias.peek().map(|(_, pair)| pair) {
161+
let comment_kind = ast::CommentKind::from_text(peek_text);
162+
if comment_kind.doc == Some(ast::CommentPlacement::Outer) {
163+
continue;
169164
}
170-
break;
171165
}
166+
break;
172167
}
173168
COMMENT => {
169+
let comment_kind = ast::CommentKind::from_text(text);
170+
if comment_kind.doc == Some(ast::CommentPlacement::Inner) {
171+
break;
172+
}
174173
res = i + 1;
175174
}
176175
_ => (),

crates/syntax/test_data/parser/ok/0037_mod.rast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
22
[email protected] "// https://github.com ..."
33
4-
MODULE@62..93
5-
[email protected] "//! docs"
6-
4+
COMMENT@62..70 "//! docs"
5+
6+
77
[email protected] "// non-docs"
88
99

0 commit comments

Comments
 (0)