about summary refs log tree commit diff
path: root/src/libsyntax/errors/snippet
diff options
context:
space:
mode:
authorJonathan Turner <jturner@mozilla.com>2016-04-28 16:39:59 -0700
committerNiko Matsakis <niko@alum.mit.edu>2016-05-02 11:49:25 -0400
commit84cb56f8ee11ba89914462e478f06e9c1e8e7971 (patch)
tree22419ea6949d9db864af11e5e8edd417fd49b992 /src/libsyntax/errors/snippet
parent49dfac487267b574c802e0bdf5a66c5ff0624340 (diff)
downloadrust-84cb56f8ee11ba89914462e478f06e9c1e8e7971.tar.gz
rust-84cb56f8ee11ba89914462e478f06e9c1e8e7971.zip
Add back in a 'old school' error format
Diffstat (limited to 'src/libsyntax/errors/snippet')
-rw-r--r--src/libsyntax/errors/snippet/mod.rs135
1 files changed, 104 insertions, 31 deletions
diff --git a/src/libsyntax/errors/snippet/mod.rs b/src/libsyntax/errors/snippet/mod.rs
index feaf48352db..18a64cc399c 100644
--- a/src/libsyntax/errors/snippet/mod.rs
+++ b/src/libsyntax/errors/snippet/mod.rs
@@ -17,11 +17,13 @@ use std::mem;
 
 mod test;
 
+#[derive(Clone)]
 pub struct SnippetData {
     codemap: Rc<CodeMap>,
     files: Vec<FileInfo>,
 }
 
+#[derive(Clone)]
 pub struct FileInfo {
     file: Rc<FileMap>,
 
@@ -35,6 +37,7 @@ pub struct FileInfo {
     lines: Vec<Line>,
 }
 
+#[derive(Clone)]
 struct Line {
     line_index: usize,
     annotations: Vec<Annotation>,
@@ -429,6 +432,10 @@ impl FileInfo {
     }
 
     fn render_file_lines(&self, codemap: &Rc<CodeMap>) -> Vec<RenderedLine> {
+        let old_school = match ::std::env::var("RUST_NEW_ERROR_FORMAT") {
+            Ok(_) => false,
+            Err(_) => true,
+        };
         // As a first step, we elide any instance of more than one
         // continuous unannotated line.
 
@@ -436,28 +443,30 @@ impl FileInfo {
         let mut output = vec![];
 
         // First insert the name of the file.
-        match self.primary_span {
-            Some(span) => {
-                let lo = codemap.lookup_char_pos(span.lo);
-                output.push(RenderedLine {
-                    text: vec![StyledString {
-                        text: lo.file.name.clone(),
-                        style: Style::FileNameStyle,
-                    }, StyledString {
-                        text: format!(":{}:{}", lo.line, lo.col.0 + 1),
-                        style: Style::LineAndColumn,
-                    }],
-                    kind: RenderedLineKind::PrimaryFileName,
-                });
-            }
-            None => {
-                output.push(RenderedLine {
-                    text: vec![StyledString {
-                        text: self.file.name.clone(),
-                        style: Style::FileNameStyle,
-                    }],
-                    kind: RenderedLineKind::OtherFileName,
-                });
+        if !old_school {
+            match self.primary_span {
+                Some(span) => {
+                    let lo = codemap.lookup_char_pos(span.lo);
+                    output.push(RenderedLine {
+                        text: vec![StyledString {
+                            text: lo.file.name.clone(),
+                            style: Style::FileNameStyle,
+                        }, StyledString {
+                            text: format!(":{}:{}", lo.line, lo.col.0 + 1),
+                            style: Style::LineAndColumn,
+                        }],
+                        kind: RenderedLineKind::PrimaryFileName,
+                    });
+                }
+                None => {
+                    output.push(RenderedLine {
+                        text: vec![StyledString {
+                            text: self.file.name.clone(),
+                            style: Style::FileNameStyle,
+                        }],
+                        kind: RenderedLineKind::OtherFileName,
+                    });
+                }
             }
         }
 
@@ -466,7 +475,31 @@ impl FileInfo {
             // Consume lines with annotations.
             while let Some(line) = next_line {
                 if line.annotations.is_empty() { break; }
-                output.append(&mut self.render_line(line));
+
+                let mut rendered_line = self.render_line(line);
+                if old_school {
+                    match self.primary_span {
+                        Some(span) => {
+                            let lo = codemap.lookup_char_pos(span.lo);
+                            rendered_line[0].text.insert(0, StyledString {
+                                text: format!(":{} ", lo.line),
+                                style: Style::LineAndColumn,
+                            });
+                            rendered_line[0].text.insert(0, StyledString {
+                                text: lo.file.name.clone(),
+                                style: Style::FileNameStyle,
+                            });
+                            let gap_amount = rendered_line[0].text[0].text.len() +
+                                rendered_line[0].text[1].text.len();
+                            rendered_line[1].text.insert(0, StyledString {
+                                text: vec![" "; gap_amount].join(""),
+                                style: Style::NoStyle
+                            });
+                        }
+                        _ =>()
+                    }
+                }
+                output.append(&mut rendered_line);
                 next_line = lines_iter.next();
             }
 
@@ -492,6 +525,10 @@ impl FileInfo {
     }
 
     fn render_line(&self, line: &Line) -> Vec<RenderedLine> {
+        let old_school = match ::std::env::var("RUST_NEW_ERROR_FORMAT") {
+            Ok(_) => false,
+            Err(_) => true,
+        };
         let source_string = self.file.get_line(line.line_index)
                                      .unwrap_or("");
         let source_kind = RenderedLineKind::SourceText {
@@ -535,12 +572,34 @@ impl FileInfo {
 
         // Next, create the highlight line.
         for annotation in &annotations {
-            for p in annotation.start_col .. annotation.end_col {
-                if annotation.is_primary {
-                    styled_buffer.putc(1, p, '^', Style::UnderlinePrimary);
-                    styled_buffer.set_style(0, p, Style::UnderlinePrimary);
-                } else {
-                    styled_buffer.putc(1, p, '-', Style::UnderlineSecondary);
+            if old_school {
+                for p in annotation.start_col .. annotation.end_col {
+                    if p == annotation.start_col {
+                        styled_buffer.putc(1, p, '^',
+                            if annotation.is_primary {
+                                Style::UnderlinePrimary
+                            } else {
+                                Style::UnderlineSecondary
+                            });
+                    }
+                    else {
+                        styled_buffer.putc(1, p, '~',
+                            if annotation.is_primary {
+                                Style::UnderlinePrimary
+                            } else {
+                                Style::UnderlineSecondary
+                            });
+                    }
+                }
+            }
+            else {
+                for p in annotation.start_col .. annotation.end_col {
+                    if annotation.is_primary {
+                        styled_buffer.putc(1, p, '^', Style::UnderlinePrimary);
+                        styled_buffer.set_style(0, p, Style::UnderlinePrimary);
+                    } else {
+                        styled_buffer.putc(1, p, '-', Style::UnderlineSecondary);
+                    }
                 }
             }
         }
@@ -555,6 +614,9 @@ impl FileInfo {
         if labeled_annotations.is_empty() {
             return styled_buffer.render(source_kind);
         }
+        if old_school {
+            return styled_buffer.render(source_kind);
+        }
 
         // Now add the text labels. We try, when possible, to stick the rightmost
         // annotation at the end of the highlight line:
@@ -647,6 +709,14 @@ impl FileInfo {
 }
 
 fn prepend_prefixes(rendered_lines: &mut [RenderedLine]) {
+    let old_school = match ::std::env::var("RUST_NEW_ERROR_FORMAT") {
+        Ok(_) => false,
+        Err(_) => true,
+    };
+    if old_school {
+        return;
+    }
+
     let prefixes: Vec<_> =
         rendered_lines.iter()
                       .map(|rl| rl.kind.prefix())
@@ -686,11 +756,14 @@ fn prepend_prefixes(rendered_lines: &mut [RenderedLine]) {
                                                   style: Style::LineNumber})
             }
             RenderedLineKind::OtherFileName => {
-                // >>>>> filename
+                //   ::: filename
                 // 22 |>
                 //   ^
                 //   padding_len
-                let dashes = (0..padding_len + 2).map(|_| '>')
+                let dashes = (0..padding_len - 1).map(|_| ' ')
+                                                 .chain(Some(':'))
+                                                 .chain(Some(':'))
+                                                 .chain(Some(':'))
                                                  .chain(Some(' '));
                 line.text.insert(0, StyledString {text: dashes.collect(),
                                                   style: Style::LineNumber})