|
1 | 1 | use std::io; |
2 | 2 | use std::ops::Range; |
3 | 3 |
|
4 | | -use crate::{Config, IndexType, LabelDisplay}; |
| 4 | +use crate::{Config, IndexType, LabelCollapseLines, LabelDisplay}; |
5 | 5 |
|
6 | 6 | use super::draw::{self, StreamAwareFmt, StreamType, WrappedWriter}; |
7 | 7 | use super::{Cache, CharSet, LabelAttach, Report, ReportStyle, Show, Span, Write}; |
@@ -626,7 +626,20 @@ impl<S: Span, K: ReportStyle> Report<S, K> { |
626 | 626 | .iter() |
627 | 627 | .any(|label| label.char_span.contains(&line.span().start())); |
628 | 628 | if !is_ellipsis && within_label { |
629 | | - is_ellipsis = true; |
| 629 | + // Check to see if all the multiline labels containing this line use ellipses |
| 630 | + let should_collapse = multi_labels |
| 631 | + .iter() |
| 632 | + .filter(|label| label.char_span.contains(&line.span().start())) |
| 633 | + .all(|label| match label.display_info.collapse_when { |
| 634 | + LabelCollapseLines::Always => true, |
| 635 | + LabelCollapseLines::MaxLines(max_lines) => { |
| 636 | + label.end_line - label.start_line >= max_lines |
| 637 | + } |
| 638 | + LabelCollapseLines::Never => false, |
| 639 | + }); |
| 640 | + if should_collapse { |
| 641 | + is_ellipsis = true; |
| 642 | + } |
630 | 643 | } else { |
631 | 644 | if !self.config.compact && !is_ellipsis { |
632 | 645 | write_margin(&mut w, idx, false, is_ellipsis, false, None, &[], &None)?; |
@@ -1000,7 +1013,8 @@ mod tests { |
1000 | 1013 | use insta::assert_snapshot; |
1001 | 1014 |
|
1002 | 1015 | use crate::{ |
1003 | | - Cache, CharSet, Config, IndexType, Label, Report, ReportKind, ReportStyle, Source, Span, |
| 1016 | + Cache, CharSet, Config, IndexType, Label, LabelCollapseLines, Report, ReportKind, |
| 1017 | + ReportStyle, Source, Span, |
1004 | 1018 | }; |
1005 | 1019 |
|
1006 | 1020 | impl<S: Span, K: ReportStyle> Report<S, K> { |
@@ -1405,6 +1419,60 @@ mod tests { |
1405 | 1419 | "###); |
1406 | 1420 | } |
1407 | 1421 |
|
| 1422 | + #[test] |
| 1423 | + fn multiline_label_show_3() { |
| 1424 | + let source = "pear\napple\n==\norange\nbanana"; |
| 1425 | + let msg = remove_trailing( |
| 1426 | + Report::build(ReportKind::Error, 0..0) |
| 1427 | + .with_config(no_color_and_ascii()) |
| 1428 | + .with_label( |
| 1429 | + Label::new(5..20) |
| 1430 | + .with_message("illegal comparison") |
| 1431 | + .with_collapse_lines_when(LabelCollapseLines::MaxLines(3)), |
| 1432 | + ) |
| 1433 | + .finish() |
| 1434 | + .write_to_string(Source::from(source)), |
| 1435 | + ); |
| 1436 | + assert_snapshot!(msg, @r" |
| 1437 | + Error: |
| 1438 | + ,-[ <unknown>:1:1 ] |
| 1439 | + | |
| 1440 | + 2 | ,-> apple |
| 1441 | + 3 | | == |
| 1442 | + 4 | |-> orange |
| 1443 | + | | |
| 1444 | + | `------------ illegal comparison |
| 1445 | + ---' |
| 1446 | + "); |
| 1447 | + } |
| 1448 | + |
| 1449 | + #[test] |
| 1450 | + fn multiline_label_longer_than_max_span_line_count() { |
| 1451 | + let source = "pear\napple\n==\norange\nbanana"; |
| 1452 | + let msg = remove_trailing( |
| 1453 | + Report::build(ReportKind::Error, 0..0) |
| 1454 | + .with_config(no_color_and_ascii()) |
| 1455 | + .with_label( |
| 1456 | + Label::new(5..source.len()) |
| 1457 | + .with_message("illegal comparison") |
| 1458 | + .with_collapse_lines_when(LabelCollapseLines::MaxLines(3)), |
| 1459 | + ) |
| 1460 | + .finish() |
| 1461 | + .write_to_string(Source::from(source)), |
| 1462 | + ); |
| 1463 | + assert_snapshot!(msg, @r" |
| 1464 | + Error: |
| 1465 | + ,-[ <unknown>:1:1 ] |
| 1466 | + | |
| 1467 | + 2 | ,-> apple |
| 1468 | + : : |
| 1469 | + 5 | |-> banana |
| 1470 | + | | |
| 1471 | + | `----------- illegal comparison |
| 1472 | + ---' |
| 1473 | + "); |
| 1474 | + } |
| 1475 | + |
1408 | 1476 | #[test] |
1409 | 1477 | fn multiline_context_label() { |
1410 | 1478 | let source = "apple\nbanana\ncarrot\ndragonfruit\negg\nfruit\ngrapes"; |
|
0 commit comments