fmt: minor array wrap improvement (#9420)

pull/9429/head
Lukas Neubert 2021-03-22 23:06:12 +01:00 committed by GitHub
parent 801da20fd9
commit 1b572f75e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 98 additions and 56 deletions

View File

@ -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 <int>', 'a_bool', 'bool_without', 'a_float <float>',
'a_string <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 <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

View File

@ -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.

View File

@ -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)

View File

@ -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']
)

View File

@ -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
}
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)
}
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)

View File

@ -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.',
]
}

View File

@ -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.']
}

View File

@ -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'"

View File

@ -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)

View File

@ -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{}

View File

@ -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'

View File

@ -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]
)