File: comments.go

package info (click to toggle)
golang-github-dave-jennifer 1.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 372 kB
  • sloc: makefile: 4
file content (108 lines) | stat: -rw-r--r-- 3,277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package jen

import (
	"fmt"
	"io"
	"strings"
)

// Comment adds a comment. If the provided string contains a newline, the
// comment is formatted in multiline style. If the comment string starts
// with "//" or "/*", the automatic formatting is disabled and the string is
// rendered directly.
func Comment(str string) *Statement {
	return newStatement().Comment(str)
}

// Comment adds a comment. If the provided string contains a newline, the
// comment is formatted in multiline style. If the comment string starts
// with "//" or "/*", the automatic formatting is disabled and the string is
// rendered directly.
func (g *Group) Comment(str string) *Statement {
	s := Comment(str)
	g.items = append(g.items, s)
	return s
}

// Comment adds a comment. If the provided string contains a newline, the
// comment is formatted in multiline style. If the comment string starts
// with "//" or "/*", the automatic formatting is disabled and the string is
// rendered directly.
func (s *Statement) Comment(str string) *Statement {
	c := comment{
		comment: str,
	}
	*s = append(*s, c)
	return s
}

// Commentf adds a comment, using a format string and a list of parameters. If
// the provided string contains a newline, the comment is formatted in
// multiline style. If the comment string starts with "//" or "/*", the
// automatic formatting is disabled and the string is rendered directly.
func Commentf(format string, a ...interface{}) *Statement {
	return newStatement().Commentf(format, a...)
}

// Commentf adds a comment, using a format string and a list of parameters. If
// the provided string contains a newline, the comment is formatted in
// multiline style. If the comment string starts with "//" or "/*", the
// automatic formatting is disabled and the string is rendered directly.
func (g *Group) Commentf(format string, a ...interface{}) *Statement {
	s := Commentf(format, a...)
	g.items = append(g.items, s)
	return s
}

// Commentf adds a comment, using a format string and a list of parameters. If
// the provided string contains a newline, the comment is formatted in
// multiline style. If the comment string starts with "//" or "/*", the
// automatic formatting is disabled and the string is rendered directly.
func (s *Statement) Commentf(format string, a ...interface{}) *Statement {
	c := comment{
		comment: fmt.Sprintf(format, a...),
	}
	*s = append(*s, c)
	return s
}

type comment struct {
	comment string
}

func (c comment) isNull(f *File) bool {
	return false
}

func (c comment) render(f *File, w io.Writer, s *Statement) error {
	if strings.HasPrefix(c.comment, "//") || strings.HasPrefix(c.comment, "/*") {
		// automatic formatting disabled.
		if _, err := w.Write([]byte(c.comment)); err != nil {
			return err
		}
		return nil
	}
	if strings.Contains(c.comment, "\n") {
		if _, err := w.Write([]byte("/*\n")); err != nil {
			return err
		}
	} else {
		if _, err := w.Write([]byte("// ")); err != nil {
			return err
		}
	}
	if _, err := w.Write([]byte(c.comment)); err != nil {
		return err
	}
	if strings.Contains(c.comment, "\n") {
		if !strings.HasSuffix(c.comment, "\n") {
			if _, err := w.Write([]byte("\n")); err != nil {
				return err
			}
		}
		if _, err := w.Write([]byte("*/")); err != nil {
			return err
		}
	}
	return nil
}