From 1b572f75e81bbd266b0b9a7ad95db412a9a76a97 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 22 Mar 2021 23:06:12 +0100 Subject: [PATCH] fmt: minor array wrap improvement (#9420) --- vlib/flag/flag_test.v | 7 +-- vlib/time/time.v | 4 +- vlib/v/builder/cc.v | 21 +++++--- vlib/v/checker/checker.v | 4 +- vlib/v/fmt/fmt.v | 69 +++++++++++++++---------- vlib/v/fmt/tests/array_init_expected.vv | 8 +++ vlib/v/fmt/tests/array_init_input.vv | 4 ++ vlib/v/gen/c/auto_str_methods.v | 4 +- vlib/v/live/live_test.v | 5 +- vlib/v/parser/v_parser_test.v | 9 ++-- vlib/v/pkgconfig/pkgconfig_test.v | 9 ++-- vlib/v/table/types.v | 10 ++-- 12 files changed, 98 insertions(+), 56 deletions(-) diff --git a/vlib/flag/flag_test.v b/vlib/flag/flag_test.v index c875989869..04338930d5 100644 --- a/vlib/flag/flag_test.v +++ b/vlib/flag/flag_test.v @@ -167,9 +167,10 @@ fn test_allow_to_build_usage_message() { usage := fp.usage() mut all_strings_found := true for s in ['flag_tool', 'v0.0.0', 'an_int ', 'a_bool', 'bool_without', 'a_float ', - 'a_string ', 'some int to define', 'some bool to define', 'this should appear on the next line', - 'some float as well', 'your credit card number', 'The arguments should be at least 1 and at most 4 in number.', - 'Usage', 'Options:', 'Description:', 'some short information about this tool'] { + 'a_string ', 'some int to define', 'some bool to define', + 'this should appear on the next line', 'some float as well', 'your credit card number', + 'The arguments should be at least 1 and at most 4 in number.', 'Usage', 'Options:', + 'Description:', 'some short information about this tool'] { if !usage.contains(s) { eprintln(" missing '$s' in usage message") all_strings_found = false diff --git a/vlib/time/time.v b/vlib/time/time.v index b79c9a695e..2861a5a184 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -35,7 +35,9 @@ pub const ( 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31, ] - long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] + long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', + 'Sunday', + ] ) // Time contains various time units for a point in time. diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 4c0dccba68..198f6b4854 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -196,9 +196,11 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) { ccoptions.args = [v.pref.cflags, '-std=gnu99'] ccoptions.wargs = ['-Wall', '-Wextra', '-Wno-unused', '-Wno-missing-braces', '-Walloc-zero', '-Wcast-qual', '-Wdate-time', '-Wduplicated-branches', '-Wduplicated-cond', '-Wformat=2', - '-Winit-self', '-Winvalid-pch', '-Wjump-misses-init', '-Wlogical-op', '-Wmultichar', '-Wnested-externs', - '-Wnull-dereference', '-Wpacked', '-Wpointer-arith', '-Wshadow', '-Wswitch-default', '-Wswitch-enum', - '-Wno-unused-parameter', '-Wno-unknown-warning-option', '-Wno-format-nonliteral'] + '-Winit-self', '-Winvalid-pch', '-Wjump-misses-init', '-Wlogical-op', '-Wmultichar', + '-Wnested-externs', '-Wnull-dereference', '-Wpacked', '-Wpointer-arith', '-Wshadow', + '-Wswitch-default', '-Wswitch-enum', '-Wno-unused-parameter', '-Wno-unknown-warning-option', + '-Wno-format-nonliteral', + ] if v.pref.os == .ios { ccoptions.args << '-framework Foundation' ccoptions.args << '-framework UIKit' @@ -381,7 +383,9 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) { } v.ccoptions = ccoptions // setup the cache too, so that different compilers/options do not interfere: - v.pref.cache_manager.set_temporary_options(ccoptions.thirdparty_object_args([ccoptions.guessed_compiler])) + v.pref.cache_manager.set_temporary_options(ccoptions.thirdparty_object_args([ + ccoptions.guessed_compiler, + ])) } fn (ccoptions CcompilerOptions) all_args() []string { @@ -777,9 +781,12 @@ fn (mut b Builder) cc_linux_cross() { verror(cc_res.output) return } - mut linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot', '-v', '-o $b.pref.out_name', - '-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', - '-lc', '-lcrypto', '-lssl', '-lpthread', '$sysroot/crtn.o'] + mut linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot', '-v', + '-o $b.pref.out_name', '-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', '-lc', '-lcrypto', '-lssl', '-lpthread', + '$sysroot/crtn.o', + ] linker_args << cflags.c_options_only_object_files() // -ldl b.dump_c_options(linker_args) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 56ab0c3607..9dd63eb759 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -25,7 +25,9 @@ const ( ] valid_comp_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus'] valid_comp_if_platforms = ['amd64', 'aarch64', 'x64', 'x32', 'little_endian', 'big_endian'] - valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking'] + valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', + 'no_bounds_checking', + ] array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort', 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop'] ) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 3e9f818eec..5390bff3f5 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -293,6 +293,8 @@ pub fn (f Fmt) imp_stmt_str(imp ast.Import) string { return '$imp.mod$imp_alias_suffix' } +//=== Node helpers ===// + fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node) bool { // No need to insert a newline if there is already one 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) return true } +pub fn (mut f Fmt) node_str(node ast.Node) string { + was_empty_line := f.empty_line + prev_line_len := f.line_len + pos := f.out.len + match node { + ast.Stmt { f.stmt(node) } + ast.Expr { f.expr(node) } + else { panic('´f.node_str()´ is not implemented for ${node}.') } + } + str := f.out.after(pos).trim_space() + f.out.go_back_to(pos) + f.empty_line = was_empty_line + f.line_len = prev_line_len + return str +} + //=== General Stmt-related methods and helpers ===// pub fn (mut f Fmt) stmts(stmts []ast.Stmt) { @@ -456,18 +474,6 @@ fn stmt_is_single_line(stmt ast.Stmt) bool { } } -pub fn (mut f Fmt) stmt_str(node ast.Stmt) string { - was_empty_line := f.empty_line - prev_line_len := f.line_len - pos := f.out.len - f.stmt(node) - str := f.out.after(pos).trim_space() - f.out.go_back_to(pos) - f.empty_line = was_empty_line - f.line_len = prev_line_len - return str -} - //=== General Expr-related methods and helpers ===// pub fn (mut f Fmt) expr(node ast.Expr) { @@ -622,20 +628,14 @@ pub fn (mut f Fmt) expr(node ast.Expr) { fn expr_is_single_line(expr ast.Expr) bool { match expr { + ast.Comment, ast.IfExpr, ast.MapInit, ast.MatchExpr { + return false + } ast.AnonFn { if !expr.decl.no_body { return false } } - ast.IfExpr { - return false - } - ast.Comment { - return false - } - ast.MatchExpr { - return false - } ast.StructInit { if !expr.is_short && (expr.fields.len > 0 || expr.pre_comments.len > 0) { return false @@ -1399,15 +1399,32 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { penalty-- } } - is_new_line := f.wrap_long_line(penalty, !inc_indent) + mut is_new_line := f.wrap_long_line(penalty, !inc_indent) if is_new_line && !inc_indent { f.indent++ inc_indent = true } - if !is_new_line && i > 0 { - f.write(' ') + single_line_expr := expr_is_single_line(expr) + if single_line_expr { + estr := f.node_str(expr) + if !is_new_line && !f.buffering && f.line_len + estr.len > fmt.max_len.last() { + f.writeln('') + is_new_line = true + if !inc_indent { + f.indent++ + inc_indent = true + } + } + if !is_new_line && i > 0 { + f.write(' ') + } + f.write(estr) + } else { + if !is_new_line && i > 0 { + f.write(' ') + } + f.expr(expr) } - f.expr(expr) if i < node.ecmnts.len && node.ecmnts[i].len > 0 { expr_pos := expr.position() for cmt in node.ecmnts[i] { @@ -2089,7 +2106,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) { } else if node.stmts.len == 1 && stmt_is_single_line(node.stmts[0]) { // the control stmts (return/break/continue...) print a newline inside them, // so, since this'll all be on one line, trim any possible whitespace - str := f.stmt_str(node.stmts[0]).trim_space() + str := f.node_str(node.stmts[0]).trim_space() single_line := ' or { $str }' if single_line.len + f.line_len <= fmt.max_len.last() { f.write(single_line) diff --git a/vlib/v/fmt/tests/array_init_expected.vv b/vlib/v/fmt/tests/array_init_expected.vv index 17d53276a1..e799c688e7 100644 --- a/vlib/v/fmt/tests/array_init_expected.vv +++ b/vlib/v/fmt/tests/array_init_expected.vv @@ -6,3 +6,11 @@ fn main() { println("oh no :'(") } } + +fn wrapping_tests() { + 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.', + ] +} diff --git a/vlib/v/fmt/tests/array_init_input.vv b/vlib/v/fmt/tests/array_init_input.vv index f56eba5cc6..4c453f8dfb 100644 --- a/vlib/v/fmt/tests/array_init_input.vv +++ b/vlib/v/fmt/tests/array_init_input.vv @@ -6,3 +6,7 @@ fn main() { println("oh no :'(") } } + +fn wrapping_tests() { + 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.'] +} diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 99684a45ee..fa49e23317 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -44,8 +44,8 @@ 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, .alias] { + } else if sym.kind in [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, + .sum_type, .function, .alias] { return '%.*s\\000' } else if sym.kind == .string { return "'%.*s\\000'" diff --git a/vlib/v/live/live_test.v b/vlib/v/live/live_test.v index 55104231de..2468075ca6 100644 --- a/vlib/v/live/live_test.v +++ b/vlib/v/live/live_test.v @@ -72,9 +72,8 @@ fn testsuite_begin() { eprintln('You can still do it by setting FORCE_LIVE_TEST=1 .') exit(0) } - for f in [tmp_file, source_file, output_file, res_original_file, res_changed_file, res_another_file, - res_stop_file, - ] { + for f in [tmp_file, source_file, output_file, res_original_file, res_changed_file, + res_another_file, res_stop_file] { os.rm(f) or {} } atomic_write_source(live_program_source) diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index 72ffc93eb7..85af6e2186 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -130,10 +130,11 @@ fn test_parse_expr() { 'bo := 2 + 3 == 5', '2 + 1', 'q := 1', 'q + 777', '2 + 3', '2+2*4', 'x := 10', 'mut aa := 12', 'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4', '1 + 2 * 5', '-a+1', '2+2'] - expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;', 'int neg = -a;', - 'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;', '2 + 3;', '2 + 2 * 4;', - 'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");', 'x = 11;', - 'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;'] + expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;', + 'int neg = -a;', 'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;', + '2 + 3;', '2 + 2 * 4;', 'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', + 'string s = tos3("hi");', 'x = 11;', 'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', + '-a + 1;', '2 + 2;'] mut e := []ast.Stmt{} table := table.new_table() vpref := &pref.Preferences{} diff --git a/vlib/v/pkgconfig/pkgconfig_test.v b/vlib/v/pkgconfig/pkgconfig_test.v index 2747397c4f..f316ce19df 100644 --- a/vlib/v/pkgconfig/pkgconfig_test.v +++ b/vlib/v/pkgconfig/pkgconfig_test.v @@ -52,7 +52,9 @@ fn test_samples() { assert x.libs == ['-Wl,--export-dynamic', '-L/usr/lib/x86_64-linux-gnu', '-lgmodule-2.0', '-pthread', '-lglib-2.0', '-lpcre'] assert x.libs_private == ['-ldl', '-pthread'] - assert x.cflags == ['-I/usr/include', '-pthread', '-I/usr/include/glib-2.0', '-I/usr/lib/x86_64-linux-gnu/glib-2.0/include'] + assert x.cflags == ['-I/usr/include', '-pthread', '-I/usr/include/glib-2.0', + '-I/usr/lib/x86_64-linux-gnu/glib-2.0/include', + ] assert x.vars == map{ 'prefix': '/usr' 'libdir': '/usr/lib/x86_64-linux-gnu' @@ -70,9 +72,8 @@ fn test_samples() { assert x.modname == 'glib-2.0' assert x.libs == ['-L/usr/lib/x86_64-linux-gnu', '-lglib-2.0', '-lpcre'] assert x.libs_private == ['-pthread'] - assert x.cflags == ['-I/usr/include/glib-2.0', '-I/usr/lib/x86_64-linux-gnu/glib-2.0/include', - '-I/usr/include', - ] + assert x.cflags == ['-I/usr/include/glib-2.0', + '-I/usr/lib/x86_64-linux-gnu/glib-2.0/include', '-I/usr/include'] assert x.vars == map{ 'prefix': '/usr' 'libdir': '/usr/lib/x86_64-linux-gnu' diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index 093d08c621..ba322b9dd8 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -344,14 +344,14 @@ pub const ( ) pub const ( - integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx, - u16_type_idx, u32_type_idx, u64_type_idx, int_literal_type_idx, rune_type_idx] + integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, + byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, int_literal_type_idx, rune_type_idx] signed_integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx] unsigned_integer_type_idxs = [byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx] float_type_idxs = [f32_type_idx, f64_type_idx, float_literal_type_idx] - number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx, - u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx, int_literal_type_idx, - float_literal_type_idx, rune_type_idx] + number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, + byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx, + int_literal_type_idx, float_literal_type_idx, rune_type_idx] pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx] string_type_idxs = [string_type_idx, ustring_type_idx] )