Skip to content

Commit a271866

Browse files
committed
Fix attachment of inner doc comments
1 parent 81ac99f commit a271866

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
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/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)