fmt: keep trailing comments after fn header decl, fixes #11396

pull/13596/head
lmp 2022-02-24 15:27:08 +01:00
parent fa645516c3
commit ecb7b0a99f
6 changed files with 52 additions and 1 deletions

View File

@ -530,7 +530,8 @@ pub mut:
has_await bool // 'true' if this function uses JS.await
//
comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl
next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl
end_comments []Comment // comments *after* header declarations. E.g.: `fn C.C_func(x int) int // Comment`
next_comments []Comment // comments that are one line after the decl; used for InterfaceDecl
//
source_file &File = 0
scope &Scope

View File

@ -899,6 +899,25 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
pub fn (mut f Fmt) fn_decl(node ast.FnDecl) {
f.attrs(node.attrs)
f.write(node.stringify(f.table, f.cur_mod, f.mod2alias)) // `Expr` instead of `ast.Expr` in mod ast
// Handle trailing comments after fn header declarations
if node.end_comments.len > 0 {
f.write(' ')
first_comment := node.end_comments[0]
if first_comment.text.contains('\n') {
f.writeln('\n')
}
f.comment(first_comment)
if node.end_comments.len > 1 {
f.writeln('\n')
comments := node.end_comments[1..]
for i, comment in comments {
f.comment(comment)
if i != comments.len-1 {
f.writeln('\n')
}
}
}
}
f.fn_body(node)
}

View File

@ -0,0 +1,16 @@
fn C.Mix_LoadMUS1(file byteptr) voidptr // *Mix_Music
fn C.Mix_LoadMUS2(file byteptr) voidptr //*Mix_Music
fn C.Mix_LoadMUS3(file byteptr) voidptr // 1
// 2
// 3
// Loads music
fn C.Mix_LoadMUS4(file byteptr) voidptr
/*
Test
*/

View File

@ -0,0 +1,9 @@
fn C.Mix_LoadMUS1(file byteptr) voidptr // *Mix_Music
fn C.Mix_LoadMUS2(file byteptr) voidptr /* *Mix_Music */
fn C.Mix_LoadMUS3(file byteptr) voidptr /* 1 */ /* 2 */ /* 3 */
// Loads music
fn C.Mix_LoadMUS4(file byteptr) voidptr /* Test
*/

View File

@ -0,0 +1,5 @@
fn C.Mix_LoadMUS1(file byteptr) voidptr // Mix_Music
fn C.Mix_LoadMUS2(file byteptr) voidptr // Mix_Music 2
// Comment

View File

@ -527,6 +527,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
is_builtin: p.builtin_mod || p.mod in util.builtin_module_parts
scope: p.scope
label_names: p.label_names
end_comments: p.eat_comments(same_line: true)
}
if generic_names.len > 0 {
p.table.register_fn_generic_types(fn_decl.fkey())