@@ -263,6 +263,41 @@ impl EmitterWriter {
263
263
264
264
draw_col_separator ( buffer, line_offset, width_offset - 2 ) ;
265
265
266
+ // Special case when there's only one annotation involved, it is the start of a multiline
267
+ // span and there's no text at the beginning of the code line. Instead of doing the whole
268
+ // graph:
269
+ //
270
+ // 2 | fn foo() {
271
+ // | _^
272
+ // 3 | |
273
+ // 4 | | }
274
+ // | |_^ test
275
+ //
276
+ // we simplify the output to:
277
+ //
278
+ // 2 | / fn foo() {
279
+ // 3 | |
280
+ // 4 | | }
281
+ // | |_^ test
282
+ if line. annotations . len ( ) == 1 {
283
+ if let Some ( ref ann) = line. annotations . get ( 0 ) {
284
+ if let AnnotationType :: MultilineStart ( depth) = ann. annotation_type {
285
+ if source_string[ 0 ..ann. start_col ] . trim ( ) == "" {
286
+ let style = if ann. is_primary {
287
+ Style :: UnderlinePrimary
288
+ } else {
289
+ Style :: UnderlineSecondary
290
+ } ;
291
+ buffer. putc ( line_offset,
292
+ width_offset + depth - 1 ,
293
+ '/' ,
294
+ style) ;
295
+ return vec ! [ ( depth, style) ] ;
296
+ }
297
+ }
298
+ }
299
+ }
300
+
266
301
// We want to display like this:
267
302
//
268
303
// vec.push(vec.pop().unwrap());
@@ -355,10 +390,8 @@ impl EmitterWriter {
355
390
for ( i, annotation) in annotations. iter ( ) . enumerate ( ) {
356
391
for ( j, next) in annotations. iter ( ) . enumerate ( ) {
357
392
if overlaps ( next, annotation, 0 ) // This label overlaps with another one and both
358
- && !annotation. is_line ( ) // take space (they have text and are not
359
- && !next. is_line ( ) // multiline lines).
360
- && annotation. has_label ( )
361
- && j > i
393
+ && annotation. has_label ( ) // take space (they have text and are not
394
+ && j > i // multiline lines).
362
395
&& p == 0 // We're currently on the first line, move the label one line down
363
396
{
364
397
// This annotation needs a new line in the output.
@@ -374,7 +407,7 @@ impl EmitterWriter {
374
407
} else {
375
408
0
376
409
} ;
377
- if overlaps ( next, annotation, l) // Do not allow two labels to be in the same
410
+ if ( overlaps ( next, annotation, l) // Do not allow two labels to be in the same
378
411
// line if they overlap including padding, to
379
412
// avoid situations like:
380
413
//
@@ -383,11 +416,18 @@ impl EmitterWriter {
383
416
// | |
384
417
// fn_spanx_span
385
418
//
386
- && !annotation. is_line ( ) // Do not add a new line if this annotation
387
- && !next. is_line ( ) // or the next are vertical line placeholders.
388
419
&& annotation. has_label ( ) // Both labels must have some text, otherwise
389
- && next. has_label ( ) // they are not overlapping.
420
+ && next. has_label ( ) ) // they are not overlapping.
421
+ // Do not add a new line if this annotation
422
+ // or the next are vertical line placeholders.
423
+ || ( annotation. takes_space ( ) // If either this or the next annotation is
424
+ && next. has_label ( ) ) // multiline start/end, move it to a new line
425
+ || ( annotation. has_label ( ) // so as not to overlap the orizontal lines.
426
+ && next. takes_space ( ) )
427
+ || ( annotation. takes_space ( )
428
+ && next. takes_space ( ) )
390
429
{
430
+ // This annotation needs a new line in the output.
391
431
p += 1 ;
392
432
break ;
393
433
}
@@ -397,6 +437,7 @@ impl EmitterWriter {
397
437
line_len = p;
398
438
}
399
439
}
440
+
400
441
if line_len != 0 {
401
442
line_len += 1 ;
402
443
}
@@ -480,7 +521,7 @@ impl EmitterWriter {
480
521
} ;
481
522
let pos = pos + 1 ;
482
523
483
- if pos > 1 && annotation. has_label ( ) {
524
+ if pos > 1 && ( annotation. has_label ( ) || annotation . takes_space ( ) ) {
484
525
for p in line_offset + 1 ..line_offset + pos + 1 {
485
526
buffer. putc ( p,
486
527
code_offset + annotation. start_col ,
@@ -514,12 +555,12 @@ impl EmitterWriter {
514
555
// After this we will have:
515
556
//
516
557
// 2 | fn foo() {
517
- // | __________ starting here...
558
+ // | __________
518
559
// | |
519
560
// | something about `foo`
520
561
// 3 |
521
562
// 4 | }
522
- // | _ ...ending here: test
563
+ // | _ test
523
564
for & ( pos, annotation) in & annotations_position {
524
565
let style = if annotation. is_primary {
525
566
Style :: LabelPrimary
@@ -557,12 +598,12 @@ impl EmitterWriter {
557
598
// After this we will have:
558
599
//
559
600
// 2 | fn foo() {
560
- // | ____-_____^ starting here...
601
+ // | ____-_____^
561
602
// | |
562
603
// | something about `foo`
563
604
// 3 |
564
605
// 4 | }
565
- // | _^ ...ending here: test
606
+ // | _^ test
566
607
for & ( _, annotation) in & annotations_position {
567
608
let ( underline, style) = if annotation. is_primary {
568
609
( '^' , Style :: UnderlinePrimary )
0 commit comments