From 6b8358d4d9317c13593ae15e6065d67eecb85296 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 29 Mar 2021 20:11:28 +0200 Subject: Remove code duplication in highlight injection --- crates/ide/src/doc_links.rs | 63 +++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 31 deletions(-) (limited to 'crates/ide/src/doc_links.rs') diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 9301cdeff..99276168f 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -98,6 +98,29 @@ pub(crate) fn remove_links(markdown: &str) -> String { out } +/// Retrieve a link to documentation for the given symbol. +pub(crate) fn external_docs( + db: &RootDatabase, + position: &FilePosition, +) -> Option { + let sema = Semantics::new(db); + let file = sema.parse(position.file_id).syntax().clone(); + let token = pick_best(file.token_at_offset(position.offset))?; + let token = sema.descend_into_macros(token); + + let node = token.parent()?; + let definition = match_ast! { + match node { + ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), + ast::Name(name) => NameClass::classify(&sema, &name).map(|d| d.referenced_or_defined(sema.db)), + _ => None, + } + }; + + get_doc_link(db, definition?) +} + +/// Extracts all links from a given markdown text. pub(crate) fn extract_definitions_from_markdown( markdown: &str, ) -> Vec<(Range, String, Option)> { @@ -178,15 +201,15 @@ pub(crate) fn resolve_doc_path_for_def( ) -> Option { match def { Definition::ModuleDef(def) => match def { - ModuleDef::Module(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::Function(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::Adt(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::Variant(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::Const(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::Static(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::Trait(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, &link, ns), - ModuleDef::BuiltinType(_) => None, + hir::ModuleDef::Module(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::Function(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::Adt(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::Variant(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::Const(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::Static(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::Trait(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, &link, ns), + hir::ModuleDef::BuiltinType(_) => None, }, Definition::Macro(it) => it.resolve_doc_path(db, &link, ns), Definition::Field(it) => it.resolve_doc_path(db, &link, ns), @@ -328,28 +351,6 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option Option { - let sema = Semantics::new(db); - let file = sema.parse(position.file_id).syntax().clone(); - let token = pick_best(file.token_at_offset(position.offset))?; - let token = sema.descend_into_macros(token); - - let node = token.parent()?; - let definition = match_ast! { - match node { - ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), - ast::Name(name) => NameClass::classify(&sema, &name).map(|d| d.referenced_or_defined(sema.db)), - _ => None, - } - }; - - get_doc_link(db, definition?) -} - /// Rewrites a markdown document, applying 'callback' to each link. fn map_links<'e>( events: impl Iterator>, -- cgit v1.2.3