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

fmt: better ternary concat wrapping (#8379)

This commit is contained in:
Lukas Neubert 2021-01-28 11:23:48 +01:00 committed by GitHub
parent d86cb67eda
commit 75d85403a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 48 additions and 24 deletions

View File

@ -353,8 +353,7 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
if start_offset != 0 {
for i in 0 .. output_slots - 1 {
output.field[i] = u32(input.field[start_slot + i] >> u32(start_offset))
output.field[i] = output.field[i] | u32(input.field[start_slot + i +
1] << u32(bitfield.slot_size - start_offset))
output.field[i] = output.field[i] | u32(input.field[start_slot + i + 1] << u32(bitfield.slot_size - start_offset))
}
} else {
for i in 0 .. output_slots - 1 {

View File

@ -387,16 +387,22 @@ fn test_mutli_array_clone() {
mut a3_2 := a3_1.clone()
a3_1[0][0][1] = 0
a3_2[0][1][0] = 0
assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5],
[6, 6],
]]
assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5],
[6, 6],
]]
// 3d array_string
mut b3_1 := [[['1', '1'], ['2', '2'], ['3', '3']], [['4', '4'],
['5', '5'], ['6', '6']]]
mut b3_2 := b3_1.clone()
b3_1[0][0][1] = '0'
b3_2[0][1][0] = '0'
assert b3_1 == [[['1', '0'], ['2', '2'], ['3', '3']], [['4', '4'], ['5', '5'], ['6', '6']]]
assert b3_2 == [[['1', '1'], ['0', '2'], ['3', '3']], [['4', '4'], ['5', '5'], ['6', '6']]]
assert b3_1 == [[['1', '0'], ['2', '2'], ['3', '3']], [['4', '4'],
['5', '5'], ['6', '6']]]
assert b3_2 == [[['1', '1'], ['0', '2'], ['3', '3']], [['4', '4'],
['5', '5'], ['6', '6']]]
}
fn test_doubling() {

View File

@ -87,12 +87,14 @@ fn test_format_ss() {
}
fn test_format_ss_milli() {
assert '11.07.1980 21:23:42.123' == time_to_test.get_fmt_str(.dot, .hhmmss24_milli, .ddmmyyyy)
assert '11.07.1980 21:23:42.123' == time_to_test.get_fmt_str(.dot, .hhmmss24_milli,
.ddmmyyyy)
assert '1980-07-11 21:23:42.123' == time_to_test.format_ss_milli()
}
fn test_format_ss_micro() {
assert '11.07.1980 21:23:42.123456' == time_to_test.get_fmt_str(.dot, .hhmmss24_micro, .ddmmyyyy)
assert '11.07.1980 21:23:42.123456' == time_to_test.get_fmt_str(.dot, .hhmmss24_micro,
.ddmmyyyy)
assert '1980-07-11 21:23:42.123456' == time_to_test.format_ss_micro()
}

View File

@ -188,9 +188,11 @@ pub fn (v Builder) get_builtin_files() []string {
if os.exists(os.join_path(location, 'builtin')) {
mut builtin_files := []string{}
if v.pref.is_bare {
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin', 'bare'))
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin',
'bare'))
} else if v.pref.backend == .js {
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin', 'js'))
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin',
'js'))
} else {
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin'))
}

View File

@ -2409,7 +2409,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
if assign_stmt.right_types.len < assign_stmt.left.len { // first type or multi return types added above
right_type := c.expr(assign_stmt.right[i])
if assign_stmt.right_types.len == i {
assign_stmt.right_types << c.check_expr_opt_call(assign_stmt.right[i], right_type)
assign_stmt.right_types << c.check_expr_opt_call(assign_stmt.right[i],
right_type)
}
}
right := if i < assign_stmt.right.len { assign_stmt.right[i] } else { assign_stmt.right[0] }

View File

@ -695,8 +695,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
comment_align_i++
align = comment_aligns[comment_align_i]
}
pad_len := align.max_attrs_len - attrs_len +
align.max_type_len - field_types[i].len
pad_len := align.max_attrs_len - attrs_len + align.max_type_len - field_types[i].len
f.write(strings.repeat(` `, pad_len))
}
f.write(' ')
@ -1449,7 +1448,7 @@ pub fn (mut f Fmt) lock_expr(lex ast.LockExpr) {
pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
buffering_save := f.buffering
if !f.buffering {
if !f.buffering && node.op in [.logical_or, .and, .plus] {
f.buffering = true
}
if node.op == .left_shift {
@ -1587,8 +1586,9 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
f.write(' ')
}
}
// When a single line if is really long, write it again as multiline
if single_line && f.line_len > fmt.max_len.last() {
// When a single line if is really long, write it again as multiline,
// except it is part of an InfixExpr.
if single_line && f.line_len > fmt.max_len.last() && !f.buffering {
single_line = false
f.single_line_if = false
f.out.go_back(f.line_len - if_start)

View File

@ -15,3 +15,10 @@ fn concat_inside_ternary() {
}
}
}
fn concat_two_or_more_ternaries() {
x := if some_condition { 'very long string part' } else { 'def' } +
if y == 'asd' { 'qwe' } else { 'something else' }
var := if other_condition { 'concat three long ternary ifs' } else { 'def' } +
if true { 'shorter' } else { 'quite short' } + if true { 'small' } else { 'tiny' }
}

View File

@ -15,3 +15,8 @@ fn concat_inside_ternary() {
}
}
}
fn concat_two_or_more_ternaries() {
x := if some_condition { 'very long string part' } else { 'def' } + if y == 'asd' { 'qwe' } else {'something else'}
var := if other_condition { 'concat three long ternary ifs' } else { 'def' } + if true {'shorter'} else {'quite short'} + if true { 'small' } else {'tiny'}
}

View File

@ -40,7 +40,9 @@ fn (g &Gen) type_to_fmt(typ table.Type) string {
sym := g.table.get_type_symbol(typ)
if typ.is_ptr() && (typ.is_int() || typ.is_float()) {
return '%.*s\\000'
} else if sym.kind in [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, .sum_type, .function] {
} else if sym.kind in [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, .sum_type,
.function,
] {
return '%.*s\\000'
} else if sym.kind == .string {
return "'%.*s\\000'"

View File

@ -202,7 +202,9 @@ fn js_dec_name(typ string) string {
}
fn is_js_prim(typ string) bool {
return typ in ['int', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i64', 'u16', 'u32', 'u64', 'byte']
return typ in ['int', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i64', 'u16', 'u32', 'u64',
'byte',
]
}
fn (mut g Gen) decode_array(value_type table.Type) string {

View File

@ -12,7 +12,8 @@ pub fn find_working_diff_command() ?string {
if env_difftool.len > 0 {
known_diff_tools << env_difftool
}
known_diff_tools << ['colordiff', 'gdiff', 'diff', 'colordiff.exe', 'diff.exe', 'opendiff', 'code', 'code.cmd']
known_diff_tools << ['colordiff', 'gdiff', 'diff', 'colordiff.exe', 'diff.exe', 'opendiff',
'code', 'code.cmd']
// NOTE: code.cmd is the Windows variant of the `code` cli tool
for diffcmd in known_diff_tools {
if diffcmd == 'opendiff' { // opendiff has no `--version` option

View File

@ -232,11 +232,8 @@ pub fn (mut ws Client) write_ptr(bytes byteptr, payload_len int, code OPCode) ?
// todo: send error here later
return error('trying to write on a closed socket!')
}
mut header_len := 2 + if payload_len > 125 { 2 } else { 0 } + if payload_len > 0xffff {
6
} else {
0
}
mut header_len := 2 + if payload_len > 125 { 2 } else { 0 } +
if payload_len > 0xffff { 6 } else { 0 }
if !ws.is_server {
header_len += 4
}