func findBreakPoint(line string, start int, width int) int {
	limit := start + width + 1
	if limit >= len(line) {
		return len(line) - 1
	}
	index := strings.LastIndex(line[start:limit], " ")
	if index >= 0 {
		return start + index
	}
	index = strings.Index(line[limit:], " ")
	if index >= 0 {
		return limit + index
	}
	return len(line) - 1
}
findBreakPoint finds the character at which to break two lines
Returned index points to the last character that should be included on the line
If breaking at a space, this will give a trailing space, but allows for
breaking at other points too as no characters will be omitted.
package astbuilder
import (
	"regexp"
	"strings"
	"github.com/dave/dst"
)
func AddWrappedComments(commentList *dst.Decorations, comments []string, width int) {
	for _, comment := range comments {
		if comment == "" {
			continue
		}
		AddWrappedComment(commentList, comment, width)
	}
}
func AddWrappedComment(commentList *dst.Decorations, comment string, width int) {
	for _, c := range formatComment(comment, width) {
		AddComment(commentList, c)
	}
}
func AddComments(commentList *dst.Decorations, comments []string) {
	for _, comment := range comments {
		if comment == "" {
			continue
		}
		AddComment(commentList, comment)
	}
}
func AddComment(commentList *dst.Decorations, comment string) {
	line := comment
	if !strings.HasPrefix(line, "//") {
		line = "//" + line
	}
	commentList.Append(line)
}
func formatComment(comment string, width int) []string {
	text := strings.ReplaceAll(comment, "**", "")
	text = brRegex.ReplaceAllLiteralString(text, "\n")
	lines := strings.Split(text, "\n")
	for i, l := range lines {
		lines[i] = strings.TrimSpace(l)
	}
	return docCommentWrap(lines, width)
}
var brRegex = regexp.MustCompile("<br[^/>]*/?>")
func docCommentWrap(lines []string, width int) []string {
	var result []string
	for _, l := range lines {
		result = append(result, wordWrap(l, width)...)
	}
	return result
}
func wordWrap(text string, width int) []string {
	var result []string
	start := 0
	for start < len(text) {
		finish := findBreakPoint(text, start, width)
		result = append(result, text[start:finish+1])
		start = finish + 1
	}
	return result
}