From 8a6d6ac1326ed522004350ae5700ef3ca10c8230 Mon Sep 17 00:00:00 2001 From: csmoe Date: Fri, 4 Jan 2019 21:28:09 +0800 Subject: add mod doc comment test --- crates/ra_syntax/tests/data/parser/ok/0037_mod.rs | 5 +++++ crates/ra_syntax/tests/data/parser/ok/0037_mod.txt | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 crates/ra_syntax/tests/data/parser/ok/0037_mod.rs create mode 100644 crates/ra_syntax/tests/data/parser/ok/0037_mod.txt (limited to 'crates') diff --git a/crates/ra_syntax/tests/data/parser/ok/0037_mod.rs b/crates/ra_syntax/tests/data/parser/ok/0037_mod.rs new file mode 100644 index 000000000..7e5a1b835 --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/ok/0037_mod.rs @@ -0,0 +1,5 @@ +// https://github.com/rust-analyzer/rust-analyzer/issues/357 + +//! docs +// non-docs +mod foo {} \ No newline at end of file diff --git a/crates/ra_syntax/tests/data/parser/ok/0037_mod.txt b/crates/ra_syntax/tests/data/parser/ok/0037_mod.txt new file mode 100644 index 000000000..e11c4a06d --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/ok/0037_mod.txt @@ -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) + -- cgit v1.2.3 From f604ff5b2f72a593e23953ed8be7e9cbeba5d287 Mon Sep 17 00:00:00 2001 From: csmoe Date: Fri, 4 Jan 2019 21:29:00 +0800 Subject: parse doc comment for items --- crates/ra_analysis/src/lib.rs | 2 +- crates/ra_analysis/tests/test/main.rs | 3 ++- crates/ra_syntax/src/ast.rs | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index ab935954c..1e26a2889 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -394,7 +394,7 @@ impl Analysis { pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable> { 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> { self.db.parent_module(position) } diff --git a/crates/ra_analysis/tests/test/main.rs b/crates/ra_analysis/tests/test/main.rs index bfdf8aef2..859778024 100644 --- a/crates/ra_analysis/tests/test/main.rs +++ b/crates/ra_analysis/tests/test/main.rs @@ -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 } diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index e968c9728..c5273a7a2 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -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> { -- cgit v1.2.3 From a0ef196d0404cf89367928f347dd515ba31d081b Mon Sep 17 00:00:00 2001 From: csmoe Date: Fri, 4 Jan 2019 21:51:45 +0800 Subject: add mod doc comments test in ast --- crates/ra_syntax/src/ast.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'crates') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index c5273a7a2..3aaa5edda 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -481,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()); +} -- cgit v1.2.3