1
0
Fork 0
golang-github-pocketbase-ty.../write_comment.go
Daniel Baumann b6e042e2af
Adding upstream version 0.0~git20250307.c2e6a77.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-22 11:21:48 +02:00

66 lines
1.4 KiB
Go

package tygoja
import (
"go/ast"
"strings"
)
func (g *PackageGenerator) writeCommentGroup(s *strings.Builder, f *ast.CommentGroup, depth int) {
if f == nil {
return
}
docLines := strings.Split(f.Text(), "\n")
g.writeIndent(s, depth)
s.WriteString("/**\n")
lastLineIdx := len(docLines) - 1
var isCodeBlock bool
emptySB := new(strings.Builder)
for i, c := range docLines {
isEndLine := i == lastLineIdx
isEmpty := len(strings.TrimSpace(c)) == 0
isIndented := strings.HasPrefix(c, "\t") || strings.HasPrefix(c, " ")
// end code block
if isCodeBlock && (isEndLine || (!isIndented && !isEmpty)) {
g.writeIndent(s, depth)
s.WriteString(" * ```\n")
isCodeBlock = false
}
// accumulate empty comment lines
// (this is done to properly enclose code blocks with new lines)
if isEmpty {
g.writeIndent(emptySB, depth)
emptySB.WriteString(" * \n")
} else {
// write all empty lines
s.WriteString(emptySB.String())
emptySB.Reset()
}
// start code block
if isIndented && !isCodeBlock && !isEndLine {
g.writeIndent(s, depth)
s.WriteString(" * ```\n")
isCodeBlock = true
}
// write comment line
if !isEmpty {
g.writeIndent(s, depth)
s.WriteString(" * ")
c = strings.ReplaceAll(c, "*/", "*\\/") // An edge case: a // comment can contain */
s.WriteString(c)
s.WriteByte('\n')
}
}
g.writeIndent(s, depth)
s.WriteString(" */\n")
}