in format.go [153:206]
func (un *formatter) CommentBlock(id int64) []string {
if !un.options.pretty {
return nil
}
var comments []string
start := un.info.GetStartLocation(id)
first := true // ¯\_(ツ)_/¯ The AST's position information is weaker than is ideal.
wasBlank := false
for line := start.Line(); line > 0; line-- {
text, ok := un.src.Snippet(line)
comment := strings.TrimSpace(text)
if !ok || (comment != "" && !strings.HasPrefix(comment, "//")) {
if first {
first = false
continue
}
break
}
first = false
loc := location{line, strings.Index(text, comment)}
if cid, ok := un.comments[loc]; ok {
if cid != id {
return nil
}
break
}
un.comments[loc] = id
if comment != "" {
// Remove comment prefix and any leading non-tab spaces.
comment = strings.TrimPrefix(comment, "//")
idx := strings.IndexFunc(comment, func(r rune) bool {
return r == '\t' || !unicode.IsSpace(r)
})
if idx >= 0 {
comment = comment[idx:]
}
// Remove all trailing white space.
comment = strings.TrimRight(comment, " \t\n\v\f\r\u0085\u00a0")
// Format comment with a leading space between text and mark.
if comment == "" {
comment = "//"
} else {
comment = "// " + comment
}
comments = append(comments, comment)
wasBlank = false
} else if !wasBlank {
comments = append(comments, "")
wasBlank = true
}
}
slices.Reverse(comments)
return comments
}