From 5734b347ddfff0d285d3eecf5735cac30271696c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 17 Mar 2021 16:10:58 +0100 Subject: Fix incorrect newline emission in Attrs::docs --- crates/hir_def/src/attr.rs | 28 ++++++------ crates/ide/src/hover.rs | 26 ++++++++++-- crates/ide_db/src/call_info.rs | 6 +-- crates/ide_db/src/call_info/tests.rs | 82 ++++++++++++++++++------------------ 4 files changed, 80 insertions(+), 62 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index aeeb2c5cf..b7353d868 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -76,7 +76,7 @@ impl ops::Deref for Attrs { impl RawAttrs { pub(crate) const EMPTY: Self = Self { entries: None }; - pub(crate) fn new(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Self { + pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { let entries = collect_attrs(owner) .enumerate() .flat_map(|(i, attr)| match attr { @@ -92,7 +92,7 @@ impl RawAttrs { Self { entries: if entries.is_empty() { None } else { Some(entries) } } } - fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Self { + fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn ast::AttrsOwner>) -> Self { let hygiene = Hygiene::new(db.upcast(), owner.file_id); Self::new(owner.value, &hygiene) } @@ -178,7 +178,7 @@ impl Attrs { Some(it) => { let raw_attrs = RawAttrs::from_attrs_owner( db, - it.as_ref().map(|it| it as &dyn AttrsOwner), + it.as_ref().map(|it| it as &dyn ast::AttrsOwner), ); match mod_data.definition_source(db) { InFile { file_id, value: ModuleSource::SourceFile(file) } => raw_attrs @@ -189,9 +189,9 @@ impl Attrs { None => RawAttrs::from_attrs_owner( db, mod_data.definition_source(db).as_ref().map(|src| match src { - ModuleSource::SourceFile(file) => file as &dyn AttrsOwner, - ModuleSource::Module(module) => module as &dyn AttrsOwner, - ModuleSource::BlockExpr(block) => block as &dyn AttrsOwner, + ModuleSource::SourceFile(file) => file as &dyn ast::AttrsOwner, + ModuleSource::Module(module) => module as &dyn ast::AttrsOwner, + ModuleSource::BlockExpr(block) => block as &dyn ast::AttrsOwner, }), ), } @@ -249,7 +249,7 @@ impl Attrs { let mut res = ArenaMap::default(); for (id, var) in src.value.iter() { - let attrs = RawAttrs::from_attrs_owner(db, src.with_value(var as &dyn AttrsOwner)) + let attrs = RawAttrs::from_attrs_owner(db, src.with_value(var as &dyn ast::AttrsOwner)) .filter(db, krate); res.insert(id, attrs) @@ -283,7 +283,7 @@ impl Attrs { /// Constructs a map that maps the lowered `Attr`s in this `Attrs` back to its original syntax nodes. /// /// `owner` must be the original owner of the attributes. - pub fn source_map(&self, owner: &dyn AttrsOwner) -> AttrSourceMap { + pub fn source_map(&self, owner: &dyn ast::AttrsOwner) -> AttrSourceMap { AttrSourceMap { attrs: collect_attrs(owner).collect() } } @@ -321,9 +321,7 @@ impl Attrs { let mut buf = String::new(); for doc in docs { // str::lines doesn't yield anything for the empty string - if doc.is_empty() { - buf.push('\n'); - } else { + if !doc.is_empty() { buf.extend(Itertools::intersperse( doc.lines().map(|line| { line.char_indices() @@ -436,7 +434,7 @@ impl Attr { /// /// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of /// the attribute represented by `Attr`. - pub fn to_src(&self, owner: &dyn AttrsOwner) -> Either { + pub fn to_src(&self, owner: &dyn ast::AttrsOwner) -> Either { collect_attrs(owner).nth(self.index as usize).unwrap_or_else(|| { panic!("cannot find `Attr` at index {} in {}", self.index, owner.syntax()) }) @@ -528,7 +526,7 @@ where N: ast::AttrsOwner, { let src = InFile::new(src.file_id, src.to_node(db.upcast())); - RawAttrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) + RawAttrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn ast::AttrsOwner)) } fn attrs_from_item_tree(id: ItemTreeId, db: &dyn DefDatabase) -> RawAttrs { @@ -537,7 +535,9 @@ fn attrs_from_item_tree(id: ItemTreeId, db: &dyn DefDatabase tree.raw_attrs(mod_item.into()).clone() } -fn collect_attrs(owner: &dyn AttrsOwner) -> impl Iterator> { +fn collect_attrs( + owner: &dyn ast::AttrsOwner, +) -> impl Iterator> { let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) .map_or((None, None), |(attrs, docs)| ((Some(attrs), Some(docs)))); diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index cc2b79124..15d309d7d 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -1533,12 +1533,21 @@ fn my() {} fn test_hover_struct_doc_comment() { check( r#" -/// bar docs +/// This is an example +/// multiline doc +/// +/// # Example +/// +/// ``` +/// let five = 5; +/// +/// assert_eq!(6, my_crate::add_one(5)); +/// ``` struct Bar; fn foo() { let bar = Ba$0r; } "#, - expect![[r#" + expect![[r##" *Bar* ```rust @@ -1551,8 +1560,17 @@ fn foo() { let bar = Ba$0r; } --- - bar docs - "#]], + This is an example + multiline doc + + # Example + + ``` + let five = 5; + + assert_eq!(6, my_crate::add_one(5)); + ``` + "##]], ); } diff --git a/crates/ide_db/src/call_info.rs b/crates/ide_db/src/call_info.rs index d4016973c..7e26c3ccf 100644 --- a/crates/ide_db/src/call_info.rs +++ b/crates/ide_db/src/call_info.rs @@ -53,15 +53,15 @@ pub fn call_info(db: &RootDatabase, position: FilePosition) -> Option match callable.kind() { hir::CallableKind::Function(func) => { - res.doc = func.docs(db).map(|it| it.as_str().to_string()); + res.doc = func.docs(db).map(|it| it.into()); format_to!(res.signature, "fn {}", func.name(db)); } hir::CallableKind::TupleStruct(strukt) => { - res.doc = strukt.docs(db).map(|it| it.as_str().to_string()); + res.doc = strukt.docs(db).map(|it| it.into()); format_to!(res.signature, "struct {}", strukt.name(db)); } hir::CallableKind::TupleEnumVariant(variant) => { - res.doc = variant.docs(db).map(|it| it.as_str().to_string()); + res.doc = variant.docs(db).map(|it| it.into()); format_to!( res.signature, "enum {}::{}", diff --git a/crates/ide_db/src/call_info/tests.rs b/crates/ide_db/src/call_info/tests.rs index 9f84c253c..75ab3eb6e 100644 --- a/crates/ide_db/src/call_info/tests.rs +++ b/crates/ide_db/src/call_info/tests.rs @@ -220,11 +220,11 @@ fn bar() { } "#, expect![[r#" - test - ------ - fn foo(j: u32) -> u32 - () - "#]], + test + ------ + fn foo(j: u32) -> u32 + () + "#]], ); } @@ -249,19 +249,19 @@ pub fn do() { add_one($0 }"#, expect![[r##" - Adds one to the number given. + Adds one to the number given. - # Examples + # Examples - ``` - let five = 5; + ``` + let five = 5; - assert_eq!(6, my_crate::add_one(5)); - ``` - ------ - fn add_one(x: i32) -> i32 - () - "##]], + assert_eq!(6, my_crate::add_one(5)); + ``` + ------ + fn add_one(x: i32) -> i32 + () + "##]], ); } @@ -291,19 +291,19 @@ pub fn do_it() { } "#, expect![[r##" - Adds one to the number given. + Adds one to the number given. - # Examples + # Examples - ``` - let five = 5; + ``` + let five = 5; - assert_eq!(6, my_crate::add_one(5)); - ``` - ------ - fn add_one(x: i32) -> i32 - () - "##]], + assert_eq!(6, my_crate::add_one(5)); + ``` + ------ + fn add_one(x: i32) -> i32 + () + "##]], ); } @@ -335,13 +335,13 @@ pub fn foo(mut r: WriteHandler<()>) { } "#, expect![[r#" - Method is called when writer finishes. + Method is called when writer finishes. - By default this method stops actor's `Context`. - ------ - fn finished(&mut self, ctx: &mut {unknown}) - () - "#]], + By default this method stops actor's `Context`. + ------ + fn finished(&mut self, ctx: &mut {unknown}) + () + "#]], ); } @@ -389,11 +389,11 @@ fn main() { } "#, expect![[r#" - A cool tuple struct - ------ - struct S(u32, i32) - (u32, ) - "#]], + A cool tuple struct + ------ + struct S(u32, i32) + (u32, ) + "#]], ); } @@ -431,11 +431,11 @@ fn main() { } "#, expect![[r#" - A Variant - ------ - enum E::A(i32) - () - "#]], + A Variant + ------ + enum E::A(i32) + () + "#]], ); } -- cgit v1.2.3