fmt: keep trailing comments after fn header decl (#13596)

pull/13606/head
Larpon 2022-02-25 14:36:48 +01:00 committed by GitHub
parent 83ea97b1a3
commit d80f5165dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 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

@ -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)
}

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())