@@ -19,6 +19,21 @@ use rustc_span::symbol::Symbol;
19
19
use super :: format:: { self , Buffer } ;
20
20
use super :: render:: { LightSpan , LinkFromSrc } ;
21
21
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
+
22
37
/// Highlights `src`, returning the HTML output.
23
38
crate fn render_with_highlighting (
24
39
src : & str ,
@@ -28,9 +43,7 @@ crate fn render_with_highlighting(
28
43
tooltip : Option < ( Option < Edition > , & str ) > ,
29
44
edition : Edition ,
30
45
extra_content : Option < Buffer > ,
31
- file_span_lo : u32 ,
32
- context : Option < & Context < ' _ > > ,
33
- root_path : & str ,
46
+ context_info : Option < ContextInfo < ' _ , ' _ , ' _ > > ,
34
47
) {
35
48
debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
36
49
if let Some ( ( edition_info, class) ) = tooltip {
@@ -47,7 +60,7 @@ crate fn render_with_highlighting(
47
60
}
48
61
49
62
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 ) ;
51
64
write_footer ( out, playground_button) ;
52
65
}
53
66
@@ -69,37 +82,28 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
69
82
///
70
83
/// Some explanations on the last arguments:
71
84
///
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.
75
88
///
76
89
/// More explanations about spans and how we use them here are provided in the
77
90
/// [`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.
84
91
fn write_code (
85
92
out : & mut Buffer ,
86
93
src : & str ,
87
94
edition : Edition ,
88
- file_span_lo : u32 ,
89
- context : Option < & Context < ' _ > > ,
90
- root_path : & str ,
95
+ context_info : Option < ContextInfo < ' _ , ' _ , ' _ > > ,
91
96
) {
92
97
// This replace allows to fix how the code source with DOS backline characters is displayed.
93
98
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
+ } ) ;
103
107
}
104
108
105
109
fn write_footer ( out : & mut Buffer , playground_button : Option < & str > ) {
@@ -540,8 +544,7 @@ fn string<T: Display>(
540
544
out : & mut Buffer ,
541
545
text : T ,
542
546
klass : Option < Class > ,
543
- context : Option < & Context < ' _ > > ,
544
- root_path : & str ,
547
+ context_info : & Option < ContextInfo < ' _ , ' _ , ' _ > > ,
545
548
) {
546
549
let klass = match klass {
547
550
None => return write ! ( out, "{}" , text) ,
@@ -570,14 +573,19 @@ fn string<T: Display>(
570
573
path
571
574
} ) ;
572
575
}
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 ;
576
584
match href {
577
585
LinkFromSrc :: Local ( span) => {
578
586
context
579
587
. href_from_span ( clean:: Span :: wrap_raw ( * span) )
580
- . map ( |s| format ! ( "{}{}" , root_path, s) )
588
+ . map ( |s| format ! ( "{}{}" , context_info . root_path, s) )
581
589
}
582
590
LinkFromSrc :: External ( def_id) => {
583
591
format:: href ( * def_id, context) . map ( |( url, _, _) | url)
0 commit comments