@@ -197,31 +197,45 @@ impl CodeSuggestion {
197
197
198
198
use rustc_span:: { CharPos , Pos } ;
199
199
200
- /// Append to a buffer the remainder of the line of existing source code, and return the
201
- /// count of lines that have been added for accurate highlighting.
200
+ /// Extracts a substring from the provided `line_opt` based on the specified low and high indices,
201
+ /// appends it to the given buffer `buf`, and returns the count of newline characters in the substring
202
+ /// for accurate highlighting.
203
+ /// If `line_opt` is `None`, a newline character is appended to the buffer, and 0 is returned.
204
+ ///
205
+ /// ## Returns
206
+ ///
207
+ /// The count of newline characters in the extracted substring.
202
208
fn push_trailing (
203
209
buf : & mut String ,
204
210
line_opt : Option < & Cow < ' _ , str > > ,
205
211
lo : & Loc ,
206
212
hi_opt : Option < & Loc > ,
207
213
) -> usize {
208
214
let mut line_count = 0 ;
215
+ // Convert CharPos to Usize, as CharPose is character offset
216
+ // Extract low index and high index
209
217
let ( lo, hi_opt) = ( lo. col . to_usize ( ) , hi_opt. map ( |hi| hi. col . to_usize ( ) ) ) ;
210
218
if let Some ( line) = line_opt {
211
219
if let Some ( lo) = line. char_indices ( ) . map ( |( i, _) | i) . nth ( lo) {
220
+ // Get high index while account for rare unicode and emoji with char_indices
212
221
let hi_opt = hi_opt. and_then ( |hi| line. char_indices ( ) . map ( |( i, _) | i) . nth ( hi) ) ;
213
222
match hi_opt {
223
+ // If high index exist, take string from low to high index
214
224
Some ( hi) if hi > lo => {
225
+ // count how many '\n' exist
215
226
line_count = line[ lo..hi] . matches ( '\n' ) . count ( ) ;
216
227
buf. push_str ( & line[ lo..hi] )
217
228
}
218
229
Some ( _) => ( ) ,
230
+ // If high index absence, take string from low index till end string.len
219
231
None => {
232
+ // count how many '\n' exist
220
233
line_count = line[ lo..] . matches ( '\n' ) . count ( ) ;
221
234
buf. push_str ( & line[ lo..] )
222
235
}
223
236
}
224
237
}
238
+ // If high index is None
225
239
if hi_opt. is_none ( ) {
226
240
buf. push ( '\n' ) ;
227
241
}
0 commit comments