about summary refs log tree commit diff
path: root/src/librustc_errors
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2017-01-09 09:07:34 -0800
committerEsteban Küber <esteban@kuber.com.ar>2017-01-09 09:11:26 -0800
commit04e4a60b45ec4debd20be2327cb6859271502c05 (patch)
tree368f7709d1ea0b981a610b6fb6f6921534a1cc46 /src/librustc_errors
parent690476191db427ab8603876ef9a8a929222e71aa (diff)
downloadrust-04e4a60b45ec4debd20be2327cb6859271502c05.tar.gz
rust-04e4a60b45ec4debd20be2327cb6859271502c05.zip
Deduplicate and document logic
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/emitter.rs29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index 2640ff62d5b..77c6c368364 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -703,10 +703,29 @@ impl EmitterWriter {
         }
     }
 
-    fn msg_with_padding(&self, msg: &str, padding: usize) -> String {
-        let padding = (0..padding)
+    /// Add a left margin to every line but the first, given a padding length and the label being
+    /// displayed.
+    fn msg_with_padding(&self, msg: &str, padding: usize, label: &str) -> String {
+        // The extra 5 ` ` is padding that's always needed to align to the `note: `:
+        //
+        //   error: message
+        //     --> file.rs:13:20
+        //      |
+        //   13 |     <CODE>
+        //      |      ^^^^
+        //      |
+        //      = note: multiline
+        //              message
+        //   ++^^^----xx
+        //    |  |   | |
+        //    |  |   | magic `2`
+        //    |  |   length of label
+        //    |  magic `3`
+        //    `max_line_num_len`
+        let padding = (0..padding + label.len() + 5)
             .map(|_| " ")
             .collect::<String>();
+
         msg.split('\n').enumerate().fold("".to_owned(), |mut acc, x| {
             if x.0 != 0 {
                 acc.push_str("\n");
@@ -737,8 +756,7 @@ impl EmitterWriter {
             buffer.append(0, &level.to_string(), Style::HeaderMsg);
             buffer.append(0, ": ", Style::NoStyle);
 
-            // The extra 3 ` ` is the padding that's always needed to align to the `note: `.
-            let message = self.msg_with_padding(msg, max_line_num_len + "note: ".len() + 3);
+            let message = self.msg_with_padding(msg, max_line_num_len, "note");
             buffer.append(0, &message, Style::NoStyle);
         } else {
             buffer.append(0, &level.to_string(), Style::Level(level.clone()));
@@ -873,8 +891,7 @@ impl EmitterWriter {
             buffer.append(0, &level.to_string(), Style::Level(level.clone()));
             buffer.append(0, ": ", Style::HeaderMsg);
 
-            // The extra 3 ` ` is the padding that's always needed to align to the `suggestion: `.
-            let message = self.msg_with_padding(msg, max_line_num_len + "suggestion: ".len() + 3);
+            let message = self.msg_with_padding(msg, max_line_num_len, "suggestion");
             buffer.append(0, &message, Style::HeaderMsg);
 
             let lines = cm.span_to_lines(primary_span).unwrap();