@@ -396,37 +396,40 @@ fn format_code_block(
396
396
. unwrap_or_else ( || formatted. snippet . len ( ) ) ;
397
397
let mut is_indented = true ;
398
398
let indent_str = Indent :: from_width ( config, config. tab_spaces ( ) ) . to_string ( config) ;
399
- for ( kind, ref line) in LineClasses :: new ( & formatted. snippet [ FN_MAIN_PREFIX . len ( ) ..block_len] ) {
400
- if !is_first {
401
- result. push ( '\n' ) ;
402
- } else {
403
- is_first = false ;
404
- }
405
- let trimmed_line = if !is_indented {
406
- line
407
- } else if line. len ( ) > config. max_width ( ) {
408
- // If there are lines that are larger than max width, we cannot tell
409
- // whether we have succeeded but have some comments or strings that
410
- // are too long, or we have failed to format code block. We will be
411
- // conservative and just return `None` in this case.
412
- return None ;
413
- } else if line. len ( ) > indent_str. len ( ) {
414
- // Make sure that the line has leading whitespaces.
415
- if line. starts_with ( indent_str. as_ref ( ) ) {
416
- let offset = if config. hard_tabs ( ) {
417
- 1
399
+
400
+ if let Some ( to_format) = formatted. snippet . get ( FN_MAIN_PREFIX . len ( ) ..block_len) {
401
+ for ( kind, ref line) in LineClasses :: new ( to_format) {
402
+ if !is_first {
403
+ result. push ( '\n' ) ;
404
+ } else {
405
+ is_first = false ;
406
+ }
407
+ let trimmed_line = if !is_indented {
408
+ line
409
+ } else if line. len ( ) > config. max_width ( ) {
410
+ // If there are lines that are larger than max width, we cannot tell
411
+ // whether we have succeeded but have some comments or strings that
412
+ // are too long, or we have failed to format code block. We will be
413
+ // conservative and just return `None` in this case.
414
+ return None ;
415
+ } else if line. len ( ) > indent_str. len ( ) {
416
+ // Make sure that the line has leading whitespaces.
417
+ if line. starts_with ( indent_str. as_ref ( ) ) {
418
+ let offset = if config. hard_tabs ( ) {
419
+ 1
420
+ } else {
421
+ config. tab_spaces ( )
422
+ } ;
423
+ & line[ offset..]
418
424
} else {
419
- config. tab_spaces ( )
420
- } ;
421
- & line[ offset..]
425
+ line
426
+ }
422
427
} else {
423
428
line
424
- }
425
- } else {
426
- line
427
- } ;
428
- result. push_str ( trimmed_line) ;
429
- is_indented = indent_next_line ( kind, line, config) ;
429
+ } ;
430
+ result. push_str ( trimmed_line) ;
431
+ is_indented = indent_next_line ( kind, line, config) ;
432
+ }
430
433
}
431
434
Some ( FormattedSnippet {
432
435
snippet : result,
0 commit comments