1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: intelligent line wrap for trailing struct syntax (#7309)

This commit is contained in:
Lukas Neubert 2020-12-14 05:03:17 +01:00 committed by GitHub
parent 591e523cf3
commit d59c5b01e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 173 deletions

View File

@ -121,18 +121,22 @@ fn gg_init_sokol_window(user_data voidptr) {
exists := $if !android { os.is_file(g.config.font_path) } $else { true } exists := $if !android { os.is_file(g.config.font_path) } $else { true }
if g.config.font_path != '' && exists { if g.config.font_path != '' && exists {
// t := time.ticks() // t := time.ticks()
g.ft = new_ft(font_path: g.config.font_path, custom_bold_font_path: g.config.custom_bold_font_path, scale: sapp.dpi_scale()) or { g.ft = new_ft(
panic(err) font_path: g.config.font_path
} custom_bold_font_path: g.config.custom_bold_font_path
scale: sapp.dpi_scale()
) or { panic(err) }
// println('FT took ${time.ticks()-t} ms') // println('FT took ${time.ticks()-t} ms')
g.font_inited = true g.font_inited = true
} else { } else {
if !exists { if !exists {
sfont := system_font_path() sfont := system_font_path()
eprintln('font file "$g.config.font_path" does not exist, the system font was used instead.') eprintln('font file "$g.config.font_path" does not exist, the system font was used instead.')
g.ft = new_ft(font_path: sfont, custom_bold_font_path: g.config.custom_bold_font_path, scale: sapp.dpi_scale()) or { g.ft = new_ft(
panic(err) font_path: sfont
} custom_bold_font_path: g.config.custom_bold_font_path
scale: sapp.dpi_scale()
) or { panic(err) }
g.font_inited = true g.font_inited = true
} }
} }

View File

@ -1858,15 +1858,17 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
} }
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent) f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
f.indent++ f.indent++
single_line_short_args := use_short_args && it.fields.len < 4 mut short_args_multiline := false
if use_short_args && !single_line_short_args { mut field_start_positions := []int{}
f.writeln('')
}
for i, field in it.fields { for i, field in it.fields {
field_start_positions << f.out.len
f.write('$field.name: ') f.write('$field.name: ')
f.prefix_expr_cast_expr(field.expr) f.prefix_expr_cast_expr(field.expr)
if field.expr is ast.StructInit {
short_args_multiline = true
}
f.comments(field.comments, inline: true, has_nl: false, level: .indent) f.comments(field.comments, inline: true, has_nl: false, level: .indent)
if single_line_short_args { if use_short_args {
if i < it.fields.len - 1 { if i < it.fields.len - 1 {
f.write(', ') f.write(', ')
} }
@ -1875,6 +1877,18 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
} }
f.comments(field.next_comments, inline: false, has_nl: true, level: .keep) f.comments(field.next_comments, inline: false, has_nl: true, level: .keep)
} }
if use_short_args {
if f.line_len > max_len[3] || short_args_multiline {
mut fields := []string{}
for pos in field_start_positions.reverse() {
fields << f.out.cut_last(f.out.len - pos).trim_suffix(', ')
}
f.writeln('')
for field in fields.reverse() {
f.writeln(field)
}
}
}
f.indent-- f.indent--
if !use_short_args { if !use_short_args {
f.write('}') f.write('}')

View File

@ -0,0 +1,26 @@
struct Bar {
x string
y int
b Baz
}
struct Baz {
x int
}
fn main() {
bar_func(x: 'this line is short enough', y: 13)
bar_func(
x: 'a very long content should cause vfmt to use multiple lines instead of one.'
y: 123456789
)
bar_func(
x: 'some string'
b: Baz{
x: 0
}
)
}
fn bar_func(bar Bar) {
}

View File

@ -0,0 +1,20 @@
struct Bar {
x string
y int
b Baz
}
struct Baz {
x int
}
fn main() {
bar_func(x: 'this line is short enough', y: 13)
bar_func(x: 'a very long content should cause vfmt to use multiple lines instead of one.', y: 123456789)
bar_func(x: 'some string', b: Baz{
x: 0
})
}
fn bar_func(bar Bar) {
}

View File

@ -16,12 +16,7 @@ fn main() {
foo_func(Baz{ foo_func(Baz{
x: 'Baz as Foo sumtype' x: 'Baz as Foo sumtype'
}) })
bar_func( bar_func(x: 'bar', y: 2, z: 3, a: 4)
x: 'bar'
y: 2
z: 3
a: 4
)
func_from_other_file(val: 'something') func_from_other_file(val: 'something')
} }

View File

@ -498,168 +498,38 @@ pub fn (mut t Table) register_builtin_type_symbols() {
// reserve index 0 so nothing can go there // reserve index 0 so nothing can go there
// save index check, 0 will mean not found // save index check, 0 will mean not found
t.register_type_symbol(kind: .placeholder, name: 'reserved_0') t.register_type_symbol(kind: .placeholder, name: 'reserved_0')
t.register_type_symbol( t.register_type_symbol(kind: .void, name: 'void', cname: 'void', mod: 'builtin')
kind: .void t.register_type_symbol(kind: .voidptr, name: 'voidptr', cname: 'voidptr', mod: 'builtin')
name: 'void' t.register_type_symbol(kind: .byteptr, name: 'byteptr', cname: 'byteptr', mod: 'builtin')
cname: 'void' t.register_type_symbol(kind: .charptr, name: 'charptr', cname: 'charptr', mod: 'builtin')
mod: 'builtin' t.register_type_symbol(kind: .i8, name: 'i8', cname: 'i8', mod: 'builtin')
) t.register_type_symbol(kind: .i16, name: 'i16', cname: 'i16', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(kind: .int, name: 'int', cname: 'int', mod: 'builtin')
kind: .voidptr t.register_type_symbol(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin')
name: 'voidptr' t.register_type_symbol(kind: .byte, name: 'byte', cname: 'byte', mod: 'builtin')
cname: 'voidptr' t.register_type_symbol(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin')
mod: 'builtin' t.register_type_symbol(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin')
) t.register_type_symbol(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(kind: .f32, name: 'f32', cname: 'f32', mod: 'builtin')
kind: .byteptr t.register_type_symbol(kind: .f64, name: 'f64', cname: 'f64', mod: 'builtin')
name: 'byteptr' t.register_type_symbol(kind: .char, name: 'char', cname: 'char', mod: 'builtin')
cname: 'byteptr' t.register_type_symbol(kind: .bool, name: 'bool', cname: 'bool', mod: 'builtin')
mod: 'builtin' t.register_type_symbol(kind: .none_, name: 'none', cname: 'none', mod: 'builtin')
) t.register_type_symbol(kind: .string, name: 'string', cname: 'string', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(kind: .ustring, name: 'ustring', cname: 'ustring', mod: 'builtin')
kind: .charptr t.register_type_symbol(kind: .array, name: 'array', cname: 'array', mod: 'builtin')
name: 'charptr' t.register_type_symbol(kind: .map, name: 'map', cname: 'map', mod: 'builtin')
cname: 'charptr' t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin')
mod: 'builtin' t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin')
) t.register_type_symbol(kind: .rune, name: 'rune', cname: 'rune', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin')
kind: .i8
name: 'i8'
cname: 'i8'
mod: 'builtin'
)
t.register_type_symbol(
kind: .i16
name: 'i16'
cname: 'i16'
mod: 'builtin'
)
t.register_type_symbol(
kind: .int
name: 'int'
cname: 'int'
mod: 'builtin'
)
t.register_type_symbol(
kind: .i64
name: 'i64'
cname: 'i64'
mod: 'builtin'
)
t.register_type_symbol(
kind: .byte
name: 'byte'
cname: 'byte'
mod: 'builtin'
)
t.register_type_symbol(
kind: .u16
name: 'u16'
cname: 'u16'
mod: 'builtin'
)
t.register_type_symbol(
kind: .u32
name: 'u32'
cname: 'u32'
mod: 'builtin'
)
t.register_type_symbol(
kind: .u64
name: 'u64'
cname: 'u64'
mod: 'builtin'
)
t.register_type_symbol(
kind: .f32
name: 'f32'
cname: 'f32'
mod: 'builtin'
)
t.register_type_symbol(
kind: .f64
name: 'f64'
cname: 'f64'
mod: 'builtin'
)
t.register_type_symbol(
kind: .char
name: 'char'
cname: 'char'
mod: 'builtin'
)
t.register_type_symbol(
kind: .bool
name: 'bool'
cname: 'bool'
mod: 'builtin'
)
t.register_type_symbol(
kind: .none_
name: 'none'
cname: 'none'
mod: 'builtin'
)
t.register_type_symbol(
kind: .string
name: 'string'
cname: 'string'
mod: 'builtin'
)
t.register_type_symbol(
kind: .ustring
name: 'ustring'
cname: 'ustring'
mod: 'builtin'
)
t.register_type_symbol(
kind: .array
name: 'array'
cname: 'array'
mod: 'builtin'
)
t.register_type_symbol(
kind: .map
name: 'map'
cname: 'map'
mod: 'builtin'
)
t.register_type_symbol(
kind: .chan
name: 'chan'
cname: 'chan'
mod: 'builtin'
)
t.register_type_symbol(
kind: .size_t
name: 'size_t'
cname: 'size_t'
mod: 'builtin'
)
t.register_type_symbol(
kind: .rune
name: 'rune'
cname: 'rune'
mod: 'builtin'
)
t.register_type_symbol(
kind: .any
name: 'any'
cname: 'any'
mod: 'builtin'
)
t.register_type_symbol( t.register_type_symbol(
kind: .any_float kind: .any_float
name: 'any_float' name: 'any_float'
cname: 'any_float' cname: 'any_float'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(kind: .any_int, name: 'any_int', cname: 'any_int', mod: 'builtin')
kind: .any_int
name: 'any_int'
cname: 'any_int'
mod: 'builtin'
)
} }
[inline] [inline]