parser, fmt: fix the formatting of fn headers with inline comments (#14010)

pull/14012/head
yuyi 2022-04-12 03:31:06 +08:00 committed by GitHub
parent 07207db998
commit 3e3b2e25db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 1 deletions

View File

@ -953,8 +953,11 @@ fn (mut f Fmt) fn_body(node ast.FnDecl) {
if node.language == .v {
if !node.no_body {
f.write(' {')
f.comments(node.comments, inline: true)
if node.stmts.len > 0 || node.pos.line_nr < node.pos.last_line {
f.writeln('')
if node.comments.len == 0 {
f.writeln('')
}
f.stmts(node.stmts)
}
f.write('}')

View File

@ -0,0 +1,7 @@
fn main() { // main
return
}
fn print_hi() { // hi
println('hi')
}

View File

@ -0,0 +1,6 @@
fn /*main*/main() {
return
}
fn // hi
print_hi(){println('hi')}

View File

@ -182,6 +182,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
mut is_ctor_new := false
mut is_c2v_variadic := false
mut is_markused := false
mut comments := []ast.Comment{}
for fna in p.attrs {
match fna.name {
'noreturn' { is_noreturn = true }
@ -205,6 +206,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
p.next()
}
p.check(.key_fn)
comments << p.eat_comments()
p.open_scope()
// C. || JS.
mut language := ast.Language.v
@ -530,6 +532,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
scope: p.scope
label_names: p.label_names
end_comments: p.eat_comments(same_line: true)
comments: comments
}
if generic_names.len > 0 {
p.table.register_fn_generic_types(fn_decl.fkey())