diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9daee0c623..407f18db37 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 698c61335b..82c5c35644 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -914,6 +914,26 @@ 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 { + first_comment := node.end_comments[0] + if first_comment.text.contains('\n') { + f.writeln('\n') + } else { + f.write(' ') + } + 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) } diff --git a/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv b/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv new file mode 100644 index 0000000000..84cd474729 --- /dev/null +++ b/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv @@ -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 +*/ diff --git a/vlib/v/fmt/tests/fn_headers_with_comments_input.vv b/vlib/v/fmt/tests/fn_headers_with_comments_input.vv new file mode 100644 index 0000000000..9474830d69 --- /dev/null +++ b/vlib/v/fmt/tests/fn_headers_with_comments_input.vv @@ -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 +*/ diff --git a/vlib/v/fmt/tests/fn_headers_with_comments_keep.vv b/vlib/v/fmt/tests/fn_headers_with_comments_keep.vv new file mode 100644 index 0000000000..dd4d451c53 --- /dev/null +++ b/vlib/v/fmt/tests/fn_headers_with_comments_keep.vv @@ -0,0 +1,5 @@ +fn C.Mix_LoadMUS1(file byteptr) voidptr // Mix_Music + +fn C.Mix_LoadMUS2(file byteptr) voidptr // Mix_Music 2 + +// Comment diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 8a09e9e20c..4d30f4d57f 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -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())