From d65dc40348afc90c812b4d392f8e085eca78fc17 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 31 Jul 2019 11:43:00 -0400 Subject: Removes `*/` in block doc comments --- crates/ra_syntax/src/ast.rs | 49 ++++++++++++++++++++++++++++++++++++++ crates/ra_syntax/src/ast/traits.rs | 15 ++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index c5746d98d..8ac313e6e 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -138,6 +138,55 @@ fn test_doc_comment_preserves_newlines() { assert_eq!("this\nis\nmod\nfoo", module.doc_comment_text().unwrap()); } +#[test] +fn test_doc_comment_single_line_block_strips_suffix() { + let file = SourceFile::parse( + r#" + /** this is mod foo*/ + mod foo {} + "#, + ) + .ok() + .unwrap(); + let module = file.syntax().descendants().find_map(Module::cast).unwrap(); + assert_eq!("this is mod foo", module.doc_comment_text().unwrap()); +} + +#[test] +fn test_doc_comment_single_line_block_strips_suffix_whitespace() { + let file = SourceFile::parse( + r#" + /** this is mod foo */ + mod foo {} + "#, + ) + .ok() + .unwrap(); + let module = file.syntax().descendants().find_map(Module::cast).unwrap(); + assert_eq!("this is mod foo", module.doc_comment_text().unwrap()); +} + +#[test] +fn test_doc_comment_multi_line_block_strips_suffix() { + let file = SourceFile::parse( + r#" + /** + this + is + mod foo + */ + mod foo {} + "#, + ) + .ok() + .unwrap(); + let module = file.syntax().descendants().find_map(Module::cast).unwrap(); + assert_eq!( + " this\n is\n mod foo\n ", + module.doc_comment_text().unwrap() + ); +} + #[test] fn test_where_predicates() { fn assert_bound(text: &str, bound: Option) { diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 6ed1b5213..0c193e019 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -115,7 +115,7 @@ pub trait DocCommentsOwner: AstNode { } /// Returns the textual content of a doc comment block as a single string. - /// That is, strips leading `///` (+ optional 1 character of whitespace) + /// That is, strips leading `///` or trailing `*/` (+ optional 1 character of whitespace in either direction) /// and joins lines. fn doc_comment_text(&self) -> Option { let mut has_comments = false; @@ -136,7 +136,18 @@ pub trait DocCommentsOwner: AstNode { prefix_len }; - line[pos..].to_owned() + let end = if comment.kind().shape.is_block() && line.ends_with("*/") { + // FIXME: Use `nth_back` here once stable + if line.chars().rev().nth(2).map(|c| c.is_whitespace()).unwrap_or(false) { + line.len() - 3 + } else { + line.len() - 2 + } + } else { + line.len() + }; + + line[pos..end].to_owned() }) .join("\n"); -- cgit v1.2.3 From 0f61aa1f09f0609271d3c0a3d23815488ec94949 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 31 Jul 2019 13:59:14 -0400 Subject: Unconditionally trim the end of comments --- crates/ra_syntax/src/ast.rs | 5 +---- crates/ra_syntax/src/ast/traits.rs | 13 ++++--------- 2 files changed, 5 insertions(+), 13 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 8ac313e6e..6f0489617 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -181,10 +181,7 @@ fn test_doc_comment_multi_line_block_strips_suffix() { .ok() .unwrap(); let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!( - " this\n is\n mod foo\n ", - module.doc_comment_text().unwrap() - ); + assert_eq!(" this\n is\n mod foo", module.doc_comment_text().unwrap()); } #[test] diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 0c193e019..1b9a2b20c 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -115,8 +115,8 @@ pub trait DocCommentsOwner: AstNode { } /// Returns the textual content of a doc comment block as a single string. - /// That is, strips leading `///` or trailing `*/` (+ optional 1 character of whitespace in either direction) - /// and joins lines. + /// That is, strips leading `///` (+ optional 1 character of whitespace), + /// trailing `*/`, trailing whitespace and then joins the lines. fn doc_comment_text(&self) -> Option { let mut has_comments = false; let docs = self @@ -137,17 +137,12 @@ pub trait DocCommentsOwner: AstNode { }; let end = if comment.kind().shape.is_block() && line.ends_with("*/") { - // FIXME: Use `nth_back` here once stable - if line.chars().rev().nth(2).map(|c| c.is_whitespace()).unwrap_or(false) { - line.len() - 3 - } else { - line.len() - 2 - } + line.len() - 2 } else { line.len() }; - line[pos..end].to_owned() + line[pos..end].trim_end().to_owned() }) .join("\n"); -- cgit v1.2.3