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

ast: fix formatting fn header with parameter comments (#19059)

This commit is contained in:
yuyi 2023-08-05 12:33:13 +08:00 committed by GitHub
parent e5cd1724f9
commit f72cb00b74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -133,6 +133,7 @@ pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string
fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_mod string, m2a map[string]string) { fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_mod string, m2a map[string]string) {
mut add_para_types := true mut add_para_types := true
mut is_wrap_needed := false
if node.generic_names.len > 0 { if node.generic_names.len > 0 {
if node.is_method { if node.is_method {
sym := t.sym(node.params[0].typ) sym := t.sym(node.params[0].typ)
@ -168,6 +169,20 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
is_type_only := param.name == '' is_type_only := param.name == ''
should_add_type := true // is_last_param || is_type_only || node.params[i + 1].typ != param.typ || should_add_type := true // is_last_param || is_type_only || node.params[i + 1].typ != param.typ ||
// (node.is_variadic && i == node.params.len - 2) // (node.is_variadic && i == node.params.len - 2)
pre_comments := param.comments.filter(it.pos.pos < param.pos.pos)
if pre_comments.len > 0 {
if i == 0 && !pre_comments.last().is_inline {
is_wrap_needed = true
f.write_string('\n\t')
}
write_comments(pre_comments, mut f)
if !f.last_n(1)[0].is_space() {
f.write_string(' ')
}
}
if is_wrap_needed {
f.write_string('\t')
}
if param.is_mut { if param.is_mut {
f.write_string(param.typ.share().str() + ' ') f.write_string(param.typ.share().str() + ' ')
} }

View File

@ -0,0 +1,8 @@
fn foo(
// Foo
s string) {
}
fn bar( /* p1 */ a string, /* p2 */ b int) {
println('hello')
}

View File

@ -0,0 +1,8 @@
fn foo(
// Foo
s string) {
}
fn bar(/*p1*/a string, /*p2*/b int) {
println('hello')
}