Skip to content

Commit d22f422

Browse files
author
Zibi Braniecki
committed
Support multiline labels in SourceAnnotation
1 parent 2e007da commit d22f422

File tree

3 files changed

+113
-60
lines changed

3 files changed

+113
-60
lines changed

src/display_list_formatting.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ pub trait DisplayListFormatting {
77

88
fn format_inline_marks(inline_marks: &[DisplayMark], inline_marks_width: usize) -> String;
99

10-
fn format_annotation_content(
10+
fn format_source_annotation_lines(
11+
f: &mut fmt::Formatter,
12+
lineno_width: usize,
13+
inline_marks: String,
1114
range: &(usize, usize),
1215
label: &[DisplayTextFragment],
1316
annotation_type: &DisplayAnnotationType,
1417
annotation_part: &DisplayAnnotationPart,
15-
) -> String;
18+
) -> fmt::Result;
1619

1720
fn format_label(label: &[DisplayTextFragment]) -> String;
1821

src/format.rs

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,16 @@ impl DisplayListFormatting for Formatter {
3030
)
3131
}
3232

33-
fn format_annotation_content(
33+
fn format_source_annotation_lines(
34+
f: &mut fmt::Formatter,
35+
lineno_width: usize,
36+
inline_marks: String,
3437
range: &(usize, usize),
3538
label: &[DisplayTextFragment],
3639
annotation_type: &DisplayAnnotationType,
3740
annotation_part: &DisplayAnnotationPart,
38-
) -> String {
39-
let label = if label.is_empty() {
40-
"".to_string()
41-
} else {
42-
format!(" {}", Self::format_label(label))
43-
};
44-
let prefix = match annotation_part {
41+
) -> fmt::Result {
42+
let indent_char = match annotation_part {
4543
DisplayAnnotationPart::Singleline => " ",
4644
DisplayAnnotationPart::MultilineStart => "_",
4745
DisplayAnnotationPart::MultilineEnd => "_",
@@ -52,12 +50,42 @@ impl DisplayListFormatting for Formatter {
5250
DisplayAnnotationType::Note => "-",
5351
DisplayAnnotationType::Help => "-",
5452
};
55-
format!(
56-
"{}{}{}",
57-
prefix.repeat(range.0),
58-
mark.repeat(range.1 - range.0),
59-
label,
60-
)
53+
if let Some((first, rest)) = Self::format_label(label)
54+
.lines()
55+
.collect::<Vec<&str>>()
56+
.split_first()
57+
{
58+
let indent = range.1;
59+
writeln!(
60+
f,
61+
"{}{}{}{} {}",
62+
format!("{} |", " ".repeat(lineno_width)),
63+
inline_marks,
64+
indent_char.repeat(range.0),
65+
mark.repeat(range.1 - range.0),
66+
first,
67+
)?;
68+
for line in rest {
69+
writeln!(
70+
f,
71+
"{}{}{} {}",
72+
format!("{} |", " ".repeat(lineno_width)),
73+
inline_marks,
74+
" ".repeat(indent),
75+
line,
76+
)?;
77+
}
78+
} else {
79+
writeln!(
80+
f,
81+
"{}{}{}{}",
82+
format!("{} |", " ".repeat(lineno_width)),
83+
inline_marks,
84+
indent_char.repeat(range.0),
85+
mark.repeat(range.1 - range.0),
86+
)?;
87+
}
88+
Ok(())
6189
}
6290

6391
fn format_label(label: &[DisplayTextFragment]) -> String {
@@ -147,21 +175,15 @@ impl DisplayListFormatting for Formatter {
147175
label,
148176
annotation_type,
149177
annotation_part,
150-
} => {
151-
let prefix = format!("{} |", " ".repeat(lineno_width));
152-
writeln!(
153-
f,
154-
"{}{}{}",
155-
prefix,
156-
Self::format_inline_marks(&inline_marks, inline_marks_width),
157-
Self::format_annotation_content(
158-
range,
159-
&label,
160-
&annotation_type,
161-
&annotation_part
162-
),
163-
)
164-
}
178+
} => Self::format_source_annotation_lines(
179+
f,
180+
lineno_width,
181+
Self::format_inline_marks(&inline_marks, inline_marks_width),
182+
range,
183+
&label,
184+
&annotation_type,
185+
&annotation_part,
186+
),
165187
DisplayLine::Fold { inline_marks } => writeln!(
166188
f,
167189
"... {}",

src/format_color.rs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,16 @@ impl DisplayListFormatting for Formatter {
3434
)
3535
}
3636

37-
fn format_annotation_content(
37+
fn format_source_annotation_lines(
38+
f: &mut fmt::Formatter,
39+
lineno_width: usize,
40+
inline_marks: String,
3841
range: &(usize, usize),
3942
label: &[DisplayTextFragment],
4043
annotation_type: &DisplayAnnotationType,
4144
annotation_part: &DisplayAnnotationPart,
42-
) -> String {
43-
let label = if label.is_empty() {
44-
"".to_string()
45-
} else {
46-
format!(" {}", Self::format_label(label))
47-
};
48-
let prefix = match annotation_part {
45+
) -> fmt::Result {
46+
let indent_char = match annotation_part {
4947
DisplayAnnotationPart::Singleline => " ",
5048
DisplayAnnotationPart::MultilineStart => "_",
5149
DisplayAnnotationPart::MultilineEnd => "_",
@@ -62,12 +60,48 @@ impl DisplayListFormatting for Formatter {
6260
DisplayAnnotationType::Note => Style::new().bold(),
6361
DisplayAnnotationType::Help => Fixed(14).bold(),
6462
};
65-
format!(
66-
"{}{}{}",
67-
prefix.repeat(range.0),
68-
color.paint(mark.repeat(range.1 - range.0)),
69-
color.paint(label),
70-
)
63+
if let Some((first, rest)) = Self::format_label(label)
64+
.lines()
65+
.collect::<Vec<&str>>()
66+
.split_first()
67+
{
68+
let indent = range.1;
69+
writeln!(
70+
f,
71+
"{}{}{}{} {}",
72+
Fixed(12)
73+
.bold()
74+
.paint(format!("{} |", " ".repeat(lineno_width))),
75+
inline_marks,
76+
indent_char.repeat(range.0),
77+
color.paint(mark.repeat(range.1 - range.0)),
78+
color.paint(*first),
79+
)?;
80+
for line in rest {
81+
writeln!(
82+
f,
83+
"{}{}{} {}",
84+
Fixed(12)
85+
.bold()
86+
.paint(format!("{} |", " ".repeat(lineno_width))),
87+
inline_marks,
88+
" ".repeat(indent),
89+
color.paint(*line),
90+
)?;
91+
}
92+
} else {
93+
writeln!(
94+
f,
95+
"{}{}{}{}",
96+
Fixed(12)
97+
.bold()
98+
.paint(format!("{} |", " ".repeat(lineno_width))),
99+
inline_marks,
100+
indent_char.repeat(range.0),
101+
color.paint(mark.repeat(range.1 - range.0)),
102+
)?;
103+
}
104+
Ok(())
71105
}
72106

73107
fn format_label(label: &[DisplayTextFragment]) -> String {
@@ -185,21 +219,15 @@ impl DisplayListFormatting for Formatter {
185219
label,
186220
annotation_type,
187221
annotation_part,
188-
} => {
189-
let prefix = format!("{} |", " ".repeat(lineno_width));
190-
writeln!(
191-
f,
192-
"{}{}{}",
193-
Fixed(12).bold().paint(prefix),
194-
Self::format_inline_marks(&inline_marks, inline_marks_width),
195-
Self::format_annotation_content(
196-
range,
197-
&label,
198-
&annotation_type,
199-
&annotation_part
200-
),
201-
)
202-
}
222+
} => Self::format_source_annotation_lines(
223+
f,
224+
lineno_width,
225+
Self::format_inline_marks(&inline_marks, inline_marks_width),
226+
range,
227+
&label,
228+
&annotation_type,
229+
&annotation_part,
230+
),
203231
DisplayLine::Fold { inline_marks } => writeln!(
204232
f,
205233
"... {}",

0 commit comments

Comments
 (0)