diff options
Diffstat (limited to 'crates/ide/src/doc_links.rs')
-rw-r--r-- | crates/ide/src/doc_links.rs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 06af36b73..d9dc63b33 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs | |||
@@ -1,9 +1,10 @@ | |||
1 | //! Resolves and rewrites links in markdown documentation. | 1 | //! Resolves and rewrites links in markdown documentation. |
2 | 2 | ||
3 | use std::convert::TryFrom; | ||
3 | use std::iter::once; | 4 | use std::iter::once; |
4 | 5 | ||
5 | use itertools::Itertools; | 6 | use itertools::Itertools; |
6 | use pulldown_cmark::{CowStr, Event, LinkType, Options, Parser, Tag}; | 7 | use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag}; |
7 | use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; | 8 | use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; |
8 | use url::Url; | 9 | use url::Url; |
9 | 10 | ||
@@ -13,7 +14,7 @@ use hir::{ | |||
13 | ModuleDef, | 14 | ModuleDef, |
14 | }; | 15 | }; |
15 | use ide_db::{ | 16 | use ide_db::{ |
16 | defs::{classify_name, classify_name_ref, Definition}, | 17 | defs::{Definition, NameClass, NameRefClass}, |
17 | RootDatabase, | 18 | RootDatabase, |
18 | }; | 19 | }; |
19 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | 20 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; |
@@ -24,11 +25,13 @@ pub type DocumentationLink = String; | |||
24 | 25 | ||
25 | /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) | 26 | /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) |
26 | pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { | 27 | pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { |
27 | let doc = Parser::new_with_broken_link_callback( | 28 | let mut cb = |link: BrokenLink| { |
28 | markdown, | 29 | Some(( |
29 | Options::empty(), | 30 | /*url*/ link.reference.to_owned().into(), |
30 | Some(&|label, _| Some((/*url*/ label.to_string(), /*title*/ label.to_string()))), | 31 | /*title*/ link.reference.to_owned().into(), |
31 | ); | 32 | )) |
33 | }; | ||
34 | let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb)); | ||
32 | 35 | ||
33 | let doc = map_links(doc, |target, title: &str| { | 36 | let doc = map_links(doc, |target, title: &str| { |
34 | // This check is imperfect, there's some overlap between valid intra-doc links | 37 | // This check is imperfect, there's some overlap between valid intra-doc links |
@@ -66,11 +69,11 @@ pub fn remove_links(markdown: &str) -> String { | |||
66 | let mut opts = Options::empty(); | 69 | let mut opts = Options::empty(); |
67 | opts.insert(Options::ENABLE_FOOTNOTES); | 70 | opts.insert(Options::ENABLE_FOOTNOTES); |
68 | 71 | ||
69 | let doc = Parser::new_with_broken_link_callback( | 72 | let mut cb = |_: BrokenLink| { |
70 | markdown, | 73 | let empty = InlineStr::try_from("").unwrap(); |
71 | opts, | 74 | Some((CowStr::Inlined(empty.clone()), CowStr::Inlined(empty))) |
72 | Some(&|_, _| Some((String::new(), String::new()))), | 75 | }; |
73 | ); | 76 | let doc = Parser::new_with_broken_link_callback(markdown, opts, Some(&mut cb)); |
74 | let doc = doc.filter_map(move |evt| match evt { | 77 | let doc = doc.filter_map(move |evt| match evt { |
75 | Event::Start(Tag::Link(link_type, ref target, ref title)) => { | 78 | Event::Start(Tag::Link(link_type, ref target, ref title)) => { |
76 | if link_type == LinkType::Inline && target.contains("://") { | 79 | if link_type == LinkType::Inline && target.contains("://") { |
@@ -229,8 +232,8 @@ pub(crate) fn external_docs( | |||
229 | let node = token.parent(); | 232 | let node = token.parent(); |
230 | let definition = match_ast! { | 233 | let definition = match_ast! { |
231 | match node { | 234 | match node { |
232 | ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition(sema.db)), | 235 | ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), |
233 | ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)), | 236 | ast::Name(name) => NameClass::classify(&sema, &name).map(|d| d.referenced_or_defined(sema.db)), |
234 | _ => None, | 237 | _ => None, |
235 | } | 238 | } |
236 | }; | 239 | }; |