Debian Codegraph @

write_comment.go

raw symbols SCIP
1 package tygoja
2
3 import (
4 "go/ast"
5 "strings"
6 )
7
8 func (g *PackageGenerator) writeCommentGroup(s *strings.Builder, f *ast.CommentGroup, depth int) {
9 if f == nil {
10 return
11 }
12
13 docLines := strings.Split(f.Text(), "\n")
14
15 g.writeIndent(s, depth)
16 s.WriteString("/**\n")
17
18 lastLineIdx := len(docLines) - 1
19
20 var isCodeBlock bool
21
22 emptySB := new(strings.Builder)
23
24 for i, c := range docLines {
25 isEndLine := i == lastLineIdx
26 isEmpty := len(strings.TrimSpace(c)) == 0
27 isIndented := strings.HasPrefix(c, "\t") || strings.HasPrefix(c, " ")
28
29 // end code block
30 if isCodeBlock && (isEndLine || (!isIndented && !isEmpty)) {
31 g.writeIndent(s, depth)
32 s.WriteString(" * ```\n")
33 isCodeBlock = false
34 }
35
36 // accumulate empty comment lines
37 // (this is done to properly enclose code blocks with new lines)
38 if isEmpty {
39 g.writeIndent(emptySB, depth)
40 emptySB.WriteString(" * \n")
41 } else {
42 // write all empty lines
43 s.WriteString(emptySB.String())
44 emptySB.Reset()
45 }
46
47 // start code block
48 if isIndented && !isCodeBlock && !isEndLine {
49 g.writeIndent(s, depth)
50 s.WriteString(" * ```\n")
51 isCodeBlock = true
52 }
53
54 // write comment line
55 if !isEmpty {
56 g.writeIndent(s, depth)
57 s.WriteString(" * ")
58 c = strings.ReplaceAll(c, "*/", "*\\/") // An edge case: a // comment can contain */
59 s.WriteString(c)
60 s.WriteByte('\n')
61 }
62 }
63
64 g.writeIndent(s, depth)
65 s.WriteString(" */\n")
66 }