@@ -19,7 +19,7 @@ use ra_ide_db::{
19
19
use ra_syntax:: {
20
20
ast, ast:: Path , match_ast, AstNode , SyntaxKind :: * , SyntaxNode , SyntaxToken , TokenAtOffset ,
21
21
} ;
22
- use ra_tt:: { Ident , Leaf , Literal , Punct , TokenTree } ;
22
+ use ra_tt:: { Ident , Leaf , Literal , TokenTree } ;
23
23
use url:: Url ;
24
24
25
25
use crate :: {
@@ -410,6 +410,8 @@ fn map_links<'e>(
410
410
}
411
411
Event :: Text ( s) if in_link => {
412
412
link_text = s. clone ( ) ;
413
+ // TODO: This can unintentionally strip words from path-based links.
414
+ // See std::box::Box -> std::box link as an example.
413
415
Event :: Text ( CowStr :: Boxed ( strip_prefixes_suffixes ( & s) . into ( ) ) )
414
416
}
415
417
Event :: Code ( s) if in_link => {
@@ -603,29 +605,33 @@ fn try_resolve_path(db: &RootDatabase, definition: &Definition, link: &str) -> O
603
605
}
604
606
605
607
/// Try to get the root URL of the documentation of a crate.
606
- // FIXME: Special case standard, core, alloc libraries
607
608
fn get_doc_url ( db : & RootDatabase , krate : & Crate ) -> Option < Url > {
608
609
// Look for #![doc(html_root_url = "...")]
609
610
let attrs = db. attrs ( AttrDef :: from ( krate. root_module ( db) ?) . into ( ) ) ;
610
611
let doc_attr_q = attrs. by_key ( "doc" ) ;
612
+
613
+ // TODO: Tests for this parsing
611
614
let doc_url = if doc_attr_q. exists ( ) {
612
- doc_attr_q. tt_values ( ) . filter_map ( |tt| match tt. token_trees . as_slice ( ) {
613
- & [
614
- TokenTree :: Leaf ( Leaf :: Ident ( Ident { text : ref ident_text, ..} ) ) ,
615
- TokenTree :: Leaf ( Leaf :: Punct ( Punct { r#char : '=' , ..} ) ) ,
616
- TokenTree :: Leaf ( Leaf :: Literal ( Literal { ref text, ..} ) )
617
- ] if ident_text == "html_root_url" => Some ( text. to_string ( ) ) ,
618
- _ => {
619
- None
620
- }
621
- } ) . next ( )
615
+ doc_attr_q. tt_values ( ) . map ( |tt| {
616
+ let name = tt. token_trees . iter ( )
617
+ . skip_while ( |tt| !matches ! ( tt, TokenTree :: Leaf ( Leaf :: Ident ( Ident { text: ref ident, ..} ) ) if ident == "html_root_url" ) )
618
+ . skip ( 2 )
619
+ . next ( ) ;
620
+
621
+ match name {
622
+ Some ( TokenTree :: Leaf ( Leaf :: Literal ( Literal { ref text, ..} ) ) ) => Some ( text) ,
623
+ _ => None
624
+ }
625
+ } ) . flat_map ( |t| t) . next ( ) . map ( |s| s. to_string ( ) )
622
626
} else {
623
627
// Fallback to docs.rs
624
628
// FIXME: Specify an exact version here (from Cargo.lock)
625
629
Some ( format ! ( "https://docs.rs/{}/*" , krate. display_name( db) ?) )
626
630
} ;
627
631
628
- doc_url. map ( |s| s. trim_matches ( '"' ) . to_owned ( ) + "/" ) . and_then ( |s| Url :: parse ( & s) . ok ( ) )
632
+ doc_url
633
+ . map ( |s| s. trim_matches ( '"' ) . trim_end_matches ( "/" ) . to_owned ( ) + "/" )
634
+ . and_then ( |s| Url :: parse ( & s) . ok ( ) )
629
635
}
630
636
631
637
/// Get the filename and extension generated for a symbol by rustdoc.
0 commit comments