Skip to content

Commit 1b572f7

Browse files
author
Lukas Neubert
authored
fmt: minor array wrap improvement (#9420)
1 parent 801da20 commit 1b572f7

File tree

12 files changed

+98
-56
lines changed

12 files changed

+98
-56
lines changed

vlib/flag/flag_test.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ fn test_allow_to_build_usage_message() {
167167
usage := fp.usage()
168168
mut all_strings_found := true
169169
for s in ['flag_tool', 'v0.0.0', 'an_int <int>', 'a_bool', 'bool_without', 'a_float <float>',
170-
'a_string <string>', 'some int to define', 'some bool to define', 'this should appear on the next line',
171-
'some float as well', 'your credit card number', 'The arguments should be at least 1 and at most 4 in number.',
172-
'Usage', 'Options:', 'Description:', 'some short information about this tool'] {
170+
'a_string <string>', 'some int to define', 'some bool to define',
171+
'this should appear on the next line', 'some float as well', 'your credit card number',
172+
'The arguments should be at least 1 and at most 4 in number.', 'Usage', 'Options:',
173+
'Description:', 'some short information about this tool'] {
173174
if !usage.contains(s) {
174175
eprintln(" missing '$s' in usage message")
175176
all_strings_found = false

vlib/time/time.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub const (
3535
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
3636
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
3737
]
38-
long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
38+
long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
39+
'Sunday',
40+
]
3941
)
4042

4143
// Time contains various time units for a point in time.

vlib/v/builder/cc.v

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) {
196196
ccoptions.args = [v.pref.cflags, '-std=gnu99']
197197
ccoptions.wargs = ['-Wall', '-Wextra', '-Wno-unused', '-Wno-missing-braces', '-Walloc-zero',
198198
'-Wcast-qual', '-Wdate-time', '-Wduplicated-branches', '-Wduplicated-cond', '-Wformat=2',
199-
'-Winit-self', '-Winvalid-pch', '-Wjump-misses-init', '-Wlogical-op', '-Wmultichar', '-Wnested-externs',
200-
'-Wnull-dereference', '-Wpacked', '-Wpointer-arith', '-Wshadow', '-Wswitch-default', '-Wswitch-enum',
201-
'-Wno-unused-parameter', '-Wno-unknown-warning-option', '-Wno-format-nonliteral']
199+
'-Winit-self', '-Winvalid-pch', '-Wjump-misses-init', '-Wlogical-op', '-Wmultichar',
200+
'-Wnested-externs', '-Wnull-dereference', '-Wpacked', '-Wpointer-arith', '-Wshadow',
201+
'-Wswitch-default', '-Wswitch-enum', '-Wno-unused-parameter', '-Wno-unknown-warning-option',
202+
'-Wno-format-nonliteral',
203+
]
202204
if v.pref.os == .ios {
203205
ccoptions.args << '-framework Foundation'
204206
ccoptions.args << '-framework UIKit'
@@ -381,7 +383,9 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) {
381383
}
382384
v.ccoptions = ccoptions
383385
// setup the cache too, so that different compilers/options do not interfere:
384-
v.pref.cache_manager.set_temporary_options(ccoptions.thirdparty_object_args([ccoptions.guessed_compiler]))
386+
v.pref.cache_manager.set_temporary_options(ccoptions.thirdparty_object_args([
387+
ccoptions.guessed_compiler,
388+
]))
385389
}
386390

387391
fn (ccoptions CcompilerOptions) all_args() []string {
@@ -777,9 +781,12 @@ fn (mut b Builder) cc_linux_cross() {
777781
verror(cc_res.output)
778782
return
779783
}
780-
mut linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot', '-v', '-o $b.pref.out_name',
781-
'-m elf_x86_64', '-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2', '$sysroot/crt1.o $sysroot/crti.o $obj_file',
782-
'-lc', '-lcrypto', '-lssl', '-lpthread', '$sysroot/crtn.o']
784+
mut linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot', '-v',
785+
'-o $b.pref.out_name', '-m elf_x86_64',
786+
'-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2',
787+
'$sysroot/crt1.o $sysroot/crti.o $obj_file', '-lc', '-lcrypto', '-lssl', '-lpthread',
788+
'$sysroot/crtn.o',
789+
]
783790
linker_args << cflags.c_options_only_object_files()
784791
// -ldl
785792
b.dump_c_options(linker_args)

vlib/v/checker/checker.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const (
2525
]
2626
valid_comp_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
2727
valid_comp_if_platforms = ['amd64', 'aarch64', 'x64', 'x32', 'little_endian', 'big_endian']
28-
valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking']
28+
valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc',
29+
'no_bounds_checking',
30+
]
2931
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort',
3032
'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
3133
)

vlib/v/fmt/fmt.v

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ pub fn (f Fmt) imp_stmt_str(imp ast.Import) string {
293293
return '$imp.mod$imp_alias_suffix'
294294
}
295295

296+
//=== Node helpers ===//
297+
296298
fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node) bool {
297299
// No need to insert a newline if there is already one
298300
if f.out.last_n(2) == '\n\n' {
@@ -345,6 +347,22 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
345347
return true
346348
}
347349

350+
pub fn (mut f Fmt) node_str(node ast.Node) string {
351+
was_empty_line := f.empty_line
352+
prev_line_len := f.line_len
353+
pos := f.out.len
354+
match node {
355+
ast.Stmt { f.stmt(node) }
356+
ast.Expr { f.expr(node) }
357+
else { panic('´f.node_str()´ is not implemented for ${node}.') }
358+
}
359+
str := f.out.after(pos).trim_space()
360+
f.out.go_back_to(pos)
361+
f.empty_line = was_empty_line
362+
f.line_len = prev_line_len
363+
return str
364+
}
365+
348366
//=== General Stmt-related methods and helpers ===//
349367

350368
pub fn (mut f Fmt) stmts(stmts []ast.Stmt) {
@@ -456,18 +474,6 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
456474
}
457475
}
458476

459-
pub fn (mut f Fmt) stmt_str(node ast.Stmt) string {
460-
was_empty_line := f.empty_line
461-
prev_line_len := f.line_len
462-
pos := f.out.len
463-
f.stmt(node)
464-
str := f.out.after(pos).trim_space()
465-
f.out.go_back_to(pos)
466-
f.empty_line = was_empty_line
467-
f.line_len = prev_line_len
468-
return str
469-
}
470-
471477
//=== General Expr-related methods and helpers ===//
472478

473479
pub fn (mut f Fmt) expr(node ast.Expr) {
@@ -622,20 +628,14 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
622628

623629
fn expr_is_single_line(expr ast.Expr) bool {
624630
match expr {
631+
ast.Comment, ast.IfExpr, ast.MapInit, ast.MatchExpr {
632+
return false
633+
}
625634
ast.AnonFn {
626635
if !expr.decl.no_body {
627636
return false
628637
}
629638
}
630-
ast.IfExpr {
631-
return false
632-
}
633-
ast.Comment {
634-
return false
635-
}
636-
ast.MatchExpr {
637-
return false
638-
}
639639
ast.StructInit {
640640
if !expr.is_short && (expr.fields.len > 0 || expr.pre_comments.len > 0) {
641641
return false
@@ -1399,15 +1399,32 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
13991399
penalty--
14001400
}
14011401
}
1402-
is_new_line := f.wrap_long_line(penalty, !inc_indent)
1402+
mut is_new_line := f.wrap_long_line(penalty, !inc_indent)
14031403
if is_new_line && !inc_indent {
14041404
f.indent++
14051405
inc_indent = true
14061406
}
1407-
if !is_new_line && i > 0 {
1408-
f.write(' ')
1407+
single_line_expr := expr_is_single_line(expr)
1408+
if single_line_expr {
1409+
estr := f.node_str(expr)
1410+
if !is_new_line && !f.buffering && f.line_len + estr.len > fmt.max_len.last() {
1411+
f.writeln('')
1412+
is_new_line = true
1413+
if !inc_indent {
1414+
f.indent++
1415+
inc_indent = true
1416+
}
1417+
}
1418+
if !is_new_line && i > 0 {
1419+
f.write(' ')
1420+
}
1421+
f.write(estr)
1422+
} else {
1423+
if !is_new_line && i > 0 {
1424+
f.write(' ')
1425+
}
1426+
f.expr(expr)
14091427
}
1410-
f.expr(expr)
14111428
if i < node.ecmnts.len && node.ecmnts[i].len > 0 {
14121429
expr_pos := expr.position()
14131430
for cmt in node.ecmnts[i] {
@@ -2089,7 +2106,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
20892106
} else if node.stmts.len == 1 && stmt_is_single_line(node.stmts[0]) {
20902107
// the control stmts (return/break/continue...) print a newline inside them,
20912108
// so, since this'll all be on one line, trim any possible whitespace
2092-
str := f.stmt_str(node.stmts[0]).trim_space()
2109+
str := f.node_str(node.stmts[0]).trim_space()
20932110
single_line := ' or { $str }'
20942111
if single_line.len + f.line_len <= fmt.max_len.last() {
20952112
f.write(single_line)

vlib/v/fmt/tests/array_init_expected.vv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ fn main() {
66
println("oh no :'(")
77
}
88
}
9+
10+
fn wrapping_tests() {
11+
my_arr := ['Lorem ipsum dolor sit amet, consectetur adipiscing',
12+
'elit. Donec varius purus leo, vel maximus diam',
13+
'finibus sed. Etiam eu urna ante. Nunc quis vehicula',
14+
'velit. Sed at mauris et quam ornare tristique.',
15+
]
16+
}

vlib/v/fmt/tests/array_init_input.vv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ fn main() {
66
println("oh no :'(")
77
}
88
}
9+
10+
fn wrapping_tests() {
11+
my_arr := ['Lorem ipsum dolor sit amet, consectetur adipiscing', 'elit. Donec varius purus leo, vel maximus diam', 'finibus sed. Etiam eu urna ante. Nunc quis vehicula', 'velit. Sed at mauris et quam ornare tristique.']
12+
}

vlib/v/gen/c/auto_str_methods.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ fn (g &Gen) type_to_fmt(typ table.Type) string {
4444
sym := g.table.get_type_symbol(typ)
4545
if typ.is_ptr() && (typ.is_int() || typ.is_float()) {
4646
return '%.*s\\000'
47-
} else if sym.kind in [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, .sum_type,
48-
.function, .alias] {
47+
} else if sym.kind in [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_,
48+
.sum_type, .function, .alias] {
4949
return '%.*s\\000'
5050
} else if sym.kind == .string {
5151
return "'%.*s\\000'"

vlib/v/live/live_test.v

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ fn testsuite_begin() {
7272
eprintln('You can still do it by setting FORCE_LIVE_TEST=1 .')
7373
exit(0)
7474
}
75-
for f in [tmp_file, source_file, output_file, res_original_file, res_changed_file, res_another_file,
76-
res_stop_file,
77-
] {
75+
for f in [tmp_file, source_file, output_file, res_original_file, res_changed_file,
76+
res_another_file, res_stop_file] {
7877
os.rm(f) or {}
7978
}
8079
atomic_write_source(live_program_source)

vlib/v/parser/v_parser_test.v

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ fn test_parse_expr() {
130130
'bo := 2 + 3 == 5', '2 + 1', 'q := 1', 'q + 777', '2 + 3', '2+2*4', 'x := 10', 'mut aa := 12',
131131
'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4', '1 + 2 * 5',
132132
'-a+1', '2+2']
133-
expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;', 'int neg = -a;',
134-
'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;', '2 + 3;', '2 + 2 * 4;',
135-
'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");', 'x = 11;',
136-
'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;']
133+
expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;',
134+
'int neg = -a;', 'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;',
135+
'2 + 3;', '2 + 2 * 4;', 'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;',
136+
'string s = tos3("hi");', 'x = 11;', 'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;',
137+
'-a + 1;', '2 + 2;']
137138
mut e := []ast.Stmt{}
138139
table := table.new_table()
139140
vpref := &pref.Preferences{}

0 commit comments

Comments
 (0)