diff --git a/src/lib.rs b/src/lib.rs index b9671827..c72f003f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -436,14 +436,40 @@ pub enum OutputVerification { None, } +fn print_full_moon_error(error: &full_moon::Error) -> String { + match error { + full_moon::Error::AstError(ast_error) => format!( + "unexpected token `{}` ({}:{} to {}:{}), {}", + ast_error.token(), + ast_error.range().0.line(), + ast_error.range().0.character(), + ast_error.range().1.line(), + ast_error.range().1.character(), + ast_error.error_message() + ), + full_moon::Error::TokenizerError(tokenizer_error) => tokenizer_error.to_string(), + } +} + +fn print_full_moon_errors(errors: &[full_moon::Error]) -> String { + if errors.len() == 1 { + print_full_moon_error(errors.first().unwrap()) + } else { + errors + .iter() + .map(|err| "\n - ".to_string() + &print_full_moon_error(err)) + .collect::() + } +} + /// A formatting error #[derive(Clone, Debug, Error)] pub enum Error { /// The input AST has a parsing error. - #[error("error parsing: {0:?}")] + #[error("error parsing: {}", print_full_moon_errors(.0))] ParseError(Vec), /// The output AST after formatting generated a parse error. This is a definite error. - #[error("INTERNAL ERROR: Output AST generated a syntax error. Please report this at https://github.com/johnnymorganz/stylua/issues\n{0:?}")] + #[error("INTERNAL ERROR: Output AST generated a syntax error. Please report this at https://github.com/johnnymorganz/stylua/issues: {}", print_full_moon_errors(.0))] VerificationAstError(Vec), /// The output AST after formatting differs from the input AST. #[error("INTERNAL WARNING: Output AST may be different to input AST. Code correctness may have changed. Please examine the formatting diff and report any issues at https://github.com/johnnymorganz/stylua/issues")]