fmt: keep single line if in struct init (#8734)

pull/8757/head
zakuro 2021-02-15 03:22:24 +09:00 committed by GitHub
parent e534b4397d
commit d3bcd5d305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View File

@ -58,11 +58,7 @@ pub fn new_builder(pref &pref.Preferences) Builder {
parent: 0 parent: 0
} }
compiled_dir: compiled_dir compiled_dir: compiled_dir
max_nr_errors: if pref.error_limit > 0 { max_nr_errors: if pref.error_limit > 0 { pref.error_limit } else { 100 }
pref.error_limit
} else {
100
}
cached_msvc: msvc cached_msvc: msvc
} }
// max_nr_errors: pref.error_limit ?? 100 TODO potential syntax? // max_nr_errors: pref.error_limit ?? 100 TODO potential syntax?

View File

@ -36,6 +36,7 @@ pub mut:
file ast.File file ast.File
did_imports bool did_imports bool
is_assign bool is_assign bool
is_struct_init bool
auto_imports []string // automatically inserted imports that the user forgot to specify auto_imports []string // automatically inserted imports that the user forgot to specify
import_pos int // position of the imports in the resulting string for later autoimports insertion import_pos int // position of the imports in the resulting string for later autoimports insertion
used_imports []string // to remove unused imports used_imports []string // to remove unused imports
@ -1636,7 +1637,7 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
dollar := if node.is_comptime { '$' } else { '' } dollar := if node.is_comptime { '$' } else { '' }
mut single_line := node.branches.len == 2 && node.has_else mut single_line := node.branches.len == 2 && node.has_else
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1]) && branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
&& (node.is_expr || f.is_assign || f.single_line_fields) && (node.is_expr || f.is_assign || f.is_struct_init || f.single_line_fields)
f.single_line_if = single_line f.single_line_if = single_line
if_start := f.line_len if_start := f.line_len
for { for {
@ -2067,6 +2068,12 @@ pub fn (mut f Fmt) map_init(it ast.MapInit) {
} }
pub fn (mut f Fmt) struct_init(it ast.StructInit) { pub fn (mut f Fmt) struct_init(it ast.StructInit) {
struct_init_save := f.is_struct_init
f.is_struct_init = true
defer {
f.is_struct_init = struct_init_save
}
type_sym := f.table.get_type_symbol(it.typ) type_sym := f.table.get_type_symbol(it.typ)
// f.write('<old name: $type_sym.name>') // f.write('<old name: $type_sym.name>')
mut name := type_sym.name mut name := type_sym.name

View File

@ -1,3 +1,8 @@
struct Foo {
n int
s string
}
fn valid_single_line() { fn valid_single_line() {
// Variable initialization // Variable initialization
a, b := if true { 'a', 'b' } else { 'b', 'a' } a, b := if true { 'a', 'b' } else { 'b', 'a' }
@ -10,6 +15,11 @@ fn valid_single_line() {
_ := if false { Foo{} } else { Foo{5, 6} } _ := if false { Foo{} } else { Foo{5, 6} }
// As argument for a function call // As argument for a function call
some_func(if cond { 'param1' } else { 'param2' }) some_func(if cond { 'param1' } else { 'param2' })
// struct init
foo := Foo{
n: if true { 1 } else { 0 }
s: if false { 'false' } else { 'true' }
}
} }
fn requires_multiple_lines() { fn requires_multiple_lines() {