424: remove non-doc comments from doc comments r=matklad a=csmoe

Closes #357

Co-authored-by: csmoe <csmoe@msn.com>
This commit is contained in:
bors[bot] 2019-01-04 13:52:47 +00:00
commit 821fa7a50a
5 changed files with 50 additions and 2 deletions

View file

@ -394,7 +394,7 @@ impl Analysis {
pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
self.db.doc_text_for(nav)
}
/// Returns a `mod name;` declaration whihc created the current module.
/// Returns a `mod name;` declaration which created the current module.
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
self.db.parent_module(position)
}

View file

@ -249,7 +249,8 @@ fn bar() {
fn test_fn_signature_with_docs_simple() {
let (desc, param) = get_signature(
r#"
// test
/// test
// non-doc-comment
fn foo(j: u32) -> u32 {
j
}

View file

@ -115,6 +115,7 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> {
/// That is, strips leading `///` and joins lines
fn doc_comment_text(self) -> RustString {
self.doc_comments()
.filter(|comment| comment.is_doc_comment())
.map(|comment| {
let prefix = comment.prefix();
let trimmed = comment
@ -206,6 +207,10 @@ impl<'a> Comment<'a> {
}
}
pub fn is_doc_comment(&self) -> bool {
self.flavor().is_doc_comment()
}
pub fn prefix(&self) -> &'static str {
self.flavor().prefix()
}
@ -237,6 +242,13 @@ impl CommentFlavor {
Multiline => "/*",
}
}
pub fn is_doc_comment(&self) -> bool {
match self {
CommentFlavor::Doc | CommentFlavor::ModuleDoc => true,
_ => false,
}
}
}
impl<'a> Whitespace<'a> {
@ -469,3 +481,16 @@ impl<'a> PrefixExpr<'a> {
}
}
}
#[test]
fn test_doc_comment_of_items() {
let file = SourceFileNode::parse(
r#"
//! doc
// non-doc
mod foo {}
"#,
);
let module = file.syntax().descendants().find_map(Module::cast).unwrap();
assert_eq!("doc", module.doc_comment_text());
}

View file

@ -0,0 +1,5 @@
// https://github.com/rust-analyzer/rust-analyzer/issues/357
//! docs
// non-docs
mod foo {}

View file

@ -0,0 +1,17 @@
SOURCE_FILE@[0; 93)
COMMENT@[0; 60)
WHITESPACE@[60; 62)
MODULE@[62; 93)
COMMENT@[62; 70)
WHITESPACE@[70; 71)
COMMENT@[71; 82)
WHITESPACE@[82; 83)
MOD_KW@[83; 86)
WHITESPACE@[86; 87)
NAME@[87; 90)
IDENT@[87; 90) "foo"
WHITESPACE@[90; 91)
ITEM_LIST@[91; 93)
L_CURLY@[91; 92)
R_CURLY@[92; 93)