Skip to content

Commit

Permalink
Fix comment placement
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Oct 23, 2023
1 parent 59d35bd commit a727581
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions format/format.go
Expand Up @@ -700,27 +700,22 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
}
}
if fields := results.NumFields(); fields > 0 { // The function has return values; let's clothe the return
var offset int
node.Results = make([]ast.Expr, 0, fields)
nameLoop:
for _, result := range results.List {
for _, ident := range result.Names {
name := ident.Name
if name == "_" { // we can't handle blank names just yet, abort the transform
node.Results = nil
offset = 0
break nameLoop
}
offset += len(name)
node.Results = append(node.Results, ast.NewIdent(name))
node.Results = append(node.Results, &ast.Ident{
NamePos: node.Pos(), // Use the Pos of the return statement, to not interfere with comment placement
Name: name,
})
}
}
if len(node.Results) > 0 {
// An ugly hack to update the Pos of any comment that immediately
// follows a rewritten return statement.
for _, comment := range f.commentsBetween(node.End(), c.Parent().End()) {
comment.List[0].Slash = comment.Pos() + token.Pos(offset)
}
c.Replace(node)
}
}
Expand Down

0 comments on commit a727581

Please sign in to comment.