diff options
author | Jeremy Kolb <[email protected]> | 2019-10-27 20:56:25 +0000 |
---|---|---|
committer | Jeremy Kolb <[email protected]> | 2019-10-27 20:56:25 +0000 |
commit | 1438f38eb63e5a79350fa6c877d9960ab90e183d (patch) | |
tree | 68ad8a4f944b82c2e17811f41948dc3cd5b345fd /crates | |
parent | 46b63c462d66925b59c0af7dec2eb3c48bc7be25 (diff) |
Preserve whitespace at the end of doc comments
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
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 24 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/traits.rs | 7 |
2 files changed, 27 insertions, 4 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 1b2ce921a..a12da5be2 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -173,7 +173,7 @@ fn test_doc_comment_single_line_block_strips_suffix_whitespace() { | |||
173 | .ok() | 173 | .ok() |
174 | .unwrap(); | 174 | .unwrap(); |
175 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); | 175 | 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()); |
177 | } | 177 | } |
178 | 178 | ||
179 | #[test] | 179 | #[test] |
@@ -191,7 +191,27 @@ fn test_doc_comment_multi_line_block_strips_suffix() { | |||
191 | .ok() | 191 | .ok() |
192 | .unwrap(); | 192 | .unwrap(); |
193 | let module = file.syntax().descendants().find_map(Module::cast).unwrap(); | 193 | 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 | ); | ||
195 | } | 215 | } |
196 | 216 | ||
197 | #[test] | 217 | #[test] |
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index f275a4955..76313684e 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs | |||
@@ -120,7 +120,7 @@ pub trait DocCommentsOwner: AstNode { | |||
120 | has_comments = true; | 120 | has_comments = true; |
121 | let prefix_len = comment.prefix().len(); | 121 | let prefix_len = comment.prefix().len(); |
122 | 122 | ||
123 | let line = comment.text().as_str(); | 123 | let line: &str = comment.text().as_str(); |
124 | 124 | ||
125 | // Determine if the prefix or prefix + 1 char is stripped | 125 | // Determine if the prefix or prefix + 1 char is stripped |
126 | let pos = | 126 | let pos = |
@@ -136,7 +136,10 @@ pub trait DocCommentsOwner: AstNode { | |||
136 | line.len() | 136 | line.len() |
137 | }; | 137 | }; |
138 | 138 | ||
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() | ||
140 | }) | 143 | }) |
141 | .join("\n"); | 144 | .join("\n"); |
142 | 145 | ||