Skip to content

Commit a932ccd

Browse files
bors[bot]ltentrup
andauthored
Merge #3785
3785: Attach doc-comment to declaration if there are newlines in between r=matklad a=ltentrup This commit changes the parser to attach doc-comments to the corresponding declaration in case there are newlines in between the doc-comment and the declaration. Implements the changes proposed in #3757 Co-authored-by: Leander Tentrup <[email protected]>
2 parents 668980d + 77f89a7 commit a932ccd

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

crates/ra_syntax/src/parsing/text_tree_sink.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,21 @@ fn n_attached_trivias<'a>(
149149
MACRO_CALL | CONST_DEF | TYPE_ALIAS_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF
150150
| TRAIT_DEF | MODULE | RECORD_FIELD_DEF | STATIC_DEF => {
151151
let mut res = 0;
152-
for (i, (kind, text)) in trivias.enumerate() {
152+
let mut trivias = trivias.enumerate().peekable();
153+
154+
while let Some((i, (kind, text))) = trivias.next() {
153155
match kind {
154156
WHITESPACE => {
155157
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 && peek_text.starts_with("///") {
164+
continue;
165+
}
166+
}
156167
break;
157168
}
158169
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// Example
2+
3+
fn test() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
SOURCE_FILE@[0; 26)
2+
FN_DEF@[0; 25)
3+
COMMENT@[0; 11) "/// Example"
4+
WHITESPACE@[11; 13) "\n\n"
5+
FN_KW@[13; 15) "fn"
6+
WHITESPACE@[15; 16) " "
7+
NAME@[16; 20)
8+
IDENT@[16; 20) "test"
9+
PARAM_LIST@[20; 22)
10+
L_PAREN@[20; 21) "("
11+
R_PAREN@[21; 22) ")"
12+
WHITESPACE@[22; 23) " "
13+
BLOCK_EXPR@[23; 25)
14+
BLOCK@[23; 25)
15+
L_CURLY@[23; 24) "{"
16+
R_CURLY@[24; 25) "}"
17+
WHITESPACE@[25; 26) "\n"

0 commit comments

Comments
 (0)