@@ -63,6 +63,24 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
63
63
}
64
64
}
65
65
66
+ /// Convert the given `src` source code into HTML by adding classes for highlighting.
67
+ ///
68
+ /// This code is used to render code blocks (in the documentation) as well as the source code pages.
69
+ ///
70
+ /// Some explanations on the last arguments:
71
+ ///
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.
75
+ ///
76
+ /// More explanations about spans and how we use them here are provided in the
77
+ /// [`local_span_to_global_span`] 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.
66
84
fn write_code (
67
85
out : & mut Buffer ,
68
86
src : & str ,
@@ -135,6 +153,8 @@ impl Class {
135
153
}
136
154
}
137
155
156
+ /// In case this is an item which can be converted into a link to a definition, it'll contain
157
+ /// a "span" (a tuple representing `(lo, hi)` equivalent of `Span`).
138
158
fn get_span ( self ) -> Option < ( u32 , u32 ) > {
139
159
match self {
140
160
Self :: Ident ( sp) | Self :: Self_ ( sp) => Some ( sp) ,
@@ -166,7 +186,7 @@ impl Iterator for TokenIter<'a> {
166
186
}
167
187
}
168
188
169
- /// Returns `None` if this is a `Class::Ident` .
189
+ /// Classifies into identifier class; returns `None` if this is a non-keyword identifier .
170
190
fn get_real_ident_class ( text : & str , edition : Edition , allow_path_keywords : bool ) -> Option < Class > {
171
191
let ignore: & [ & str ] =
172
192
if allow_path_keywords { & [ "self" , "Self" , "super" , "crate" ] } else { & [ "self" , "Self" ] } ;
@@ -181,7 +201,20 @@ fn get_real_ident_class(text: &str, edition: Edition, allow_path_keywords: bool)
181
201
} )
182
202
}
183
203
184
- fn move_span ( file_span_lo : u32 , start : u32 , end : u32 ) -> ( u32 , u32 ) {
204
+ /// Before explaining what this function does, some global explanations on rust's `Span`:
205
+ ///
206
+ /// Each source code file is stored in the source map in the compiler and has a
207
+ /// `lo` and a `hi` (lowest and highest bytes in this source map which can be seen as one huge
208
+ /// string to simplify things). So in this case, this represents the starting byte of the current
209
+ /// file. It'll be used later on to retrieve the "definition span" from the
210
+ /// `span_correspondance_map` (which is inside `context`).
211
+ ///
212
+ /// This when we transform the "span" we have from reading the input into a "span" which can be
213
+ /// used as index to the `span_correspondance_map` to get the definition of this item.
214
+ ///
215
+ /// So in here, `file_span_lo` is representing the "lo" byte in the global source map, and to make
216
+ /// our "span" works in there, we simply add `file_span_lo` to our values.
217
+ fn local_span_to_global_span ( file_span_lo : u32 , start : u32 , end : u32 ) -> ( u32 , u32 ) {
185
218
( start + file_span_lo, end + file_span_lo)
186
219
}
187
220
@@ -199,6 +232,9 @@ struct Classifier<'a> {
199
232
}
200
233
201
234
impl < ' a > Classifier < ' a > {
235
+ /// Takes as argument the source code to HTML-ify, the rust edition to use and the source code
236
+ /// file "lo" byte which we be used later on by the `span_correspondance_map`. More explanations
237
+ /// are provided in the [`local_span_to_global_span`] function documentation about how it works.
202
238
fn new ( src : & str , edition : Edition , file_span_lo : u32 ) -> Classifier < ' _ > {
203
239
let tokens = TokenIter { src } . peekable ( ) ;
204
240
Classifier {
@@ -263,7 +299,10 @@ impl<'a> Classifier<'a> {
263
299
}
264
300
}
265
301
266
- /// Wraps the tokens iteration to ensure that the byte_pos is always correct.
302
+ /// Wraps the tokens iteration to ensure that the `byte_pos` is always correct.
303
+ ///
304
+ /// It returns the token's kind, the token as a string and its byte position in the source
305
+ /// string.
267
306
fn next ( & mut self ) -> Option < ( TokenKind , & ' a str , u32 ) > {
268
307
if let Some ( ( kind, text) ) = self . tokens . next ( ) {
269
308
let before = self . byte_pos ;
@@ -306,8 +345,12 @@ impl<'a> Classifier<'a> {
306
345
}
307
346
}
308
347
309
- /// Single step of highlighting. This will classify `token`, but maybe also
310
- /// a couple of following ones as well.
348
+ /// Single step of highlighting. This will classify `token`, but maybe also a couple of
349
+ /// following ones as well.
350
+ ///
351
+ /// `before` is the position of the given token in the `source` string and is used as "lo" byte
352
+ /// in case we want to try to generate a link for this token using the
353
+ /// `span_correspondance_map`.
311
354
fn advance (
312
355
& mut self ,
313
356
token : TokenKind ,
@@ -453,22 +496,24 @@ impl<'a> Classifier<'a> {
453
496
self . in_macro_nonterminal = false ;
454
497
Class :: MacroNonTerminal
455
498
}
456
- "self" | "Self" => Class :: Self_ ( move_span (
499
+ "self" | "Self" => Class :: Self_ ( local_span_to_global_span (
457
500
self . file_span_lo ,
458
501
before,
459
502
before + text. len ( ) as u32 ,
460
503
) ) ,
461
- _ => Class :: Ident ( move_span (
504
+ _ => Class :: Ident ( local_span_to_global_span (
462
505
self . file_span_lo ,
463
506
before,
464
507
before + text. len ( ) as u32 ,
465
508
) ) ,
466
509
} ,
467
510
Some ( c) => c,
468
511
} ,
469
- TokenKind :: RawIdent | TokenKind :: UnknownPrefix => {
470
- Class :: Ident ( move_span ( self . file_span_lo , before, before + text. len ( ) as u32 ) )
471
- }
512
+ TokenKind :: RawIdent | TokenKind :: UnknownPrefix => Class :: Ident ( local_span_to_global_span (
513
+ self . file_span_lo ,
514
+ before,
515
+ before + text. len ( ) as u32 ,
516
+ ) ) ,
472
517
TokenKind :: Lifetime { .. } => Class :: Lifetime ,
473
518
} ;
474
519
// Anything that didn't return above is the simple case where we the
@@ -501,8 +546,13 @@ fn exit_span(out: &mut Buffer) {
501
546
/// enter_span(Foo), string("text", None), exit_span()
502
547
/// string("text", Foo)
503
548
/// ```
549
+ ///
504
550
/// The latter can be thought of as a shorthand for the former, which is more
505
551
/// flexible.
552
+ ///
553
+ /// Note that if `context` is not `None` and that the given `klass` contains a `Span`, the function
554
+ /// will then try to find this `span` in the `span_correspondance_map`. If found, it'll then
555
+ /// generate a link for this element (which corresponds to where its definition is located).
506
556
fn string < T : Display > (
507
557
out : & mut Buffer ,
508
558
text : T ,
0 commit comments