1use std::io::{self, Write};
2use std::ops::Range;
3use termcolor::{ColorSpec, WriteColor};
4
5use crate::diagnostic::{LabelStyle, Severity};
6use crate::files::{Error, Location};
7use crate::term::{Chars, Config, Styles};
8
9pub struct Locus {
11 pub name: String,
13 pub location: Location,
15}
16
17pub type SingleLabel<'diagnostic> = (LabelStyle, Range<usize>, &'diagnostic str);
23
24pub enum MultiLabel<'diagnostic> {
28 Top(usize),
42 Left,
48 Bottom(usize, &'diagnostic str),
55}
56
57#[derive(Copy, Clone)]
58enum VerticalBound {
59 Top,
60 Bottom,
61}
62
63type Underline = (LabelStyle, VerticalBound);
64
65pub struct Renderer<'writer, 'config> {
112 writer: &'writer mut dyn WriteColor,
113 config: &'config Config,
114}
115
116impl<'writer, 'config> Renderer<'writer, 'config> {
117 pub fn new(
119 writer: &'writer mut dyn WriteColor,
120 config: &'config Config,
121 ) -> Renderer<'writer, 'config> {
122 Renderer { writer, config }
123 }
124
125 fn chars(&self) -> &'config Chars {
126 &self.config.chars
127 }
128
129 fn styles(&self) -> &'config Styles {
130 &self.config.styles
131 }
132
133 pub fn render_header(
139 &mut self,
140 locus: Option<&Locus>,
141 severity: Severity,
142 code: Option<&str>,
143 message: &str,
144 ) -> Result<(), Error> {
145 if let Some(locus) = locus {
151 self.snippet_locus(locus)?;
152 write!(self, ": ")?;
153 }
154
155 self.set_color(self.styles().header(severity))?;
161 match severity {
162 Severity::Bug => write!(self, "bug")?,
163 Severity::Error => write!(self, "error")?,
164 Severity::Warning => write!(self, "warning")?,
165 Severity::Help => write!(self, "help")?,
166 Severity::Note => write!(self, "note")?,
167 }
168
169 if let Some(code) = &code.filter(|code| !code.is_empty()) {
175 write!(self, "[{}]", code)?;
176 }
177
178 self.set_color(&self.styles().header_message)?;
184 write!(self, ": {}", message)?;
185 self.reset()?;
186
187 writeln!(self)?;
188
189 Ok(())
190 }
191
192 pub fn render_empty(&mut self) -> Result<(), Error> {
194 writeln!(self)?;
195 Ok(())
196 }
197
198 pub fn render_snippet_start(
204 &mut self,
205 outer_padding: usize,
206 locus: &Locus,
207 ) -> Result<(), Error> {
208 self.outer_gutter(outer_padding)?;
209
210 self.set_color(&self.styles().source_border)?;
211 write!(self, "{}", self.chars().snippet_start)?;
212 self.reset()?;
213
214 write!(self, " ")?;
215 self.snippet_locus(&locus)?;
216
217 writeln!(self)?;
218
219 Ok(())
220 }
221
222 pub fn render_snippet_source(
229 &mut self,
230 outer_padding: usize,
231 line_number: usize,
232 source: &str,
233 severity: Severity,
234 single_labels: &[SingleLabel<'_>],
235 num_multi_labels: usize,
236 multi_labels: &[(usize, LabelStyle, MultiLabel<'_>)],
237 ) -> Result<(), Error> {
238 let source = source.trim_end_matches(['\n', '\r', '\0'].as_ref());
241
242 {
248 self.outer_gutter_number(line_number, outer_padding)?;
250 self.border_left()?;
251
252 let mut multi_labels_iter = multi_labels.iter().peekable();
254 for label_column in 0..num_multi_labels {
255 match multi_labels_iter.peek() {
256 Some((label_index, label_style, label)) if *label_index == label_column => {
257 match label {
258 MultiLabel::Top(start)
259 if *start <= source.len() - source.trim_start().len() =>
260 {
261 self.label_multi_top_left(severity, *label_style)?;
262 }
263 MultiLabel::Top(..) => self.inner_gutter_space()?,
264 MultiLabel::Left | MultiLabel::Bottom(..) => {
265 self.label_multi_left(severity, *label_style, None)?;
266 }
267 }
268 multi_labels_iter.next();
269 }
270 Some((_, _, _)) | None => self.inner_gutter_space()?,
271 }
272 }
273
274 write!(self, " ")?;
276 let mut in_primary = false;
277 for (metrics, ch) in self.char_metrics(source.char_indices()) {
278 let column_range = metrics.byte_index..(metrics.byte_index + ch.len_utf8());
279
280 let is_primary = single_labels.iter().any(|(ls, range, _)| {
282 *ls == LabelStyle::Primary && is_overlapping(range, &column_range)
283 }) || multi_labels.iter().any(|(_, ls, label)| {
284 *ls == LabelStyle::Primary
285 && match label {
286 MultiLabel::Top(start) => column_range.start >= *start,
287 MultiLabel::Left => true,
288 MultiLabel::Bottom(start, _) => column_range.end <= *start,
289 }
290 });
291
292 if is_primary && !in_primary {
294 self.set_color(self.styles().label(severity, LabelStyle::Primary))?;
295 in_primary = true;
296 } else if !is_primary && in_primary {
297 self.reset()?;
298 in_primary = false;
299 }
300
301 match ch {
302 '\t' => (0..metrics.unicode_width).try_for_each(|_| write!(self, " "))?,
303 _ => write!(self, "{}", ch)?,
304 }
305 }
306 if in_primary {
307 self.reset()?;
308 }
309 writeln!(self)?;
310 }
311
312 if !single_labels.is_empty() {
322 let mut num_messages = 0;
343 let mut max_label_start = 0;
351 let mut max_label_end = 0;
359 let mut trailing_label = None;
365
366 for (label_index, label) in single_labels.iter().enumerate() {
367 let (_, range, message) = label;
368 if !message.is_empty() {
369 num_messages += 1;
370 }
371 max_label_start = std::cmp::max(max_label_start, range.start);
372 max_label_end = std::cmp::max(max_label_end, range.end);
373 if range.end == max_label_end {
375 if message.is_empty() {
376 trailing_label = None;
377 } else {
378 trailing_label = Some((label_index, label));
379 }
380 }
381 }
382 if let Some((trailing_label_index, (_, trailing_range, _))) = trailing_label {
383 if single_labels
386 .iter()
387 .enumerate()
388 .filter(|(label_index, _)| *label_index != trailing_label_index)
389 .any(|(_, (_, range, _))| is_overlapping(trailing_range, range))
390 {
391 trailing_label = None;
394 }
395 }
396
397 self.outer_gutter(outer_padding)?;
403 self.border_left()?;
404 self.inner_gutter(severity, num_multi_labels, multi_labels)?;
405 write!(self, " ")?;
406
407 let mut previous_label_style = None;
408 let placeholder_metrics = Metrics {
409 byte_index: source.len(),
410 unicode_width: 1,
411 };
412 for (metrics, ch) in self
413 .char_metrics(source.char_indices())
414 .chain(std::iter::once((placeholder_metrics, '\0')))
422 {
423 let column_range = metrics.byte_index..(metrics.byte_index + ch.len_utf8());
425 let current_label_style = single_labels
426 .iter()
427 .filter(|(_, range, _)| is_overlapping(range, &column_range))
428 .map(|(label_style, _, _)| *label_style)
429 .max_by_key(label_priority_key);
430
431 if previous_label_style != current_label_style {
433 match current_label_style {
434 None => self.reset()?,
435 Some(label_style) => {
436 self.set_color(self.styles().label(severity, label_style))?;
437 }
438 }
439 }
440
441 let caret_ch = match current_label_style {
442 Some(LabelStyle::Primary) => Some(self.chars().single_primary_caret),
443 Some(LabelStyle::Secondary) => Some(self.chars().single_secondary_caret),
444 None if metrics.byte_index < max_label_end => Some(' '),
446 None => None,
447 };
448 if let Some(caret_ch) = caret_ch {
449 (0..metrics.unicode_width).try_for_each(|_| write!(self, "{}", caret_ch))?;
451 }
452
453 previous_label_style = current_label_style;
454 }
455 if previous_label_style.is_some() {
457 self.reset()?;
458 }
459 if let Some((_, (label_style, _, message))) = trailing_label {
461 write!(self, " ")?;
462 self.set_color(self.styles().label(severity, *label_style))?;
463 write!(self, "{}", message)?;
464 self.reset()?;
465 }
466 writeln!(self)?;
467
468 if num_messages > trailing_label.iter().count() {
477 self.outer_gutter(outer_padding)?;
483 self.border_left()?;
484 self.inner_gutter(severity, num_multi_labels, multi_labels)?;
485 write!(self, " ")?;
486 self.caret_pointers(
487 severity,
488 max_label_start,
489 single_labels,
490 trailing_label,
491 source.char_indices(),
492 )?;
493 writeln!(self)?;
494
495 for (label_style, range, message) in
503 hanging_labels(single_labels, trailing_label).rev()
504 {
505 self.outer_gutter(outer_padding)?;
506 self.border_left()?;
507 self.inner_gutter(severity, num_multi_labels, multi_labels)?;
508 write!(self, " ")?;
509 self.caret_pointers(
510 severity,
511 max_label_start,
512 single_labels,
513 trailing_label,
514 source
515 .char_indices()
516 .take_while(|(byte_index, _)| *byte_index < range.start),
517 )?;
518 self.set_color(self.styles().label(severity, *label_style))?;
519 write!(self, "{}", message)?;
520 self.reset()?;
521 writeln!(self)?;
522 }
523 }
524 }
525
526 for (multi_label_index, (_, label_style, label)) in multi_labels.iter().enumerate() {
533 let (label_style, range, bottom_message) = match label {
534 MultiLabel::Left => continue, MultiLabel::Top(start) if *start <= source.len() - source.trim_start().len() => {
537 continue
538 }
539 MultiLabel::Top(range) => (*label_style, range, None),
540 MultiLabel::Bottom(range, message) => (*label_style, range, Some(message)),
541 };
542
543 self.outer_gutter(outer_padding)?;
544 self.border_left()?;
545
546 let mut underline = None;
552 let mut multi_labels_iter = multi_labels.iter().enumerate().peekable();
553 for label_column in 0..num_multi_labels {
554 match multi_labels_iter.peek() {
555 Some((i, (label_index, ls, label))) if *label_index == label_column => {
556 match label {
557 MultiLabel::Left => {
558 self.label_multi_left(severity, *ls, underline.map(|(s, _)| s))?;
559 }
560 MultiLabel::Top(..) if multi_label_index > *i => {
561 self.label_multi_left(severity, *ls, underline.map(|(s, _)| s))?;
562 }
563 MultiLabel::Bottom(..) if multi_label_index < *i => {
564 self.label_multi_left(severity, *ls, underline.map(|(s, _)| s))?;
565 }
566 MultiLabel::Top(..) if multi_label_index == *i => {
567 underline = Some((*ls, VerticalBound::Top));
568 self.label_multi_top_left(severity, label_style)?
569 }
570 MultiLabel::Bottom(..) if multi_label_index == *i => {
571 underline = Some((*ls, VerticalBound::Bottom));
572 self.label_multi_bottom_left(severity, label_style)?;
573 }
574 MultiLabel::Top(..) | MultiLabel::Bottom(..) => {
575 self.inner_gutter_column(severity, underline)?;
576 }
577 }
578 multi_labels_iter.next();
579 }
580 Some((_, _)) | None => self.inner_gutter_column(severity, underline)?,
581 }
582 }
583
584 match bottom_message {
586 None => self.label_multi_top_caret(severity, label_style, source, *range)?,
587 Some(message) => {
588 self.label_multi_bottom_caret(severity, label_style, source, *range, message)?
589 }
590 }
591 }
592
593 Ok(())
594 }
595
596 pub fn render_snippet_empty(
602 &mut self,
603 outer_padding: usize,
604 severity: Severity,
605 num_multi_labels: usize,
606 multi_labels: &[(usize, LabelStyle, MultiLabel<'_>)],
607 ) -> Result<(), Error> {
608 self.outer_gutter(outer_padding)?;
609 self.border_left()?;
610 self.inner_gutter(severity, num_multi_labels, multi_labels)?;
611 writeln!(self)?;
612 Ok(())
613 }
614
615 pub fn render_snippet_break(
621 &mut self,
622 outer_padding: usize,
623 severity: Severity,
624 num_multi_labels: usize,
625 multi_labels: &[(usize, LabelStyle, MultiLabel<'_>)],
626 ) -> Result<(), Error> {
627 self.outer_gutter(outer_padding)?;
628 self.border_left_break()?;
629 self.inner_gutter(severity, num_multi_labels, multi_labels)?;
630 writeln!(self)?;
631 Ok(())
632 }
633
634 pub fn render_snippet_note(
641 &mut self,
642 outer_padding: usize,
643 message: &str,
644 ) -> Result<(), Error> {
645 for (note_line_index, line) in message.lines().enumerate() {
646 self.outer_gutter(outer_padding)?;
647 match note_line_index {
648 0 => {
649 self.set_color(&self.styles().note_bullet)?;
650 write!(self, "{}", self.chars().note_bullet)?;
651 self.reset()?;
652 }
653 _ => write!(self, " ")?,
654 }
655 writeln!(self, " {}", line)?;
657 }
658
659 Ok(())
660 }
661
662 fn char_metrics(
666 &self,
667 char_indices: impl Iterator<Item = (usize, char)>,
668 ) -> impl Iterator<Item = (Metrics, char)> {
669 use unicode_width::UnicodeWidthChar;
670
671 let tab_width = self.config.tab_width;
672 let mut unicode_column = 0;
673
674 char_indices.map(move |(byte_index, ch)| {
675 let metrics = Metrics {
676 byte_index,
677 unicode_width: match (ch, tab_width) {
678 ('\t', 0) => 0, ('\t', _) => tab_width - (unicode_column % tab_width),
680 (ch, _) => ch.width().unwrap_or(0),
681 },
682 };
683 unicode_column += metrics.unicode_width;
684
685 (metrics, ch)
686 })
687 }
688
689 fn snippet_locus(&mut self, locus: &Locus) -> Result<(), Error> {
691 write!(
692 self,
693 "{name}:{line_number}:{column_number}",
694 name = locus.name,
695 line_number = locus.location.line_number,
696 column_number = locus.location.column_number,
697 )?;
698 Ok(())
699 }
700
701 fn outer_gutter(&mut self, outer_padding: usize) -> Result<(), Error> {
703 write!(self, "{space: >width$} ", space = "", width = outer_padding)?;
704 Ok(())
705 }
706
707 fn outer_gutter_number(
709 &mut self,
710 line_number: usize,
711 outer_padding: usize,
712 ) -> Result<(), Error> {
713 self.set_color(&self.styles().line_number)?;
714 write!(
715 self,
716 "{line_number: >width$}",
717 line_number = line_number,
718 width = outer_padding,
719 )?;
720 self.reset()?;
721 write!(self, " ")?;
722 Ok(())
723 }
724
725 fn border_left(&mut self) -> Result<(), Error> {
727 self.set_color(&self.styles().source_border)?;
728 write!(self, "{}", self.chars().source_border_left)?;
729 self.reset()?;
730 Ok(())
731 }
732
733 fn border_left_break(&mut self) -> Result<(), Error> {
735 self.set_color(&self.styles().source_border)?;
736 write!(self, "{}", self.chars().source_border_left_break)?;
737 self.reset()?;
738 Ok(())
739 }
740
741 fn caret_pointers(
743 &mut self,
744 severity: Severity,
745 max_label_start: usize,
746 single_labels: &[SingleLabel<'_>],
747 trailing_label: Option<(usize, &SingleLabel<'_>)>,
748 char_indices: impl Iterator<Item = (usize, char)>,
749 ) -> Result<(), Error> {
750 for (metrics, ch) in self.char_metrics(char_indices) {
751 let column_range = metrics.byte_index..(metrics.byte_index + ch.len_utf8());
752 let label_style = hanging_labels(single_labels, trailing_label)
753 .filter(|(_, range, _)| column_range.contains(&range.start))
754 .map(|(label_style, _, _)| *label_style)
755 .max_by_key(label_priority_key);
756
757 let mut spaces = match label_style {
758 None => 0..metrics.unicode_width,
759 Some(label_style) => {
760 self.set_color(self.styles().label(severity, label_style))?;
761 write!(self, "{}", self.chars().pointer_left)?;
762 self.reset()?;
763 1..metrics.unicode_width
764 }
765 };
766 if metrics.byte_index <= max_label_start {
768 spaces.try_for_each(|_| write!(self, " "))?;
769 }
770 }
771
772 Ok(())
773 }
774
775 fn label_multi_left(
781 &mut self,
782 severity: Severity,
783 label_style: LabelStyle,
784 underline: Option<LabelStyle>,
785 ) -> Result<(), Error> {
786 match underline {
787 None => write!(self, " ")?,
788 Some(label_style) => {
790 self.set_color(self.styles().label(severity, label_style))?;
791 write!(self, "{}", self.chars().multi_top)?;
792 self.reset()?;
793 }
794 }
795 self.set_color(self.styles().label(severity, label_style))?;
796 write!(self, "{}", self.chars().multi_left)?;
797 self.reset()?;
798 Ok(())
799 }
800
801 fn label_multi_top_left(
807 &mut self,
808 severity: Severity,
809 label_style: LabelStyle,
810 ) -> Result<(), Error> {
811 write!(self, " ")?;
812 self.set_color(self.styles().label(severity, label_style))?;
813 write!(self, "{}", self.chars().multi_top_left)?;
814 self.reset()?;
815 Ok(())
816 }
817
818 fn label_multi_bottom_left(
824 &mut self,
825 severity: Severity,
826 label_style: LabelStyle,
827 ) -> Result<(), Error> {
828 write!(self, " ")?;
829 self.set_color(self.styles().label(severity, label_style))?;
830 write!(self, "{}", self.chars().multi_bottom_left)?;
831 self.reset()?;
832 Ok(())
833 }
834
835 fn label_multi_top_caret(
841 &mut self,
842 severity: Severity,
843 label_style: LabelStyle,
844 source: &str,
845 start: usize,
846 ) -> Result<(), Error> {
847 self.set_color(self.styles().label(severity, label_style))?;
848
849 for (metrics, _) in self
850 .char_metrics(source.char_indices())
851 .take_while(|(metrics, _)| metrics.byte_index < start + 1)
852 {
853 (0..metrics.unicode_width)
855 .try_for_each(|_| write!(self, "{}", self.chars().multi_top))?;
856 }
857
858 let caret_start = match label_style {
859 LabelStyle::Primary => self.config.chars.multi_primary_caret_start,
860 LabelStyle::Secondary => self.config.chars.multi_secondary_caret_start,
861 };
862 write!(self, "{}", caret_start)?;
863 self.reset()?;
864 writeln!(self)?;
865 Ok(())
866 }
867
868 fn label_multi_bottom_caret(
874 &mut self,
875 severity: Severity,
876 label_style: LabelStyle,
877 source: &str,
878 start: usize,
879 message: &str,
880 ) -> Result<(), Error> {
881 self.set_color(self.styles().label(severity, label_style))?;
882
883 for (metrics, _) in self
884 .char_metrics(source.char_indices())
885 .take_while(|(metrics, _)| metrics.byte_index < start)
886 {
887 (0..metrics.unicode_width)
889 .try_for_each(|_| write!(self, "{}", self.chars().multi_bottom))?;
890 }
891
892 let caret_end = match label_style {
893 LabelStyle::Primary => self.config.chars.multi_primary_caret_start,
894 LabelStyle::Secondary => self.config.chars.multi_secondary_caret_start,
895 };
896 write!(self, "{}", caret_end)?;
897 if !message.is_empty() {
898 write!(self, " {}", message)?;
899 }
900 self.reset()?;
901 writeln!(self)?;
902 Ok(())
903 }
904
905 fn inner_gutter_column(
907 &mut self,
908 severity: Severity,
909 underline: Option<Underline>,
910 ) -> Result<(), Error> {
911 match underline {
912 None => self.inner_gutter_space(),
913 Some((label_style, vertical_bound)) => {
914 self.set_color(self.styles().label(severity, label_style))?;
915 let ch = match vertical_bound {
916 VerticalBound::Top => self.config.chars.multi_top,
917 VerticalBound::Bottom => self.config.chars.multi_bottom,
918 };
919 write!(self, "{0}{0}", ch)?;
920 self.reset()?;
921 Ok(())
922 }
923 }
924 }
925
926 fn inner_gutter_space(&mut self) -> Result<(), Error> {
928 write!(self, " ")?;
929 Ok(())
930 }
931
932 fn inner_gutter(
934 &mut self,
935 severity: Severity,
936 num_multi_labels: usize,
937 multi_labels: &[(usize, LabelStyle, MultiLabel<'_>)],
938 ) -> Result<(), Error> {
939 let mut multi_labels_iter = multi_labels.iter().peekable();
940 for label_column in 0..num_multi_labels {
941 match multi_labels_iter.peek() {
942 Some((label_index, ls, label)) if *label_index == label_column => match label {
943 MultiLabel::Left | MultiLabel::Bottom(..) => {
944 self.label_multi_left(severity, *ls, None)?;
945 multi_labels_iter.next();
946 }
947 MultiLabel::Top(..) => {
948 self.inner_gutter_space()?;
949 multi_labels_iter.next();
950 }
951 },
952 Some((_, _, _)) | None => self.inner_gutter_space()?,
953 }
954 }
955
956 Ok(())
957 }
958}
959
960impl<'writer, 'config> Write for Renderer<'writer, 'config> {
961 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
962 self.writer.write(buf)
963 }
964
965 fn flush(&mut self) -> io::Result<()> {
966 self.writer.flush()
967 }
968}
969
970impl<'writer, 'config> WriteColor for Renderer<'writer, 'config> {
971 fn supports_color(&self) -> bool {
972 self.writer.supports_color()
973 }
974
975 fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
976 self.writer.set_color(spec)
977 }
978
979 fn reset(&mut self) -> io::Result<()> {
980 self.writer.reset()
981 }
982
983 fn is_synchronous(&self) -> bool {
984 self.writer.is_synchronous()
985 }
986}
987
988struct Metrics {
989 byte_index: usize,
990 unicode_width: usize,
991}
992
993fn is_overlapping(range0: &Range<usize>, range1: &Range<usize>) -> bool {
995 let start = std::cmp::max(range0.start, range1.start);
996 let end = std::cmp::min(range0.end, range1.end);
997 start < end
998}
999
1000fn label_priority_key(label_style: &LabelStyle) -> u8 {
1002 match label_style {
1003 LabelStyle::Secondary => 0,
1004 LabelStyle::Primary => 1,
1005 }
1006}
1007
1008fn hanging_labels<'labels, 'diagnostic>(
1011 single_labels: &'labels [SingleLabel<'diagnostic>],
1012 trailing_label: Option<(usize, &'labels SingleLabel<'diagnostic>)>,
1013) -> impl 'labels + DoubleEndedIterator<Item = &'labels SingleLabel<'diagnostic>> {
1014 single_labels
1015 .iter()
1016 .enumerate()
1017 .filter(|(_, (_, _, message))| !message.is_empty())
1018 .filter(move |(i, _)| trailing_label.map_or(true, |(j, _)| *i != j))
1019 .map(|(_, label)| label)
1020}