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

fmt: fix formating of fn decl with end comments (#18181)

This commit is contained in:
yuyi 2023-05-17 08:05:59 +08:00 committed by GitHub
parent ee7d34e650
commit 35f2a0fb66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -1001,7 +1001,7 @@ 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 {
if node.no_body && node.end_comments.len > 0 {
first_comment := node.end_comments[0]
if first_comment.text.contains('\n') {
f.writeln('\n')
@ -1045,6 +1045,25 @@ fn (mut f Fmt) fn_body(node ast.FnDecl) {
f.stmts(node.stmts)
}
f.write('}')
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')
}
}
}
}
}
if !node.is_anon {
f.writeln('')

View File

@ -0,0 +1,9 @@
fn (c Color) is_blue() bool {
return c == .blue
} // method of enum
fn (c Color) show_int() int {
return int(c)
} // method of enum
fn main() {}