-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_gen.go
213 lines (182 loc) · 5.15 KB
/
output_gen.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package errgoengine
import (
"fmt"
"strings"
)
type OutputGenerator struct {
GenAfterExplain func(*OutputGenerator)
Builder *strings.Builder
}
func (gen *OutputGenerator) Heading(level int, text string) {
// dont go below zero, dont go above 6
level = max(min(6, level), 0)
for i := 0; i < level; i++ {
gen.Builder.WriteByte('#')
}
gen.Builder.WriteByte(' ')
gen.Writeln(text)
}
func (gen *OutputGenerator) Break() {
gen.Builder.WriteByte('\n')
}
func (gen *OutputGenerator) FromExplanation(level int, explain *ExplainGenerator) {
if level == 1 && (explain.Builder == nil || explain.Builder.Len() == 0) && explain.Sections == nil {
gen.Writeln("No explanation found for this error.")
return
}
if explain.Builder != nil {
gen.Write(explain.Builder.String())
}
if explain.Sections != nil {
for sectionName, exp := range explain.Sections {
gen.Break()
gen.Heading(level+1, sectionName)
gen.FromExplanation(level+1, exp)
}
} else {
gen.Break()
}
}
func (gen *OutputGenerator) Writeln(str string, d ...any) {
if len(str) == 0 {
return
}
gen.Write(str, d...)
gen.Break()
}
func (gen *OutputGenerator) Write(str string, d ...any) {
final := fmt.Sprintf(str, d...)
for _, c := range final {
if c == '\t' {
// 1 tab = 4 spaces
gen.Builder.WriteString(" ")
} else {
gen.Builder.WriteRune(c)
}
}
}
func (gen *OutputGenerator) WriteLines(lines ...string) {
for _, line := range lines {
if len(line) == 0 {
gen.Break()
} else {
gen.Writeln(line)
}
}
}
func (gen *OutputGenerator) Generate(explain *ExplainGenerator, bugFix *BugFixGenerator) string {
if gen.Builder == nil {
gen.Builder = &strings.Builder{}
}
if len(explain.ErrorName) != 0 {
gen.Heading(1, explain.ErrorName)
}
gen.FromExplanation(1, explain)
if gen.GenAfterExplain != nil {
gen.GenAfterExplain(gen)
}
gen.Heading(2, "Steps to fix")
if bugFix.Suggestions != nil && len(bugFix.Suggestions) != 0 {
for sIdx, s := range bugFix.Suggestions {
if len(bugFix.Suggestions) == 1 {
gen.Heading(3, s.Title)
} else {
gen.Heading(3, fmt.Sprintf("%d. %s", sIdx+1, s.Title))
}
for idx, step := range s.Steps {
if len(s.Steps) == 1 {
gen.Writeln(step.Content)
} else {
gen.Writeln(fmt.Sprintf("%d. %s", idx+1, step.Content))
}
if step.Fixes == nil && len(step.Fixes) == 0 {
continue
}
if len(step.Fixes) != 0 {
doc := step.Doc.Document
descriptionBuilder := &strings.Builder{}
// get the start and end line after applying the diff
startLine := step.StartLine
afterLine := step.AfterLine
// get the original start and end line
origStartLine := step.OrigStartLine
origAfterLine := step.OrigAfterLine
gen.Writeln("```diff")
// use origStartLine instead of startLine because we want to show the original lines
if startLine > 0 {
deduct := -2
if step.DiffPosition.Line < 0 {
deduct += step.DiffPosition.Line
}
gen.WriteLines(step.Doc.LinesAt(origStartLine+deduct, origStartLine-1)...)
}
modified := step.Doc.ModifiedLinesAt(startLine, afterLine)
original := step.Doc.LinesAt(origStartLine, origAfterLine)
for i, origLine := range original {
if i >= len(modified) || modified[i] != origLine {
gen.Write("- ")
}
if len(origLine) == 0 {
gen.Break()
} else {
gen.Writeln(origLine)
}
}
// show this only if the total is not negative
if startLine >= origStartLine && afterLine >= origAfterLine {
// TODO: redundant
modified := step.Doc.ModifiedLinesAt(startLine, afterLine)
// TODO: merge with previous `original` variable
originalLines := doc.LinesAt(origStartLine, min(origAfterLine+step.DiffPosition.Line, doc.TotalLines()))
for i, modifiedLine := range modified {
if i == 0 && len(modified) == 1 && len(modifiedLine) == 0 {
continue
}
// skip marking as "addition" if the lines are the same
if i < len(originalLines) && modifiedLine == originalLines[i] {
// write only if the line is not the last line
if startLine+i < origAfterLine {
gen.Write(modifiedLine)
gen.Break()
}
continue
}
gen.Write("+")
if len(modifiedLine) != 0 {
gen.Write(" ")
}
gen.Write(modifiedLine)
gen.Break()
}
}
gen.WriteLines(step.Doc.LinesAt(origAfterLine+1, min(origAfterLine+2, step.Doc.TotalLines()))...)
gen.Writeln("```")
for fIdx, fix := range step.Fixes {
if len(fix.Description) != 0 {
if fIdx < len(step.Fixes)-1 {
descriptionBuilder.WriteString(fix.Description + "\n")
} else {
descriptionBuilder.WriteString(fix.Description)
}
}
}
if descriptionBuilder.Len() != 0 {
gen.Writeln(descriptionBuilder.String())
}
}
}
if sIdx < len(bugFix.Suggestions)-1 {
gen.Break()
}
}
} else {
gen.Writeln("No bug fixes found for this error.")
}
return strings.TrimSpace(gen.Builder.String())
}
func (gen *OutputGenerator) Reset() {
if gen.GenAfterExplain != nil {
gen.GenAfterExplain = nil
}
gen.Builder.Reset()
}