Skip to content

Commit aa39dd6

Browse files
committed
feat: Add top-level comment_pretty_doc config option
This commit introduces a new configuration option which can be used to enable or disable post-processing of the comment/description. This is useful for providing the description as a comment verbatim. Signed-off-by: Alexander Jung <alex@nder.sh>
1 parent 29500ea commit aa39dd6

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

gen/generator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ func NewGenerator(spec *ogen.Spec, opts Options) (*Generator, error) {
7373

7474
// Set the comment line limit for pretty documentation.
7575
ir.SetLineLimit(opts.Generator.CommentLineLimit)
76+
// Set whether to use pretty documentation.
77+
ir.SetPrettyDoc(opts.Generator.CommentPrettyDoc)
7678

7779
var external jsonschema.ExternalResolver
7880
if opts.Parser.AllowRemote {

gen/ir/description.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
// Global line limit, can be configured via SetLineLimit.
1212
var lineLimit = 100
1313

14+
// Global flag to enable or disable pretty documentation.
15+
var prettyDocEnabled = true
16+
1417
// SetLineLimit sets the maximum width of a comment line before it is wrapped.
1518
// Use a negative value to disable line wrapping altogether.
1619
func SetLineLimit(limit int) {
@@ -27,6 +30,16 @@ func GetLineLimit() int {
2730
return lineLimit
2831
}
2932

33+
// SetPrettyDoc enables or disables pretty documentation.
34+
func SetPrettyDoc(enabled bool) {
35+
prettyDocEnabled = enabled
36+
}
37+
38+
// IsPrettyDocEnabled returns whether pretty documentation is enabled.
39+
func IsPrettyDocEnabled() bool {
40+
return prettyDocEnabled
41+
}
42+
3043
func splitLine(s string, limit int) (r []string) {
3144
// If limit is negative, don't split lines.
3245
if limit < 0 {
@@ -68,6 +81,25 @@ func splitLine(s string, limit int) (r []string) {
6881
}
6982

7083
func prettyDoc(s, deprecation string) (r []string) {
84+
// If pretty documentation is disabled, return the comment as-is
85+
if !prettyDocEnabled {
86+
// Just split by newlines.
87+
for _, line := range strings.Split(s, "\n") {
88+
r = append(r, line)
89+
}
90+
91+
// Add deprecation notice if provided
92+
if deprecation != "" {
93+
if len(r) > 0 {
94+
// Insert empty line between description and deprecated notice.
95+
r = append(r, "")
96+
}
97+
r = append(r, deprecation)
98+
}
99+
100+
return r
101+
}
102+
71103
// Original pretty documentation behavior
72104
// TODO(tdakkota): basic common mark rendering?
73105
for _, line := range strings.Split(s, "\n") {

gen/ir/description_flag_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ir
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestPrettyDocEnabledFlag(t *testing.T) {
10+
// Save original state to restore later
11+
originalState := prettyDocEnabled
12+
13+
// Test with pretty documentation enabled
14+
SetPrettyDoc(true)
15+
require.True(t, IsPrettyDocEnabled())
16+
doc := " this is a test. "
17+
prettyResult := prettyDoc(doc, "")
18+
require.Equal(t, "This is a test.", prettyResult[0])
19+
20+
// Test with pretty documentation disabled
21+
SetPrettyDoc(false)
22+
require.False(t, IsPrettyDocEnabled())
23+
verbatimResult := prettyDoc(doc, "")
24+
require.Equal(t, " this is a test. ", verbatimResult[0])
25+
26+
// Restore original state
27+
SetPrettyDoc(originalState)
28+
}

gen/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ type GenerateOptions struct {
161161
IgnoreNotImplemented []string `json:"ignore_not_implemented" yaml:"ignore_not_implemented"`
162162
// NotImplementedHook is hook for ErrNotImplemented errors.
163163
NotImplementedHook func(name string, err error) `json:"-" yaml:"-"`
164+
// CommentPrettyDoc enables or disables pretty documentation for comments.
165+
CommentPrettyDoc bool `json:"comment_pretty_doc" yaml:"comment_pretty_doc"`
164166
// CommentLineLimit sets the maximum width of a comment line before it is
165167
// wrapped. -1 disables line wrapping altogether.
166168
CommentLineLimit int `json:"comment_line_limit" yaml:"comment_line_limit"`

0 commit comments

Comments
 (0)