Skip to content

Commit 04e4a60

Browse files
committed
Deduplicate and document logic
1 parent 6904761 commit 04e4a60

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/librustc_errors/emitter.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,29 @@ impl EmitterWriter {
703703
}
704704
}
705705

706-
fn msg_with_padding(&self, msg: &str, padding: usize) -> String {
707-
let padding = (0..padding)
706+
/// Add a left margin to every line but the first, given a padding length and the label being
707+
/// displayed.
708+
fn msg_with_padding(&self, msg: &str, padding: usize, label: &str) -> String {
709+
// The extra 5 ` ` is padding that's always needed to align to the `note: `:
710+
//
711+
// error: message
712+
// --> file.rs:13:20
713+
// |
714+
// 13 | <CODE>
715+
// | ^^^^
716+
// |
717+
// = note: multiline
718+
// message
719+
// ++^^^----xx
720+
// | | | |
721+
// | | | magic `2`
722+
// | | length of label
723+
// | magic `3`
724+
// `max_line_num_len`
725+
let padding = (0..padding + label.len() + 5)
708726
.map(|_| " ")
709727
.collect::<String>();
728+
710729
msg.split('\n').enumerate().fold("".to_owned(), |mut acc, x| {
711730
if x.0 != 0 {
712731
acc.push_str("\n");
@@ -737,8 +756,7 @@ impl EmitterWriter {
737756
buffer.append(0, &level.to_string(), Style::HeaderMsg);
738757
buffer.append(0, ": ", Style::NoStyle);
739758

740-
// The extra 3 ` ` is the padding that's always needed to align to the `note: `.
741-
let message = self.msg_with_padding(msg, max_line_num_len + "note: ".len() + 3);
759+
let message = self.msg_with_padding(msg, max_line_num_len, "note");
742760
buffer.append(0, &message, Style::NoStyle);
743761
} else {
744762
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
@@ -873,8 +891,7 @@ impl EmitterWriter {
873891
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
874892
buffer.append(0, ": ", Style::HeaderMsg);
875893

876-
// The extra 3 ` ` is the padding that's always needed to align to the `suggestion: `.
877-
let message = self.msg_with_padding(msg, max_line_num_len + "suggestion: ".len() + 3);
894+
let message = self.msg_with_padding(msg, max_line_num_len, "suggestion");
878895
buffer.append(0, &message, Style::HeaderMsg);
879896

880897
let lines = cm.span_to_lines(primary_span).unwrap();

0 commit comments

Comments
 (0)