Skip to content

Commit

Permalink
fmt: fix formating of fn decl with end comments (#18181)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored May 17, 2023
1 parent ee7d34e commit 35f2a0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 20 additions & 1 deletion vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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('')
Expand Down
9 changes: 9 additions & 0 deletions vlib/v/fmt/tests/fn_with_end_comments_keep.vv
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit 35f2a0f

Please sign in to comment.