Skip to content

Commit b216321

Browse files
authored
Merge pull request #105 from Muscraft/fix-margin
Fix margin
2 parents e86a322 + e89fd82 commit b216321

File tree

8 files changed

+551
-467
lines changed

8 files changed

+551
-467
lines changed

Diff for: src/renderer/display_list.rs

+504-359
Large diffs are not rendered by default.

Diff for: src/renderer/margin.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const ELLIPSIS_PASSING: usize = 6;
44
const LONG_WHITESPACE: usize = 20;
55
const LONG_WHITESPACE_PADDING: usize = 4;
66

7-
#[derive(Clone, Copy, Debug)]
7+
#[derive(Clone, Copy, Debug, PartialEq)]
88
pub struct Margin {
99
/// The available whitespace in the left that can be consumed when centering.
1010
whitespace_left: usize,
@@ -17,7 +17,7 @@ pub struct Margin {
1717
/// The end of the line to be displayed.
1818
computed_right: usize,
1919
/// The current width of the terminal. 140 by default and in tests.
20-
column_width: usize,
20+
term_width: usize,
2121
/// The end column of a span label, including the span. Doesn't account for labels not in the
2222
/// same line as the span.
2323
label_right: usize,
@@ -29,7 +29,7 @@ impl Margin {
2929
span_left: usize,
3030
span_right: usize,
3131
label_right: usize,
32-
column_width: usize,
32+
term_width: usize,
3333
max_line_len: usize,
3434
) -> Self {
3535
// The 6 is padding to give a bit of room for `...` when displaying:
@@ -47,7 +47,7 @@ impl Margin {
4747
span_right: span_right + ELLIPSIS_PASSING,
4848
computed_left: 0,
4949
computed_right: 0,
50-
column_width,
50+
term_width,
5151
label_right: label_right + ELLIPSIS_PASSING,
5252
};
5353
m.compute(max_line_len);
@@ -67,7 +67,7 @@ impl Margin {
6767
} else {
6868
self.computed_right
6969
};
70-
right < line_len && self.computed_left + self.column_width < line_len
70+
right < line_len && self.computed_left + self.term_width < line_len
7171
}
7272

7373
fn compute(&mut self, max_line_len: usize) {
@@ -81,22 +81,22 @@ impl Margin {
8181
// relevant code.
8282
self.computed_right = max(max_line_len, self.computed_left);
8383

84-
if self.computed_right - self.computed_left > self.column_width {
84+
if self.computed_right - self.computed_left > self.term_width {
8585
// Trimming only whitespace isn't enough, let's get craftier.
86-
if self.label_right - self.whitespace_left <= self.column_width {
86+
if self.label_right - self.whitespace_left <= self.term_width {
8787
// Attempt to fit the code window only trimming whitespace.
8888
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 {
89+
self.computed_right = self.computed_left + self.term_width;
90+
} else if self.label_right - self.span_left <= self.term_width {
9191
// 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;
92+
let padding_left = (self.term_width - (self.label_right - self.span_left)) / 2;
9393
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 {
94+
self.computed_right = self.computed_left + self.term_width;
95+
} else if self.span_right - self.span_left <= self.term_width {
9696
// 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;
97+
let padding_left = (self.term_width - (self.span_right - self.span_left)) / 5 * 2;
9898
self.computed_left = self.span_left.saturating_sub(padding_left);
99-
self.computed_right = self.computed_left + self.column_width;
99+
self.computed_right = self.computed_left + self.term_width;
100100
} else {
101101
// Mostly give up but still don't show the full line.
102102
self.computed_left = self.span_left;
@@ -110,7 +110,7 @@ impl Margin {
110110
}
111111

112112
pub(crate) fn right(&self, line_len: usize) -> usize {
113-
if line_len.saturating_sub(self.computed_left) <= self.column_width {
113+
if line_len.saturating_sub(self.computed_left) <= self.term_width {
114114
line_len
115115
} else {
116116
min(line_len, self.computed_right)

Diff for: src/renderer/mod.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ pub(crate) mod stylesheet;
1717
use crate::snippet::Message;
1818
pub use anstyle::*;
1919
use display_list::DisplayList;
20-
pub use margin::Margin;
20+
use margin::Margin;
2121
use std::fmt::Display;
2222
use stylesheet::Stylesheet;
2323

24+
pub const DEFAULT_TERM_WIDTH: usize = 140;
25+
2426
/// A renderer for [`Message`]s
2527
#[derive(Clone)]
2628
pub struct Renderer {
2729
anonymized_line_numbers: bool,
28-
margin: Option<Margin>,
30+
term_width: usize,
2931
stylesheet: Stylesheet,
3032
}
3133

@@ -34,7 +36,7 @@ impl Renderer {
3436
pub const fn plain() -> Self {
3537
Self {
3638
anonymized_line_numbers: false,
37-
margin: None,
39+
term_width: DEFAULT_TERM_WIDTH,
3840
stylesheet: Stylesheet::plain(),
3941
}
4042
}
@@ -94,25 +96,9 @@ impl Renderer {
9496
self
9597
}
9698

97-
/// Set the margin for the output
98-
///
99-
/// This controls the various margins of the output.
100-
///
101-
/// # Example
102-
///
103-
/// ```text
104-
/// error: expected type, found `22`
105-
/// --> examples/footer.rs:29:25
106-
/// |
107-
/// 26 | ... annotations: vec![SourceAnnotation {
108-
/// | ---------------- info: while parsing this struct
109-
/// ...
110-
/// 29 | ... range: <22, 25>,
111-
/// | ^^
112-
/// |
113-
/// ```
114-
pub const fn margin(mut self, margin: Option<Margin>) -> Self {
115-
self.margin = margin;
99+
// Set the terminal width
100+
pub const fn term_width(mut self, term_width: usize) -> Self {
101+
self.term_width = term_width;
116102
self
117103
}
118104

@@ -170,7 +156,7 @@ impl Renderer {
170156
msg,
171157
&self.stylesheet,
172158
self.anonymized_line_numbers,
173-
self.margin,
159+
self.term_width,
174160
)
175161
}
176162
}

Diff for: tests/fixtures/deserialize.rs

+5-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde::{Deserialize, Deserializer, Serialize};
22
use std::ops::Range;
33

4-
use annotate_snippets::{renderer::Margin, Annotation, Level, Message, Renderer, Snippet};
4+
use annotate_snippets::renderer::DEFAULT_TERM_WIDTH;
5+
use annotate_snippets::{Annotation, Level, Message, Renderer, Snippet};
56

67
#[derive(Deserialize)]
78
pub struct Fixture<'a> {
@@ -148,55 +149,18 @@ enum LevelDef {
148149
pub struct RendererDef {
149150
#[serde(default)]
150151
anonymized_line_numbers: bool,
151-
#[serde(deserialize_with = "deserialize_margin")]
152152
#[serde(default)]
153-
margin: Option<Margin>,
153+
term_width: Option<usize>,
154154
}
155155

156156
impl From<RendererDef> for Renderer {
157157
fn from(val: RendererDef) -> Self {
158158
let RendererDef {
159159
anonymized_line_numbers,
160-
margin,
160+
term_width,
161161
} = val;
162162
Renderer::plain()
163163
.anonymized_line_numbers(anonymized_line_numbers)
164-
.margin(margin)
164+
.term_width(term_width.unwrap_or(DEFAULT_TERM_WIDTH))
165165
}
166166
}
167-
168-
fn deserialize_margin<'de, D>(deserializer: D) -> Result<Option<Margin>, D::Error>
169-
where
170-
D: Deserializer<'de>,
171-
{
172-
#[derive(Deserialize)]
173-
struct Wrapper {
174-
whitespace_left: usize,
175-
span_left: usize,
176-
span_right: usize,
177-
label_right: usize,
178-
column_width: usize,
179-
max_line_len: usize,
180-
}
181-
182-
Option::<Wrapper>::deserialize(deserializer).map(|opt_wrapped: Option<Wrapper>| {
183-
opt_wrapped.map(|wrapped: Wrapper| {
184-
let Wrapper {
185-
whitespace_left,
186-
span_left,
187-
span_right,
188-
label_right,
189-
column_width,
190-
max_line_len,
191-
} = wrapped;
192-
Margin::new(
193-
whitespace_left,
194-
span_left,
195-
span_right,
196-
label_right,
197-
column_width,
198-
max_line_len,
199-
)
200-
})
201-
})
202-
}

Diff for: tests/fixtures/no-color/strip_line.toml

-7
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,3 @@ range = [192, 194]
1616
[renderer]
1717
color = false
1818
anonymized_line_numbers = true
19-
[renderer.margin]
20-
whitespace_left = 180
21-
span_left = 192
22-
span_right = 194
23-
label_right = 221
24-
column_width = 140
25-
max_line_len = 195

Diff for: tests/fixtures/no-color/strip_line_char.toml

-7
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,3 @@ range = [192, 194]
1616
[renderer]
1717
color = false
1818
anonymized_line_numbers = true
19-
[renderer.margin]
20-
whitespace_left = 180
21-
span_left = 192
22-
span_right = 194
23-
label_right = 221
24-
column_width = 140
25-
max_line_len = 195

Diff for: tests/fixtures/no-color/strip_line_non_ws.svg

+7-5
Loading

Diff for: tests/fixtures/no-color/strip_line_non_ws.toml

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ id = "E0308"
44
title = "mismatched types"
55

66
[[message.snippets]]
7-
source = " let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();"
7+
source = """
8+
let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();
9+
"""
810
line_start = 4
911
origin = "$DIR/non-whitespace-trimming.rs"
1012

1113
[[message.snippets.annotations]]
12-
label = "expected (), found integer"
14+
label = "expected `()`, found integer"
1315
level = "Error"
14-
range = [240, 242]
16+
range = [241, 243]
17+
18+
[[message.snippets.annotations]]
19+
label = "expected due to this"
20+
level = "Error"
21+
range = [236, 238]
22+
1523

1624
[renderer]
1725
anonymized_line_numbers = true
18-
[renderer.margin]
19-
whitespace_left = 4
20-
span_left = 240
21-
span_right = 242
22-
label_right = 271
23-
column_width = 140
24-
max_line_len = 371

0 commit comments

Comments
 (0)