@@ -19,6 +19,21 @@ use rustc_span::symbol::Symbol;
1919use super :: format:: { self , Buffer } ;
2020use super :: render:: { LightSpan , LinkFromSrc } ;
2121
22+ /// This type is needed in case we want to render links on items to allow to go to their definition.
23+ crate struct ContextInfo < ' a , ' b , ' c > {
24+ crate context : & ' a Context < ' b > ,
25+ /// This represents the "lo" bytes of the current file we're rendering. To get a [`Span`] from
26+ /// it, you just need to add add your current byte position in the string and its length (to get
27+ /// the "hi" part).
28+ ///
29+ /// This is used to create a [`LightSpan`] which is then used as an index in the `span_map` in
30+ /// order to retrieve the definition's [`Span`] (which is used to generate the URL).
31+ crate file_span_lo : u32 ,
32+ /// This field is used to know "how far" from the top of the directory we are to link to either
33+ /// documentation pages or other source pages.
34+ crate root_path : & ' c str ,
35+ }
36+
2237/// Highlights `src`, returning the HTML output.
2338crate fn render_with_highlighting (
2439 src : & str ,
@@ -28,9 +43,7 @@ crate fn render_with_highlighting(
2843 tooltip : Option < ( Option < Edition > , & str ) > ,
2944 edition : Edition ,
3045 extra_content : Option < Buffer > ,
31- file_span_lo : u32 ,
32- context : Option < & Context < ' _ > > ,
33- root_path : & str ,
46+ context_info : Option < ContextInfo < ' _ , ' _ , ' _ > > ,
3447) {
3548 debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
3649 if let Some ( ( edition_info, class) ) = tooltip {
@@ -47,7 +60,7 @@ crate fn render_with_highlighting(
4760 }
4861
4962 write_header ( out, class, extra_content) ;
50- write_code ( out, & src, edition, file_span_lo , context , root_path ) ;
63+ write_code ( out, & src, edition, context_info ) ;
5164 write_footer ( out, playground_button) ;
5265}
5366
@@ -69,37 +82,28 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
6982///
7083/// Some explanations on the last arguments:
7184///
72- /// In case we are rendering a code block and not a source code file, `file_span_lo` value doesn't
73- /// matter and `context` will be `None`. To put it more simply: if `context ` is `None`, the code
74- /// won't try to generate links to an ident definition.
85+ /// In case we are rendering a code block and not a source code file, `context_info` will be `None`.
86+ /// To put it more simply: if `context_info ` is `None`, the code won't try to generate links to an
87+ /// item definition.
7588///
7689/// More explanations about spans and how we use them here are provided in the
7790/// [`LightSpan::new_in_file`] function documentation about how it works.
78- ///
79- /// As for `root_path`, it's used to know "how far" from the top of the directory we are to link
80- /// to either documentation pages or other source pages.
81- ///
82- /// Same as `file_span_lo`: its value doesn't matter in case you are not rendering a source code
83- /// file.
8491fn write_code (
8592 out : & mut Buffer ,
8693 src : & str ,
8794 edition : Edition ,
88- file_span_lo : u32 ,
89- context : Option < & Context < ' _ > > ,
90- root_path : & str ,
95+ context_info : Option < ContextInfo < ' _ , ' _ , ' _ > > ,
9196) {
9297 // This replace allows to fix how the code source with DOS backline characters is displayed.
9398 let src = src. replace ( "\r \n " , "\n " ) ;
94- Classifier :: new ( & src, edition, file_span_lo) . highlight ( & mut |highlight| {
95- match highlight {
96- Highlight :: Token { text, class } => {
97- string ( out, Escape ( text) , class, context, root_path)
98- }
99- Highlight :: EnterSpan { class } => enter_span ( out, class) ,
100- Highlight :: ExitSpan => exit_span ( out) ,
101- } ;
102- } ) ;
99+ Classifier :: new ( & src, edition, context_info. as_ref ( ) . map ( |c| c. file_span_lo ) . unwrap_or ( 0 ) )
100+ . highlight ( & mut |highlight| {
101+ match highlight {
102+ Highlight :: Token { text, class } => string ( out, Escape ( text) , class, & context_info) ,
103+ Highlight :: EnterSpan { class } => enter_span ( out, class) ,
104+ Highlight :: ExitSpan => exit_span ( out) ,
105+ } ;
106+ } ) ;
103107}
104108
105109fn write_footer ( out : & mut Buffer , playground_button : Option < & str > ) {
@@ -540,8 +544,7 @@ fn string<T: Display>(
540544 out : & mut Buffer ,
541545 text : T ,
542546 klass : Option < Class > ,
543- context : Option < & Context < ' _ > > ,
544- root_path : & str ,
547+ context_info : & Option < ContextInfo < ' _ , ' _ , ' _ > > ,
545548) {
546549 let klass = match klass {
547550 None => return write ! ( out, "{}" , text) ,
@@ -570,14 +573,19 @@ fn string<T: Display>(
570573 path
571574 } ) ;
572575 }
573- if let Some ( context) = context {
574- if let Some ( href) =
575- context. shared . span_correspondance_map . get ( & def_span) . and_then ( |href| {
576+ if let Some ( context_info) = context_info {
577+ if let Some ( href) = context_info
578+ . context
579+ . shared
580+ . span_correspondance_map
581+ . get ( & def_span)
582+ . and_then ( |href| {
583+ let context = context_info. context ;
576584 match href {
577585 LinkFromSrc :: Local ( span) => {
578586 context
579587 . href_from_span ( clean:: Span :: wrap_raw ( * span) )
580- . map ( |s| format ! ( "{}{}" , root_path, s) )
588+ . map ( |s| format ! ( "{}{}" , context_info . root_path, s) )
581589 }
582590 LinkFromSrc :: External ( def_id) => {
583591 format:: href ( * def_id, context) . map ( |( url, _, _) | url)
0 commit comments