FIX: Don't print an extraneous new line on fmt.

Fixes: #49
This commit is contained in:
Jeremy Wall 2019-10-02 19:53:20 -05:00
parent 8f64d3385a
commit b9309ce12b
2 changed files with 61 additions and 56 deletions

View File

@ -473,9 +473,11 @@ where
write!(self.w, "{{\n")?; write!(self.w, "{{\n")?;
self.curr_indent += self.indent_size; self.curr_indent += self.indent_size;
let indent = self.make_indent(); let indent = self.make_indent();
let mut prefix_newline = false;
for stmt in _def.statements.iter() { for stmt in _def.statements.iter() {
write!(self.w, "{}", indent)?; write!(self.w, "{}", indent)?;
self.render_stmt(stmt)?; self.render_stmt(stmt, prefix_newline)?;
prefix_newline = true;
} }
self.curr_indent -= self.indent_size; self.curr_indent -= self.indent_size;
write!(self.w, "}}")?; write!(self.w, "}}")?;
@ -532,8 +534,11 @@ where
Ok(()) Ok(())
} }
pub fn render_stmt(&mut self, stmt: &Statement) -> std::io::Result<()> { pub fn render_stmt(&mut self, stmt: &Statement, prefix_newline: bool) -> std::io::Result<()> {
// All statements start at the beginning of a line. // All statements start at the beginning of a line.
if prefix_newline {
write!(self.w, "\n")?;
}
let line = stmt.pos().line; let line = stmt.pos().line;
self.render_comment_if_needed(line)?; self.render_comment_if_needed(line)?;
match stmt { match stmt {
@ -557,14 +562,16 @@ where
self.render_expr(&_expr)?; self.render_expr(&_expr)?;
} }
}; };
write!(self.w, ";\n\n")?; write!(self.w, ";\n")?;
self.last_line = line; self.last_line = line;
Ok(()) Ok(())
} }
pub fn render(&mut self, stmts: &Vec<Statement>) -> std::io::Result<()> { pub fn render(&mut self, stmts: &Vec<Statement>) -> std::io::Result<()> {
let mut prefix_newline = false;
for v in stmts { for v in stmts {
self.render_stmt(v)?; self.render_stmt(v, prefix_newline)?;
prefix_newline = true;
} }
let comment_line = self.comment_group_lines.first().cloned(); let comment_line = self.comment_group_lines.first().cloned();
if let Some(last_comment_line) = comment_line { if let Some(last_comment_line) = comment_line {

View File

@ -33,67 +33,67 @@ fn print_to_buffer(input: &str) -> String {
#[test] #[test]
fn test_simple_value_printing() { fn test_simple_value_printing() {
let input = "1;"; let input = "1;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_simple_selector_printing() { fn test_simple_selector_printing() {
let input = "foo.bar.quux;"; let input = "foo.bar.quux;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_simple_quoted_printing() { fn test_simple_quoted_printing() {
let input = "\"foo\";"; let input = "\"foo\";";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_escaped_quoted_printing() { fn test_escaped_quoted_printing() {
let input = "\"f\\\\o\\\"o\";"; let input = "\"f\\\\o\\\"o\";";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_empty_tuple_printing() { fn test_empty_tuple_printing() {
let input = "{};"; let input = "{};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_empty_list_printing() { fn test_empty_list_printing() {
let input = "[];"; let input = "[];";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_non_empty_tuple_printing() { fn test_non_empty_tuple_printing() {
let input = "{\n foo = 1,\n};"; let input = "{\n foo = 1,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_nested_empty_tuple_printing() { fn test_nested_empty_tuple_printing() {
let input = "{\n foo = {},\n};"; let input = "{\n foo = {},\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_list_nested_empty_tuple_printing() { fn test_list_nested_empty_tuple_printing() {
let input = "[\n {},\n];"; let input = "[\n {},\n];";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_nested_non_empty_tuple_printing() { fn test_nested_non_empty_tuple_printing() {
let input = "{\n foo = {\n bar = 1,\n },\n};"; let input = "{\n foo = {\n bar = 1,\n },\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_nested_non_empty_list_printing() { fn test_nested_non_empty_list_printing() {
let input = "[\n [\n 1,\n ],\n];"; let input = "[\n [\n 1,\n ],\n];";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -101,80 +101,80 @@ fn test_simple_quoted_field_tuple_printing() {
let input = "{\n \"foo\" = {\n bar = 1,\n },\n};"; let input = "{\n \"foo\" = {\n bar = 1,\n },\n};";
assert_eq!( assert_eq!(
print_to_buffer(input), print_to_buffer(input),
format!("{}\n\n", "{\n foo = {\n bar = 1,\n },\n};") format!("{}\n", "{\n foo = {\n bar = 1,\n },\n};")
); );
} }
#[test] #[test]
fn test_special_quoted_field_tuple_printing() { fn test_special_quoted_field_tuple_printing() {
let input = "{\n \"foo bar\" = {\n bar = 1,\n },\n};"; let input = "{\n \"foo bar\" = {\n bar = 1,\n },\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_let_statement_printing() { fn test_let_statement_printing() {
let input = "let tpl = {\n \"foo bar\" = {\n bar = 1,\n },\n};"; let input = "let tpl = {\n \"foo bar\" = {\n bar = 1,\n },\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_call_expr_printing() { fn test_call_expr_printing() {
let input = "call(\n foo,\n bar,\n);"; let input = "call(\n foo,\n bar,\n);";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_call_expr_one_arg_printing() { fn test_call_expr_one_arg_printing() {
let input = "call(foo);"; let input = "call(foo);";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_copy_expr_printing() { fn test_copy_expr_printing() {
let input = "copy{\n foo = 1,\n bar = 2,\n};"; let input = "copy{\n foo = 1,\n bar = 2,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_copy_expr_one_arg_printing() { fn test_copy_expr_one_arg_printing() {
let input = "copy{\n foo = 1,\n};"; let input = "copy{\n foo = 1,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_out_expr_printing() { fn test_out_expr_printing() {
let input = "out json {\n foo = 1,\n};"; let input = "out json {\n foo = 1,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_select_expr_no_default_printing() { fn test_select_expr_no_default_printing() {
let input = "select true, {\n true = 1,\n false = 2,\n};"; let input = "select true, {\n true = 1,\n false = 2,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_select_expr_with_default_printing() { fn test_select_expr_with_default_printing() {
let input = "select true, 3, {\n true = 1,\n false = 2,\n};"; let input = "select true, 3, {\n true = 1,\n false = 2,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_not_expr_printing() { fn test_not_expr_printing() {
let input = "not true;"; let input = "not true;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_fail_expr_printing() { fn test_fail_expr_printing() {
let input = "fail \"AHHh\";"; let input = "fail \"AHHh\";";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_trace_expr_printing() { fn test_trace_expr_printing() {
let input = "TRACE \"AHHh\";"; let input = "TRACE \"AHHh\";";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -189,9 +189,8 @@ fn test_module_no_out_expr_printing() {
memory_size = mod.mem, memory_size = mod.mem,
cpu_count = mod.cpu, cpu_count = mod.cpu,
}; };
};"; };";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -206,9 +205,8 @@ fn test_module_with_out_expr_printing() {
memory_size = mod.mem, memory_size = mod.mem,
cpu_count = mod.cpu, cpu_count = mod.cpu,
}; };
};"; };";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -217,7 +215,7 @@ fn test_func_expr_printing() {
foo = foo, foo = foo,
bar = bar, bar = bar,
};"; };";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -225,7 +223,7 @@ fn test_func_expr_single_arg_printing() {
let input = "let f = func (foo) => { let input = "let f = func (foo) => {
foo = foo, foo = foo,
};"; };";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -233,7 +231,7 @@ fn test_format_expr_single_arg_printing() {
let input = "\"what? @{item.foo}\" % { let input = "\"what? @{item.foo}\" % {
foo = 1, foo = 1,
};"; };";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -241,37 +239,37 @@ fn test_format_expr_list_arg_printing() {
let input = "\"what? @ @\" % ( let input = "\"what? @ @\" % (
1, 1,
2);"; 2);";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_statement_with_comment_printing() { fn test_statement_with_comment_printing() {
let input = "// add 1 + 1\n1 + 1;"; let input = "// add 1 + 1\n1 + 1;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_statement_with_comment_printing_groups() { fn test_statement_with_comment_printing_groups() {
let input = "// add 1\n// and 1\n1 + 1;"; let input = "// add 1\n// and 1\n1 + 1;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_statement_with_comment_printing_multiple_groups() { fn test_statement_with_comment_printing_multiple_groups() {
let input = "\n// group 1\n// more group 1\n\n// group 2\n// more group 2\n1 + 1;"; let input = "\n// group 1\n// more group 1\n\n// group 2\n// more group 2\n1 + 1;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input.trim())); assert_eq!(print_to_buffer(input), format!("{}\n", input.trim()));
} }
#[test] #[test]
fn test_statement_with_comment_printing_comments_at_end() { fn test_statement_with_comment_printing_comments_at_end() {
let input = "// group 1\n1 + 1;\n\n// group 2\n\n"; let input = "// group 1\n1 + 1;\n\n// group 2\n\n";
assert_eq!(print_to_buffer(input), format!("{}\n", input.trim())); assert_eq!(print_to_buffer(input), input.replace("\n\n", "\n"));
} }
#[test] #[test]
fn test_tuple_expression_with_embedded_comment() { fn test_tuple_expression_with_embedded_comment() {
let input = "{\n foo = bar,\n // a comment\n bar = foo,\n};"; let input = "{\n foo = bar,\n // a comment\n bar = foo,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
@ -285,7 +283,7 @@ fn test_tuple_expression_with_embedded_comment_same_line() {
foo = bar, foo = bar,
bar = foo, bar = foo,
};"; };";
assert_eq!(print_to_buffer(input), format!("{}\n\n", expected)); assert_eq!(print_to_buffer(input), format!("{}\n", expected));
} }
#[test] #[test]
@ -293,7 +291,7 @@ fn test_tuple_expression_with_embedded_comment_mid_field_expr() {
let input = "{\n foo = bar,\n bar =\n// a comment\n foo\n};"; let input = "{\n foo = bar,\n bar =\n// a comment\n foo\n};";
assert_eq!( assert_eq!(
print_to_buffer(input), print_to_buffer(input),
"{\n foo = bar,\n // a comment\n bar = foo,\n};\n\n" "{\n foo = bar,\n // a comment\n bar = foo,\n};\n"
); );
} }
@ -302,57 +300,57 @@ fn test_tuple_expression_with_embedded_comment_and_mid_field_expr() {
let input = "{\n foo = bar,\n// a comment\n bar =\n// another comment\n foo\n};"; let input = "{\n foo = bar,\n// a comment\n bar =\n// another comment\n foo\n};";
assert_eq!( assert_eq!(
print_to_buffer(input), print_to_buffer(input),
"{\n foo = bar,\n // a comment\n // another comment\n bar = foo,\n};\n\n" "{\n foo = bar,\n // a comment\n // another comment\n bar = foo,\n};\n"
); );
} }
#[test] #[test]
fn test_list_expression_with_embedded_comment() { fn test_list_expression_with_embedded_comment() {
let input = "[\n bar,\n // a comment\n foo,\n];"; let input = "[\n bar,\n // a comment\n foo,\n];";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_binary_expression_with_embedded_comment() { fn test_binary_expression_with_embedded_comment() {
let input = "true == \n// false is not true\nfalse;"; let input = "true == \n// false is not true\nfalse;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_empty_call_expression_with_comment() { fn test_empty_call_expression_with_comment() {
let input = "// a comment\nmyfunc();"; let input = "// a comment\nmyfunc();";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_call_expression_with_embedded_comment_in_args() { fn test_call_expression_with_embedded_comment_in_args() {
let input = "// a comment\nmyfunc(\n arg1,\n // another comment\n arg2,\n);"; let input = "// a comment\nmyfunc(\n arg1,\n // another comment\n arg2,\n);";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_copy_expression_with_embedded_comment_in_args() { fn test_copy_expression_with_embedded_comment_in_args() {
let input = "// a comment\nmyfunc{\n foo = arg1,\n // another comment\n bar = arg2,\n};"; let input = "// a comment\nmyfunc{\n foo = arg1,\n // another comment\n bar = arg2,\n};";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_trace_expression_with_embedded_comment() { fn test_trace_expression_with_embedded_comment() {
let input = "// a comment\nTRACE \n// another comment\nfoo;"; let input = "// a comment\nTRACE \n// another comment\nfoo;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_fail_expression_with_embedded_comment() { fn test_fail_expression_with_embedded_comment() {
let input = "// a comment\nfail \n// another comment\nfoo;"; let input = "// a comment\nfail \n// another comment\nfoo;";
assert_eq!(print_to_buffer(input), format!("{}\n\n", input)); assert_eq!(print_to_buffer(input), format!("{}\n", input));
} }
#[test] #[test]
fn test_format_expression_with_embedded_comment() { fn test_format_expression_with_embedded_comment() {
let input = "// a comment\n\"@(item.bar)\" % \n// another comment\nfoo;"; let input = "// a comment\n\"@(item.bar)\" % \n// another comment\nfoo;";
let output = print_to_buffer(input); let output = print_to_buffer(input);
assert_eq!(output, format!("{}\n\n", input.trim())); assert_eq!(output, format!("{}\n", input.trim()));
} }
#[test] #[test]
@ -360,7 +358,7 @@ fn test_filter_func_operator_expression_with_embedded_comment() {
//let input = "// a comment\nfilter(foo, bar);"; //let input = "// a comment\nfilter(foo, bar);";
let input = "// a comment\nfilter(\n // another comment\n foo,\n // one more\n bar);"; let input = "// a comment\nfilter(\n // another comment\n foo,\n // one more\n bar);";
let output = print_to_buffer(input); let output = print_to_buffer(input);
assert_eq!(output, format!("{}\n\n", input.trim())); assert_eq!(output, format!("{}\n", input.trim()));
} }
#[test] #[test]
@ -373,7 +371,7 @@ fn test_reduce_func_operator_expression_with_embedded_comment() {
// and the last // and the last
target);"; target);";
let output = print_to_buffer(input); let output = print_to_buffer(input);
assert_eq!(output, format!("{}\n\n", input.trim())); assert_eq!(output, format!("{}\n", input.trim()));
} }
#[test] #[test]
@ -381,7 +379,7 @@ fn test_map_func_operator_expression_with_embedded_comment() {
//let input = "// a comment\nfilter(foo, bar);"; //let input = "// a comment\nfilter(foo, bar);";
let input = "// a comment\nmap(\n // another comment\n foo,\n // one more\n bar);"; let input = "// a comment\nmap(\n // another comment\n foo,\n // one more\n bar);";
let output = print_to_buffer(input); let output = print_to_buffer(input);
assert_eq!(output, format!("{}\n\n", input.trim())); assert_eq!(output, format!("{}\n", input.trim()));
} }
#[test] #[test]
@ -389,5 +387,5 @@ fn test_grouped_expression_with_embedded_comment() {
//let input = "// a comment\nfilter(foo, bar);"; //let input = "// a comment\nfilter(foo, bar);";
let input = "// a comment\n(\n // a comment\n foo\n);"; let input = "// a comment\n(\n // a comment\n foo\n);";
let output = print_to_buffer(input); let output = print_to_buffer(input);
assert_eq!(output, format!("{}\n\n", input.trim())); assert_eq!(output, format!("{}\n", input.trim()));
} }