-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathdescription.go
More file actions
124 lines (106 loc) · 2.63 KB
/
description.go
File metadata and controls
124 lines (106 loc) · 2.63 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package ir
import (
"strings"
"unicode"
"unicode/utf8"
"github.com/ogen-go/ogen/internal/naming"
)
// Global line limit, can be configured via SetLineLimit.
var lineLimit = 100
// Global flag to enable or disable pretty documentation.
var prettyDocEnabled = true
// SetLineLimit sets the maximum width of a comment line before it is wrapped.
// Use a negative value to disable line wrapping altogether.
func SetLineLimit(limit int) {
if limit == 0 {
// Use default value for zero.
lineLimit = 100
return
}
lineLimit = limit
}
// GetLineLimit returns the current line limit value.
func GetLineLimit() int {
return lineLimit
}
// SetPrettyDoc enables or disables pretty documentation.
func SetPrettyDoc(enabled bool) {
prettyDocEnabled = enabled
}
// IsPrettyDocEnabled returns whether pretty documentation is enabled.
func IsPrettyDocEnabled() bool {
return prettyDocEnabled
}
func splitLine(s string, limit int) (r []string) {
// If limit is negative, don't split lines.
if limit < 0 {
s = strings.TrimSpace(s)
if s == "" {
return nil
}
return []string{s}
}
s = strings.TrimSpace(s)
if s == "" {
return nil
}
// TODO(tdakkota): handle links in docs
for {
if len(s) < limit {
r = append(r, s)
return
}
idx := strings.LastIndexFunc(s[:limit-1], func(r rune) bool {
return unicode.IsSpace(r) || r == '.' || r == ',' || r == ';'
})
if idx < 0 || len(s)-1 == idx {
r = append(r, s)
return
}
if ch, size := utf8.DecodeRuneInString(s[idx:]); unicode.IsSpace(ch) {
r = append(r, s[:idx])
s = s[idx+size:]
} else {
// Do not cut dots and commas.
r = append(r, s[:idx+size])
s = s[idx+size:]
}
}
}
func prettyDoc(s, deprecation string) (r []string) {
// If pretty documentation is disabled, return the comment as-is
if !prettyDocEnabled {
// Just split by newlines.
for _, line := range strings.Split(s, "\n") {
r = append(r, line)
}
// Add deprecation notice if provided
if deprecation != "" {
if len(r) > 0 {
// Insert empty line between description and deprecated notice.
r = append(r, "")
}
r = append(r, deprecation)
}
return r
}
// Original pretty documentation behavior
// TODO(tdakkota): basic common mark rendering?
for _, line := range strings.Split(s, "\n") {
r = append(r, splitLine(line, lineLimit)...)
}
if len(r) > 0 {
r[0] = naming.Capitalize(r[0])
if last := r[len(r)-1]; len(last) > 0 && last[len(last)-1] != '.' {
r[len(r)-1] = last + "."
}
}
if deprecation != "" {
if len(r) > 0 {
// Insert empty line between description and deprecated notice.
r = append(r, "")
}
r = append(r, deprecation)
}
return r
}