Skip to content

Commit bbd0fbd

Browse files
bors[bot]kjeremy
andauthored
Merge #2101
2101: Preserve whitespace at the end of doc comments r=matklad a=kjeremy Whitespace can have special meaning in markdown. For instance ending a line with three spaces will render a new line. Note that this behavior diverges from RLS. Fixes #1997 Co-authored-by: Jeremy Kolb <[email protected]>
2 parents 46b63c4 + 1438f38 commit bbd0fbd

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

crates/ra_syntax/src/ast.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn test_doc_comment_single_line_block_strips_suffix_whitespace() {
173173
.ok()
174174
.unwrap();
175175
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
176-
assert_eq!("this is mod foo", module.doc_comment_text().unwrap());
176+
assert_eq!("this is mod foo ", module.doc_comment_text().unwrap());
177177
}
178178

179179
#[test]
@@ -191,7 +191,27 @@ fn test_doc_comment_multi_line_block_strips_suffix() {
191191
.ok()
192192
.unwrap();
193193
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
194-
assert_eq!(" this\n is\n mod foo", module.doc_comment_text().unwrap());
194+
assert_eq!(
195+
" this\n is\n mod foo\n ",
196+
module.doc_comment_text().unwrap()
197+
);
198+
}
199+
200+
#[test]
201+
fn test_comments_preserve_trailing_whitespace() {
202+
let file = SourceFile::parse(
203+
r#"
204+
/// Representation of a Realm.
205+
/// In the specification these are called Realm Records.
206+
struct Realm {}"#,
207+
)
208+
.ok()
209+
.unwrap();
210+
let def = file.syntax().descendants().find_map(StructDef::cast).unwrap();
211+
assert_eq!(
212+
"Representation of a Realm. \nIn the specification these are called Realm Records.",
213+
def.doc_comment_text().unwrap()
214+
);
195215
}
196216

197217
#[test]

crates/ra_syntax/src/ast/traits.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub trait DocCommentsOwner: AstNode {
120120
has_comments = true;
121121
let prefix_len = comment.prefix().len();
122122

123-
let line = comment.text().as_str();
123+
let line: &str = comment.text().as_str();
124124

125125
// Determine if the prefix or prefix + 1 char is stripped
126126
let pos =
@@ -136,7 +136,10 @@ pub trait DocCommentsOwner: AstNode {
136136
line.len()
137137
};
138138

139-
line[pos..end].trim_end().to_owned()
139+
// Note that we do not trim the end of the line here
140+
// since whitespace can have special meaning at the end
141+
// of a line in markdown.
142+
line[pos..end].to_owned()
140143
})
141144
.join("\n");
142145

0 commit comments

Comments
 (0)