@@ -63,6 +63,24 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
6363 }
6464}
6565
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.
6684fn write_code (
6785 out : & mut Buffer ,
6886 src : & str ,
@@ -135,6 +153,8 @@ impl Class {
135153 }
136154 }
137155
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`).
138158 fn get_span ( self ) -> Option < ( u32 , u32 ) > {
139159 match self {
140160 Self :: Ident ( sp) | Self :: Self_ ( sp) => Some ( sp) ,
@@ -166,7 +186,7 @@ impl Iterator for TokenIter<'a> {
166186 }
167187}
168188
169- /// Returns `None` if this is a `Class::Ident` .
189+ /// Classifies into identifier class; returns `None` if this is a non-keyword identifier .
170190fn get_real_ident_class ( text : & str , edition : Edition , allow_path_keywords : bool ) -> Option < Class > {
171191 let ignore: & [ & str ] =
172192 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)
181201 } )
182202}
183203
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 ) {
185218 ( start + file_span_lo, end + file_span_lo)
186219}
187220
@@ -199,6 +232,9 @@ struct Classifier<'a> {
199232}
200233
201234impl < ' 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.
202238 fn new ( src : & str , edition : Edition , file_span_lo : u32 ) -> Classifier < ' _ > {
203239 let tokens = TokenIter { src } . peekable ( ) ;
204240 Classifier {
@@ -263,7 +299,10 @@ impl<'a> Classifier<'a> {
263299 }
264300 }
265301
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.
267306 fn next ( & mut self ) -> Option < ( TokenKind , & ' a str , u32 ) > {
268307 if let Some ( ( kind, text) ) = self . tokens . next ( ) {
269308 let before = self . byte_pos ;
@@ -306,8 +345,12 @@ impl<'a> Classifier<'a> {
306345 }
307346 }
308347
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`.
311354 fn advance (
312355 & mut self ,
313356 token : TokenKind ,
@@ -453,22 +496,24 @@ impl<'a> Classifier<'a> {
453496 self . in_macro_nonterminal = false ;
454497 Class :: MacroNonTerminal
455498 }
456- "self" | "Self" => Class :: Self_ ( move_span (
499+ "self" | "Self" => Class :: Self_ ( local_span_to_global_span (
457500 self . file_span_lo ,
458501 before,
459502 before + text. len ( ) as u32 ,
460503 ) ) ,
461- _ => Class :: Ident ( move_span (
504+ _ => Class :: Ident ( local_span_to_global_span (
462505 self . file_span_lo ,
463506 before,
464507 before + text. len ( ) as u32 ,
465508 ) ) ,
466509 } ,
467510 Some ( c) => c,
468511 } ,
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+ ) ) ,
472517 TokenKind :: Lifetime { .. } => Class :: Lifetime ,
473518 } ;
474519 // Anything that didn't return above is the simple case where we the
@@ -501,8 +546,13 @@ fn exit_span(out: &mut Buffer) {
501546/// enter_span(Foo), string("text", None), exit_span()
502547/// string("text", Foo)
503548/// ```
549+ ///
504550/// The latter can be thought of as a shorthand for the former, which is more
505551/// 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).
506556fn string < T : Display > (
507557 out : & mut Buffer ,
508558 text : T ,
0 commit comments