vfmt2: skip arg type if possible

pull/3777/head
Alexey 2020-02-18 23:02:43 +03:00 committed by GitHub
parent c314ab7b60
commit 39c4842bf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 4 deletions

View File

@ -29,7 +29,7 @@ pub fn fmt(file ast.File, table &table.Table) string {
indent: -1
}
f.stmts(file.stmts)
return f.out.str()
return f.out.str().trim_space() + '\n'
}
pub fn (f mut Fmt) write(s string) {
@ -106,9 +106,14 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
}
f.write('fn ${receiver}${it.name}(')
for i, arg in it.args {
sym := f.table.get_type_symbol(arg.typ)
f.write('$arg.name $sym.name')
if i < it.args.len - 1 {
is_last_arg := i == it.args.len - 1
should_add_type := is_last_arg || it.args[i + 1].typ != arg.typ
f.write(arg.name)
if should_add_type {
arg_typ_sym := f.table.get_type_symbol(arg.typ)
f.write(' ${arg_typ_sym.name}')
}
if !is_last_arg {
f.write(', ')
}
}

View File

@ -45,3 +45,26 @@ fn voidfn() {
println('this is a function that does not return anything')
}
fn fn_with_1_arg(arg int) int {
return 0
}
fn fn_with_2a_args(arg1, arg2 int) int {
return 0
}
fn fn_with_2_args_to_be_shorten(arg1, arg2 int) int {
return 0
}
fn fn_with_2b_args(arg1 string, arg2 int) int {
return 0
}
fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int {
return 0
}
fn (this User) fn_with_receiver() {
println('')
}

View File

@ -48,3 +48,27 @@ User
fn voidfn(){
println('this is a function that does not return anything')
}
fn fn_with_1_arg(arg int) int {
return 0
}
fn fn_with_2a_args(arg1, arg2 int) int {
return 0
}
fn fn_with_2_args_to_be_shorten(arg1 int, arg2 int) int {
return 0
}
fn fn_with_2b_args(arg1 string, arg2 int) int {
return 0
}
fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int {
return 0
}
fn (this User) fn_with_receiver() {
println('')
}