@@ -467,17 +467,24 @@ fn map_links<'e>(
467
467
/// ```
468
468
fn get_doc_base_url ( db : & RootDatabase , krate : & Crate ) -> Option < Url > {
469
469
let display_name = krate. display_name ( db) ?;
470
- krate
471
- . get_html_root_url ( db)
472
- . or_else ( || {
473
- // Fallback to docs.rs. This uses `display_name` and can never be
474
- // correct, but that's what fallbacks are about.
475
- //
476
- // FIXME: clicking on the link should just open the file in the editor,
477
- // instead of falling back to external urls.
478
- Some ( format ! ( "https://docs.rs/{krate}/*/" , krate = display_name) )
479
- } )
480
- . and_then ( |s| Url :: parse ( & s) . ok ( ) ?. join ( & format ! ( "{}/" , display_name) ) . ok ( ) )
470
+ let base = match & * * display_name. crate_name ( ) {
471
+ // std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
472
+ // FIXME: Use the toolchains channel instead of nightly
473
+ name @ ( "core" | "std" | "alloc" | "proc_macro" | "test" ) => {
474
+ format ! ( "https://doc.rust-lang.org/nightly/{}" , name)
475
+ }
476
+ _ => {
477
+ krate. get_html_root_url ( db) . or_else ( || {
478
+ // Fallback to docs.rs. This uses `display_name` and can never be
479
+ // correct, but that's what fallbacks are about.
480
+ //
481
+ // FIXME: clicking on the link should just open the file in the editor,
482
+ // instead of falling back to external urls.
483
+ Some ( format ! ( "https://docs.rs/{krate}/*/" , krate = display_name) )
484
+ } ) ?
485
+ }
486
+ } ;
487
+ Url :: parse ( & base) . ok ( ) ?. join ( & format ! ( "{}/" , display_name) ) . ok ( )
481
488
}
482
489
483
490
/// Get the filename and extension generated for a symbol by rustdoc.
@@ -555,73 +562,90 @@ mod tests {
555
562
fn external_docs_doc_url_crate ( ) {
556
563
check_external_docs (
557
564
r#"
558
- //- /main.rs crate:main deps:test
559
- use test $0::Foo;
560
- //- /lib.rs crate:test
565
+ //- /main.rs crate:main deps:foo
566
+ use foo $0::Foo;
567
+ //- /lib.rs crate:foo
561
568
pub struct Foo;
562
569
"# ,
563
- expect ! [ [ r#"https://docs.rs/test/*/test/index.html"# ] ] ,
570
+ expect ! [ [ r#"https://docs.rs/foo/*/foo/index.html"# ] ] ,
571
+ ) ;
572
+ }
573
+
574
+ #[ test]
575
+ fn external_docs_doc_url_std_crate ( ) {
576
+ check_external_docs (
577
+ r#"
578
+ //- /main.rs crate:std
579
+ use self$0;
580
+ "# ,
581
+ expect ! [ [ r#"https://doc.rust-lang.org/nightly/std/index.html"# ] ] ,
564
582
) ;
565
583
}
566
584
567
585
#[ test]
568
586
fn external_docs_doc_url_struct ( ) {
569
587
check_external_docs (
570
588
r#"
589
+ //- /main.rs crate:foo
571
590
pub struct Fo$0o;
572
591
"# ,
573
- expect ! [ [ r#"https://docs.rs/test /*/test /struct.Foo.html"# ] ] ,
592
+ expect ! [ [ r#"https://docs.rs/foo /*/foo /struct.Foo.html"# ] ] ,
574
593
) ;
575
594
}
576
595
577
596
#[ test]
578
597
fn external_docs_doc_url_struct_field ( ) {
579
598
check_external_docs (
580
599
r#"
600
+ //- /main.rs crate:foo
581
601
pub struct Foo {
582
602
field$0: ()
583
603
}
584
604
"# ,
585
- expect ! [ [ r##"https://docs.rs/test /*/test /struct.Foo.html#structfield.field"## ] ] ,
605
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /struct.Foo.html#structfield.field"## ] ] ,
586
606
) ;
587
607
}
588
608
589
609
#[ test]
590
610
fn external_docs_doc_url_fn ( ) {
591
611
check_external_docs (
592
612
r#"
613
+ //- /main.rs crate:foo
593
614
pub fn fo$0o() {}
594
615
"# ,
595
- expect ! [ [ r## "https://docs.rs/test /*/test /fn.foo.html"# # ] ] ,
616
+ expect ! [ [ r#"https://docs.rs/foo /*/foo /fn.foo.html"# ] ] ,
596
617
) ;
597
618
}
598
619
599
620
#[ test]
600
621
fn external_docs_doc_url_impl_assoc ( ) {
601
622
check_external_docs (
602
623
r#"
624
+ //- /main.rs crate:foo
603
625
pub struct Foo;
604
626
impl Foo {
605
627
pub fn method$0() {}
606
628
}
607
629
"# ,
608
- expect ! [ [ r##"https://docs.rs/test /*/test /struct.Foo.html#method.method"## ] ] ,
630
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /struct.Foo.html#method.method"## ] ] ,
609
631
) ;
610
632
check_external_docs (
611
633
r#"
634
+ //- /main.rs crate:foo
612
635
pub struct Foo;
613
636
impl Foo {
614
637
const CONST$0: () = ();
615
638
}
616
639
"# ,
617
- expect ! [ [ r##"https://docs.rs/test /*/test /struct.Foo.html#associatedconstant.CONST"## ] ] ,
640
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /struct.Foo.html#associatedconstant.CONST"## ] ] ,
618
641
) ;
619
642
}
620
643
621
644
#[ test]
622
645
fn external_docs_doc_url_impl_trait_assoc ( ) {
623
646
check_external_docs (
624
647
r#"
648
+ //- /main.rs crate:foo
625
649
pub struct Foo;
626
650
pub trait Trait {
627
651
fn method() {}
@@ -630,10 +654,11 @@ impl Trait for Foo {
630
654
pub fn method$0() {}
631
655
}
632
656
"# ,
633
- expect ! [ [ r##"https://docs.rs/test /*/test /struct.Foo.html#method.method"## ] ] ,
657
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /struct.Foo.html#method.method"## ] ] ,
634
658
) ;
635
659
check_external_docs (
636
660
r#"
661
+ //- /main.rs crate:foo
637
662
pub struct Foo;
638
663
pub trait Trait {
639
664
const CONST: () = ();
@@ -642,10 +667,11 @@ impl Trait for Foo {
642
667
const CONST$0: () = ();
643
668
}
644
669
"# ,
645
- expect ! [ [ r##"https://docs.rs/test /*/test /struct.Foo.html#associatedconstant.CONST"## ] ] ,
670
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /struct.Foo.html#associatedconstant.CONST"## ] ] ,
646
671
) ;
647
672
check_external_docs (
648
673
r#"
674
+ //- /main.rs crate:foo
649
675
pub struct Foo;
650
676
pub trait Trait {
651
677
type Type;
@@ -654,64 +680,70 @@ impl Trait for Foo {
654
680
type Type$0 = ();
655
681
}
656
682
"# ,
657
- expect ! [ [ r##"https://docs.rs/test /*/test /struct.Foo.html#associatedtype.Type"## ] ] ,
683
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /struct.Foo.html#associatedtype.Type"## ] ] ,
658
684
) ;
659
685
}
660
686
661
687
#[ test]
662
688
fn external_docs_doc_url_trait_assoc ( ) {
663
689
check_external_docs (
664
690
r#"
691
+ //- /main.rs crate:foo
665
692
pub trait Foo {
666
693
fn method$0();
667
694
}
668
695
"# ,
669
- expect ! [ [ r##"https://docs.rs/test /*/test /trait.Foo.html#tymethod.method"## ] ] ,
696
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /trait.Foo.html#tymethod.method"## ] ] ,
670
697
) ;
671
698
check_external_docs (
672
699
r#"
700
+ //- /main.rs crate:foo
673
701
pub trait Foo {
674
702
const CONST$0: ();
675
703
}
676
704
"# ,
677
- expect ! [ [ r##"https://docs.rs/test /*/test /trait.Foo.html#associatedconstant.CONST"## ] ] ,
705
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /trait.Foo.html#associatedconstant.CONST"## ] ] ,
678
706
) ;
679
707
check_external_docs (
680
708
r#"
709
+ //- /main.rs crate:foo
681
710
pub trait Foo {
682
711
type Type$0;
683
712
}
684
713
"# ,
685
- expect ! [ [ r##"https://docs.rs/test /*/test /trait.Foo.html#associatedtype.Type"## ] ] ,
714
+ expect ! [ [ r##"https://docs.rs/foo /*/foo /trait.Foo.html#associatedtype.Type"## ] ] ,
686
715
) ;
687
716
}
688
717
689
718
#[ test]
690
719
fn external_docs_trait ( ) {
691
720
check_external_docs (
692
721
r#"
722
+ //- /main.rs crate:foo
693
723
trait Trait$0 {}
694
724
"# ,
695
- expect ! [ [ r#"https://docs.rs/test /*/test /trait.Trait.html"# ] ] ,
725
+ expect ! [ [ r#"https://docs.rs/foo /*/foo /trait.Trait.html"# ] ] ,
696
726
)
697
727
}
698
728
699
729
#[ test]
700
730
fn external_docs_module ( ) {
701
731
check_external_docs (
702
732
r#"
733
+ //- /main.rs crate:foo
703
734
pub mod foo {
704
735
pub mod ba$0r {}
705
736
}
706
737
"# ,
707
- expect ! [ [ r#"https://docs.rs/test /*/test /foo/bar/index.html"# ] ] ,
738
+ expect ! [ [ r#"https://docs.rs/foo /*/foo /foo/bar/index.html"# ] ] ,
708
739
)
709
740
}
710
741
711
742
#[ test]
712
743
fn external_docs_reexport_order ( ) {
713
744
check_external_docs (
714
745
r#"
746
+ //- /main.rs crate:foo
715
747
pub mod wrapper {
716
748
pub use module::Item;
717
749
@@ -724,7 +756,7 @@ fn foo() {
724
756
let bar: wrapper::It$0em;
725
757
}
726
758
"# ,
727
- expect ! [ [ r#"https://docs.rs/test /*/test /wrapper/module/struct.Item.html"# ] ] ,
759
+ expect ! [ [ r#"https://docs.rs/foo /*/foo /wrapper/module/struct.Item.html"# ] ] ,
728
760
)
729
761
}
730
762
@@ -753,6 +785,7 @@ trait Trait$0 {
753
785
fn rewrite_html_root_url ( ) {
754
786
check_rewrite (
755
787
r#"
788
+ //- /main.rs crate:foo
756
789
#![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
757
790
758
791
pub mod foo {
@@ -761,7 +794,7 @@ pub mod foo {
761
794
/// [Foo](foo::Foo)
762
795
pub struct B$0ar
763
796
"# ,
764
- expect ! [ [ r#"[Foo](https://example.com/test /foo/struct.Foo.html)"# ] ] ,
797
+ expect ! [ [ r#"[Foo](https://example.com/foo /foo/struct.Foo.html)"# ] ] ,
765
798
) ;
766
799
}
767
800
@@ -771,6 +804,7 @@ pub struct B$0ar
771
804
// [Foo](https://docs.rs/test/*/test/struct.Foo.html)
772
805
check_rewrite (
773
806
r#"
807
+ //- /main.rs crate:foo
774
808
pub struct Foo {
775
809
/// [Foo](struct.Foo.html)
776
810
fie$0ld: ()
@@ -784,40 +818,45 @@ pub struct Foo {
784
818
fn rewrite_struct ( ) {
785
819
check_rewrite (
786
820
r#"
821
+ //- /main.rs crate:foo
787
822
/// [Foo]
788
823
pub struct $0Foo;
789
824
"# ,
790
- expect ! [ [ r#"[Foo](https://docs.rs/test /*/test /struct.Foo.html)"# ] ] ,
825
+ expect ! [ [ r#"[Foo](https://docs.rs/foo /*/foo /struct.Foo.html)"# ] ] ,
791
826
) ;
792
827
check_rewrite (
793
828
r#"
829
+ //- /main.rs crate:foo
794
830
/// [`Foo`]
795
831
pub struct $0Foo;
796
832
"# ,
797
- expect ! [ [ r#"[`Foo`](https://docs.rs/test /*/test /struct.Foo.html)"# ] ] ,
833
+ expect ! [ [ r#"[`Foo`](https://docs.rs/foo /*/foo /struct.Foo.html)"# ] ] ,
798
834
) ;
799
835
check_rewrite (
800
836
r#"
837
+ //- /main.rs crate:foo
801
838
/// [Foo](struct.Foo.html)
802
839
pub struct $0Foo;
803
840
"# ,
804
- expect ! [ [ r#"[Foo](https://docs.rs/test /*/test /struct.Foo.html)"# ] ] ,
841
+ expect ! [ [ r#"[Foo](https://docs.rs/foo /*/foo /struct.Foo.html)"# ] ] ,
805
842
) ;
806
843
check_rewrite (
807
844
r#"
845
+ //- /main.rs crate:foo
808
846
/// [struct Foo](struct.Foo.html)
809
847
pub struct $0Foo;
810
848
"# ,
811
- expect ! [ [ r#"[struct Foo](https://docs.rs/test /*/test /struct.Foo.html)"# ] ] ,
849
+ expect ! [ [ r#"[struct Foo](https://docs.rs/foo /*/foo /struct.Foo.html)"# ] ] ,
812
850
) ;
813
851
check_rewrite (
814
852
r#"
853
+ //- /main.rs crate:foo
815
854
/// [my Foo][foo]
816
855
///
817
856
/// [foo]: Foo
818
857
pub struct $0Foo;
819
858
"# ,
820
- expect ! [ [ r#"[my Foo](https://docs.rs/test /*/test /struct.Foo.html)"# ] ] ,
859
+ expect ! [ [ r#"[my Foo](https://docs.rs/foo /*/foo /struct.Foo.html)"# ] ] ,
821
860
) ;
822
861
}
823
862
0 commit comments