fmt: minor code cleanup (#8147)

pull/8159/head
Lukas Neubert 2021-01-17 05:32:10 +01:00 committed by GitHub
parent 5185a59ac7
commit 3e44816e44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -700,14 +700,14 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
comments := field.comments
// Handle comments before field
mut comm_idx := 0
f.indent++
for comm_idx < comments.len && comments[comm_idx].pos.pos < field.pos.pos {
f.indent++
f.empty_line = true
f.comment(comments[comm_idx], {})
f.writeln('')
f.indent--
comm_idx++
}
f.indent--
f.write('\t$field.name ')
// Handle comments between field name and type
mut comments_len := 0
@ -1834,9 +1834,16 @@ pub fn (mut f Fmt) chan_init(mut it ast.ChanInit) {
f.write('}')
}
fn should_decrease_arr_penalty(e ast.Expr) bool {
if e is ast.ArrayInit || e is ast.StructInit || e is ast.MapInit || e is ast.CallExpr {
return true
}
return false
}
pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
// `x := []string`
// `x := []string{}`
f.write(f.table.type_to_str_using_aliases(it.typ, f.mod2alias))
f.write('{')
if it.has_len {
@ -1878,14 +1885,10 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
line_break := f.array_init_break[f.array_init_depth - 1]
mut penalty := if line_break && !is_same_line_comment { 0 } else { 3 }
if penalty > 0 {
if i == 0 || it.exprs[i - 1] is ast.ArrayInit || it.exprs[i - 1] is ast.StructInit ||
it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr
{
if i == 0 || should_decrease_arr_penalty(it.exprs[i - 1]) {
penalty--
}
if expr is ast.ArrayInit ||
expr is ast.StructInit || expr is ast.MapInit || expr is ast.CallExpr
{
if should_decrease_arr_penalty(expr) {
penalty--
}
}

View File

@ -59,6 +59,9 @@ fn test_fmt() {
if expected_ocontent != result_ocontent {
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file $vrelpath after formatting, does not look as expected.'))
if ipath.ends_with(b2v_keep_path) {
continue
}
if diff_cmd == '' {
eprintln('>> sorry, but no working "diff" CLI command can be found')
continue