Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 79f657e

Browse files
committed
fix!: Move Margin to renderer
1 parent dfd4e87 commit 79f657e

File tree

4 files changed

+124
-118
lines changed

4 files changed

+124
-118
lines changed

‎src/display_list/mod.rs‎

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
//!
3333
//! The above snippet has been built out of the following structure:
3434
use crate::snippet;
35-
use std::cmp::{max, min};
3635
use std::fmt::{Display, Write};
3736
use std::{cmp, fmt};
3837

39-
use crate::renderer::{stylesheet::Stylesheet, Style};
38+
use crate::renderer::{stylesheet::Stylesheet, Margin,Style};
4039

4140
/// List of lines to be displayed.
4241
pub struct DisplayList<'a> {
@@ -531,120 +530,6 @@ impl<'a> DisplayList<'a> {
531530
}
532531
}
533532

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

‎src/renderer/margin.rs‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use std::cmp::{max, min};
2+
3+
const ELLIPSIS_PASSING: usize = 6;
4+
const LONG_WHITESPACE: usize = 20;
5+
const LONG_WHITESPACE_PADDING: usize = 4;
6+
7+
#[derive(Clone, Copy, Debug)]
8+
pub struct Margin {
9+
/// The available whitespace in the left that can be consumed when centering.
10+
whitespace_left: usize,
11+
/// The column of the beginning of left-most span.
12+
span_left: usize,
13+
/// The column of the end of right-most span.
14+
span_right: usize,
15+
/// The beginning of the line to be displayed.
16+
computed_left: usize,
17+
/// The end of the line to be displayed.
18+
computed_right: usize,
19+
/// The current width of the terminal. 140 by default and in tests.
20+
column_width: usize,
21+
/// The end column of a span label, including the span. Doesn't account for labels not in the
22+
/// same line as the span.
23+
label_right: usize,
24+
}
25+
26+
impl Margin {
27+
pub fn new(
28+
whitespace_left: usize,
29+
span_left: usize,
30+
span_right: usize,
31+
label_right: usize,
32+
column_width: usize,
33+
max_line_len: usize,
34+
) -> Self {
35+
// The 6 is padding to give a bit of room for `...` when displaying:
36+
// ```
37+
// error: message
38+
// --> file.rs:16:58
39+
// |
40+
// 16 | ... fn foo(self) -> Self::Bar {
41+
// | ^^^^^^^^^
42+
// ```
43+
44+
let mut m = Margin {
45+
whitespace_left: whitespace_left.saturating_sub(ELLIPSIS_PASSING),
46+
span_left: span_left.saturating_sub(ELLIPSIS_PASSING),
47+
span_right: span_right + ELLIPSIS_PASSING,
48+
computed_left: 0,
49+
computed_right: 0,
50+
column_width,
51+
label_right: label_right + ELLIPSIS_PASSING,
52+
};
53+
m.compute(max_line_len);
54+
m
55+
}
56+
57+
pub(crate) fn was_cut_left(&self) -> bool {
58+
self.computed_left > 0
59+
}
60+
61+
pub(crate) fn was_cut_right(&self, line_len: usize) -> bool {
62+
let right =
63+
if self.computed_right == self.span_right || self.computed_right == self.label_right {
64+
// Account for the "..." padding given above. Otherwise we end up with code lines that
65+
// do fit but end in "..." as if they were trimmed.
66+
self.computed_right - ELLIPSIS_PASSING
67+
} else {
68+
self.computed_right
69+
};
70+
right < line_len && self.computed_left + self.column_width < line_len
71+
}
72+
73+
fn compute(&mut self, max_line_len: usize) {
74+
// When there's a lot of whitespace (>20), we want to trim it as it is useless.
75+
self.computed_left = if self.whitespace_left > LONG_WHITESPACE {
76+
self.whitespace_left - (LONG_WHITESPACE - LONG_WHITESPACE_PADDING) // We want some padding.
77+
} else {
78+
0
79+
};
80+
// We want to show as much as possible, max_line_len is the right-most boundary for the
81+
// relevant code.
82+
self.computed_right = max(max_line_len, self.computed_left);
83+
84+
if self.computed_right - self.computed_left > self.column_width {
85+
// Trimming only whitespace isn't enough, let's get craftier.
86+
if self.label_right - self.whitespace_left <= self.column_width {
87+
// Attempt to fit the code window only trimming whitespace.
88+
self.computed_left = self.whitespace_left;
89+
self.computed_right = self.computed_left + self.column_width;
90+
} else if self.label_right - self.span_left <= self.column_width {
91+
// Attempt to fit the code window considering only the spans and labels.
92+
let padding_left = (self.column_width - (self.label_right - self.span_left)) / 2;
93+
self.computed_left = self.span_left.saturating_sub(padding_left);
94+
self.computed_right = self.computed_left + self.column_width;
95+
} else if self.span_right - self.span_left <= self.column_width {
96+
// Attempt to fit the code window considering the spans and labels plus padding.
97+
let padding_left = (self.column_width - (self.span_right - self.span_left)) / 5 * 2;
98+
self.computed_left = self.span_left.saturating_sub(padding_left);
99+
self.computed_right = self.computed_left + self.column_width;
100+
} else {
101+
// Mostly give up but still don't show the full line.
102+
self.computed_left = self.span_left;
103+
self.computed_right = self.span_right;
104+
}
105+
}
106+
}
107+
108+
pub(crate) fn left(&self, line_len: usize) -> usize {
109+
min(self.computed_left, line_len)
110+
}
111+
112+
pub(crate) fn right(&self, line_len: usize) -> usize {
113+
if line_len.saturating_sub(self.computed_left) <= self.column_width {
114+
line_len
115+
} else {
116+
min(line_len, self.computed_right)
117+
}
118+
}
119+
}

‎src/renderer/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
mod margin;
12
pub mod stylesheet;
23

3-
use crate::display_list::{DisplayList,Margin};
4+
use crate::display_list::DisplayList;
45
use crate::snippet::Snippet;
56
pub use anstyle::*;
7+
pub use margin::Margin;
68
use std::fmt::Display;
79
use stylesheet::Stylesheet;
810

‎tests/deserialize/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::{Deserialize, Deserializer, Serialize};
22

33
use annotate_snippets::renderer::Renderer;
44
use annotate_snippets::{
5-
display_list::Margin,
5+
renderer::Margin,
66
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
77
};
88

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /