|
32 | 32 | //!
|
33 | 33 | //! The above snippet has been built out of the following structure:
|
34 | 34 | use crate::snippet;
|
35 | | -use std::cmp::{max, min}; |
36 | 35 | use std::fmt::{Display, Write};
|
37 | 36 | use std::{cmp, fmt};
|
38 | 37 |
|
39 | 38 | use anstyle::Style;
|
40 | 39 |
|
| 40 | +use crate::renderer::margin::Margin; |
41 | 41 | use crate::renderer::StyleSheet;
|
42 | 42 |
|
43 | 43 | /// List of lines to be displayed.
|
@@ -533,120 +533,6 @@ impl<'a> DisplayList<'a> {
|
533 | 533 | }
|
534 | 534 | }
|
535 | 535 |
|
536 | | -#[derive(Clone, Copy, Debug)] |
537 | | -pub struct Margin { |
538 | | - /// The available whitespace in the left that can be consumed when centering. |
539 | | - whitespace_left: usize, |
540 | | - /// The column of the beginning of left-most span. |
541 | | - span_left: usize, |
542 | | - /// The column of the end of right-most span. |
543 | | - span_right: usize, |
544 | | - /// The beginning of the line to be displayed. |
545 | | - computed_left: usize, |
546 | | - /// The end of the line to be displayed. |
547 | | - computed_right: usize, |
548 | | - /// The current width of the terminal. 140 by default and in tests. |
549 | | - column_width: usize, |
550 | | - /// The end column of a span label, including the span. Doesn't account for labels not in the |
551 | | - /// same line as the span. |
552 | | - label_right: usize, |
553 | | -} |
554 | | - |
555 | | -impl Margin { |
556 | | - pub fn new( |
557 | | - whitespace_left: usize, |
558 | | - span_left: usize, |
559 | | - span_right: usize, |
560 | | - label_right: usize, |
561 | | - column_width: usize, |
562 | | - max_line_len: usize, |
563 | | - ) -> Self { |
564 | | - // The 6 is padding to give a bit of room for `...` when displaying: |
565 | | - // ``` |
566 | | - // error: message |
567 | | - // --> file.rs:16:58 |
568 | | - // | |
569 | | - // 16 | ... fn foo(self) -> Self::Bar { |
570 | | - // | ^^^^^^^^^ |
571 | | - // ``` |
572 | | - |
573 | | - let mut m = Margin { |
574 | | - whitespace_left: whitespace_left.saturating_sub(6), |
575 | | - span_left: span_left.saturating_sub(6), |
576 | | - span_right: span_right + 6, |
577 | | - computed_left: 0, |
578 | | - computed_right: 0, |
579 | | - column_width, |
580 | | - label_right: label_right + 6, |
581 | | - }; |
582 | | - m.compute(max_line_len); |
583 | | - m |
584 | | - } |
585 | | - |
586 | | - pub(crate) fn was_cut_left(&self) -> bool { |
587 | | - self.computed_left > 0 |
588 | | - } |
589 | | - |
590 | | - pub(crate) fn was_cut_right(&self, line_len: usize) -> bool { |
591 | | - let right = |
592 | | - if self.computed_right == self.span_right || self.computed_right == self.label_right { |
593 | | - // Account for the "..." padding given above. Otherwise we end up with code lines that |
594 | | - // do fit but end in "..." as if they were trimmed. |
595 | | - self.computed_right - 6 |
596 | | - } else { |
597 | | - self.computed_right |
598 | | - }; |
599 | | - right < line_len && self.computed_left + self.column_width < line_len |
600 | | - } |
601 | | - |
602 | | - fn compute(&mut self, max_line_len: usize) { |
603 | | - // When there's a lot of whitespace (>20), we want to trim it as it is useless. |
604 | | - self.computed_left = if self.whitespace_left > 20 { |
605 | | - self.whitespace_left - 16 // We want some padding. |
606 | | - } else { |
607 | | - 0 |
608 | | - }; |
609 | | - // We want to show as much as possible, max_line_len is the right-most boundary for the |
610 | | - // relevant code. |
611 | | - self.computed_right = max(max_line_len, self.computed_left); |
612 | | - |
613 | | - if self.computed_right - self.computed_left > self.column_width { |
614 | | - // Trimming only whitespace isn't enough, let's get craftier. |
615 | | - if self.label_right - self.whitespace_left <= self.column_width { |
616 | | - // Attempt to fit the code window only trimming whitespace. |
617 | | - self.computed_left = self.whitespace_left; |
618 | | - self.computed_right = self.computed_left + self.column_width; |
619 | | - } else if self.label_right - self.span_left <= self.column_width { |
620 | | - // Attempt to fit the code window considering only the spans and labels. |
621 | | - let padding_left = (self.column_width - (self.label_right - self.span_left)) / 2; |
622 | | - self.computed_left = self.span_left.saturating_sub(padding_left); |
623 | | - self.computed_right = self.computed_left + self.column_width; |
624 | | - } else if self.span_right - self.span_left <= self.column_width { |
625 | | - // Attempt to fit the code window considering the spans and labels plus padding. |
626 | | - let padding_left = (self.column_width - (self.span_right - self.span_left)) / 5 * 2; |
627 | | - self.computed_left = self.span_left.saturating_sub(padding_left); |
628 | | - self.computed_right = self.computed_left + self.column_width; |
629 | | - } else { |
630 | | - // Mostly give up but still don't show the full line. |
631 | | - self.computed_left = self.span_left; |
632 | | - self.computed_right = self.span_right; |
633 | | - } |
634 | | - } |
635 | | - } |
636 | | - |
637 | | - pub(crate) fn left(&self, line_len: usize) -> usize { |
638 | | - min(self.computed_left, line_len) |
639 | | - } |
640 | | - |
641 | | - pub(crate) fn right(&self, line_len: usize) -> usize { |
642 | | - if line_len.saturating_sub(self.computed_left) <= self.column_width { |
643 | | - line_len |
644 | | - } else { |
645 | | - min(line_len, self.computed_right) |
646 | | - } |
647 | | - } |
648 | | -} |
649 | | - |
650 | 536 | /// Inline annotation which can be used in either Raw or Source line.
|
651 | 537 | #[derive(Debug, PartialEq)]
|
652 | 538 | pub struct Annotation<'a> {
|
|
0 commit comments