From 0f29b4fc2a4178cba4ca0bc82879bf18abddff8a Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 28 Oct 2016 20:34:25 -0700 Subject: [PATCH] Pretty errors with underlined tokens! --- notes | 3 --- src/integration.rs | 16 ++++++++++++++++ src/lib.rs | 14 ++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/notes b/notes index 97ffe05..6617ff5 100644 --- a/notes +++ b/notes @@ -1,9 +1,6 @@ notes ----- -- integration testing - . underline problem token in error messages - - figure out argument passing: . flag: j build --set a=hello . by export: A=HELLO j build diff --git a/src/integration.rs b/src/integration.rs index 316e6ce..de9e5ab 100644 --- a/src/integration.rs +++ b/src/integration.rs @@ -198,3 +198,19 @@ recipe: "Recipe \"recipe\" failed with exit code 100\n", ); } + +#[test] +fn error() { + integration_test( + "error", + &[], + "bar:\nhello:\nfoo: bar baaaaaaaz hello", + 255, + "", + "error: recipe foo has unknown dependency baaaaaaaz + | +3 | foo: bar baaaaaaaz hello + | ^^^^^^^^^ +", + ); +} diff --git a/src/lib.rs b/src/lib.rs index a82ed39..7cefa66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -479,7 +479,7 @@ impl<'a, T: Display> Display for Or<'a, T> { impl<'a> Display for Error<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - try!(write!(f, "justfile:{}: ", self.line)); + try!(write!(f, "error: ")); match self.kind { ErrorKind::BadName{name} => { @@ -487,7 +487,7 @@ impl<'a> Display for Error<'a> { } ErrorKind::CircularRecipeDependency{recipe, ref circle} => { if circle.len() == 2 { - try!(write!(f, "recipe {} depends on itself", recipe)); + try!(write!(f, "recipe {} depends on itself:", recipe)); } else { try!(write!(f, "recipe {} has circular dependency: {}", recipe, circle.join(" -> "))); } @@ -556,9 +556,15 @@ impl<'a> Display for Error<'a> { } match self.text.lines().nth(self.line) { - Some(line) => try!(write!(f, "{}", line)), + Some(line) => { + let line_number_width = self.line.to_string().len(); + try!(write!(f, "{0:1$} |\n", "", line_number_width)); + try!(write!(f, "{} | {}\n", self.line + 1, line)); + try!(write!(f, "{0:1$} |", "", line_number_width)); + try!(write!(f, " {0:1$}{2:^<3$}", "", self.column, "", self.width.unwrap_or(0))); + }, None => if self.index != self.text.len() { - try!(write!(f, "internal error: Error has invalid line number: {}", self.line)) + try!(write!(f, "internal error: Error has invalid line number: {}", self.line + 1)) }, };