| 607 | } |
| 608 | |
| 609 | fn combine_prql_and_frames(source: &str, frames: Vec<(Option<pr::Span>, pl::Lineage)>) -> String { |
| 610 | let source = Source::from(source); |
| 611 | let lines = source.lines().collect_vec(); |
| 612 | let width = lines.iter().map(|l| l.len()).max().unwrap_or(0); |
| 613 | |
| 614 | // Not sure this is the nicest construction. Some of this was added in the |
| 615 | // upgrade from ariande 0.3.0 to 0.4.0 and possibly there are more elegant |
| 616 | // ways to build the output. (Though `.get_line_text` seems to be the only |
| 617 | // method to get actual text out of a `Source`...) |
| 618 | let mut printed_lines_count = 0; |
| 619 | let mut result = Vec::new(); |
| 620 | for (span, frame) in frames { |
| 621 | if let Some(span) = span { |
| 622 | let line_len = source.get_line_range(&Range::from(span)).end - 1; |
| 623 | |
| 624 | while printed_lines_count < line_len { |
| 625 | result.push( |
| 626 | source |
| 627 | .get_line_text(source.line(printed_lines_count).unwrap()) |
| 628 | .unwrap() |
| 629 | // Ariadne 0.4.1 added a line break at the end of the line, so we |
| 630 | // trim it. |
| 631 | .trim_end() |
| 632 | .to_string(), |
| 633 | ); |
| 634 | printed_lines_count += 1; |
| 635 | } |
| 636 | |
| 637 | if printed_lines_count >= lines.len() { |
| 638 | break; |
| 639 | } |
| 640 | let chars: String = source |
| 641 | .get_line_text(source.line(printed_lines_count).unwrap()) |
| 642 | .unwrap() |
| 643 | // Ariadne 0.4.1 added a line break at the end of the line, so we |
| 644 | // trim it. |
| 645 | .trim_end() |
| 646 | .to_string(); |
| 647 | printed_lines_count += 1; |
| 648 | |
| 649 | result.push(format!("{chars:width$} # {frame}")); |
| 650 | } |
| 651 | } |
| 652 | for line in lines.iter().skip(printed_lines_count) { |
| 653 | result.push(source.get_line_text(line.to_owned()).unwrap().to_string()); |
| 654 | } |
| 655 | |
| 656 | result.into_iter().join("\n") + "\n" |
| 657 | } |
| 658 | |
| 659 | /// Unit tests for `prqlc`. Integration tests (where we call the actual binary) |
| 660 | /// are in `prqlc/tests/test.rs`. |