Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-db/src/defs.rs')
| -rw-r--r-- | crates/ide-db/src/defs.rs | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/crates/ide-db/src/defs.rs b/crates/ide-db/src/defs.rs index 9ce85b2bf3..c051fd863d 100644 --- a/crates/ide-db/src/defs.rs +++ b/crates/ide-db/src/defs.rs @@ -5,10 +5,8 @@ // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). -use std::borrow::Cow; - use crate::RootDatabase; -use crate::documentation::{Documentation, HasDocs}; +use crate::documentation::{DocsRangeMap, Documentation, HasDocs}; use crate::famous_defs::FamousDefs; use arrayvec::ArrayVec; use either::Either; @@ -23,7 +21,7 @@ use hir::{ use span::Edition; use stdx::{format_to, impl_from}; use syntax::{ - SyntaxKind, SyntaxNode, SyntaxToken, + SyntaxKind, SyntaxNode, SyntaxToken, TextSize, ast::{self, AstNode}, match_ast, }; @@ -201,25 +199,21 @@ impl Definition { Some(name) } - pub fn docs<'db>( + pub fn docs( &self, - db: &'db RootDatabase, + db: &RootDatabase, famous_defs: Option<&FamousDefs<'_, '_>>, display_target: DisplayTarget, - ) -> Option<Documentation<'db>> { - self.docs_with_rangemap(db, famous_defs, display_target).map(|docs| match docs { - Either::Left(Cow::Borrowed(docs)) => Documentation::new_borrowed(docs.docs()), - Either::Left(Cow::Owned(docs)) => Documentation::new_owned(docs.into_docs()), - Either::Right(docs) => docs, - }) + ) -> Option<Documentation> { + self.docs_with_rangemap(db, famous_defs, display_target).map(|(docs, _)| docs) } - pub fn docs_with_rangemap<'db>( + pub fn docs_with_rangemap( &self, - db: &'db RootDatabase, + db: &RootDatabase, famous_defs: Option<&FamousDefs<'_, '_>>, display_target: DisplayTarget, - ) -> Option<Either<Cow<'db, hir::Docs>, Documentation<'db>>> { + ) -> Option<(Documentation, Option<DocsRangeMap>)> { let docs = match self { Definition::Macro(it) => it.docs_with_rangemap(db), Definition::Field(it) => it.docs_with_rangemap(db), @@ -235,13 +229,15 @@ impl Definition { it.docs_with_rangemap(db).or_else(|| { // docs are missing, try to fall back to the docs of the aliased item. let adt = it.ty(db).as_adt()?; - let mut docs = adt.docs_with_rangemap(db)?.into_owned(); + let (docs, range_map) = adt.docs_with_rangemap(db)?; let header_docs = format!( "*This is the documentation for* `{}`\n\n", adt.display(db, display_target) ); - docs.prepend_str(&header_docs); - Some(Cow::Owned(docs)) + let offset = TextSize::new(header_docs.len() as u32); + let range_map = range_map.shift_docstring_line_range(offset); + let docs = header_docs + docs.as_str(); + Some((Documentation::new(docs), range_map)) }) } Definition::BuiltinType(it) => { @@ -250,7 +246,7 @@ impl Definition { let primitive_mod = format!("prim_{}", it.name().display(fd.0.db, display_target.edition)); let doc_owner = find_std_module(fd, &primitive_mod, display_target.edition)?; - doc_owner.docs_with_rangemap(db) + doc_owner.docs_with_rangemap(fd.0.db) }) } Definition::BuiltinLifetime(StaticLifetime) => None, @@ -286,7 +282,7 @@ impl Definition { ); } - return Some(Either::Right(Documentation::new_owned(docs.replace('*', "\\*")))); + return Some((Documentation::new(docs.replace('*', "\\*")), None)); } Definition::ToolModule(_) => None, Definition::DeriveHelper(_) => None, @@ -303,7 +299,7 @@ impl Definition { let item = trait_.items(db).into_iter().find(|it| it.name(db) == name)?; item.docs_with_rangemap(db) }) - .map(Either::Left) + .map(|(docs, range_map)| (docs, Some(range_map))) } pub fn label(&self, db: &RootDatabase, display_target: DisplayTarget) -> String { |