Skip to content

Commit

Permalink
refactor: remove contextData param in OutputGen, add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 3, 2024
1 parent 3a501c2 commit a5937e0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
46 changes: 45 additions & 1 deletion errgoengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ErrgoEngine struct {
ErrorTemplates ErrorTemplates
FS *MultiReadFileFS
OutputGen *OutputGenerator
IsTesting bool
}

func New() *ErrgoEngine {
Expand Down Expand Up @@ -125,7 +126,50 @@ func (e *ErrgoEngine) Translate(template *CompiledErrorTemplate, contextData *Co
template.OnGenBugFixFn(contextData, fixGen)
}

output := e.OutputGen.Generate(contextData, expGen, fixGen)
if e.IsTesting {
// add a code snippet that points to the error
e.OutputGen.GenAfterExplain = func(gen *OutputGenerator) {
err := contextData.MainError
if err == nil {
return
}

doc := err.Document
if doc == nil || err.Nearest.IsNull() {
return
}

startLineNr := err.Nearest.StartPosition().Line
startLines := doc.LinesAt(max(startLineNr-1, 0), startLineNr)
endLines := doc.LinesAt(min(startLineNr+1, doc.TotalLines()), min(startLineNr+2, doc.TotalLines()))
arrowLength := int(err.Nearest.EndByte() - err.Nearest.StartByte())
if arrowLength == 0 {
arrowLength = 1
}

startArrowPos := err.Nearest.StartPosition().Column
gen.Writeln("```")
gen.WriteLines(startLines...)

for i := 0; i < startArrowPos; i++ {
if startLines[len(startLines)-1][i] == '\t' {
gen.Builder.WriteString(" ")
} else {
gen.Builder.WriteByte(' ')
}
}

for i := 0; i < arrowLength; i++ {
gen.Builder.WriteByte('^')
}

gen._break()
gen.WriteLines(endLines...)
gen.Writeln("```")
}
}

output := e.OutputGen.Generate(expGen, fixGen)
defer e.OutputGen.Reset()

return expGen.Builder.String(), output
Expand Down
2 changes: 1 addition & 1 deletion error_templates/test_utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func SetupTest(tb testing.TB, cfg SetupTestConfig) TestCases {

// load error templates
engine := lib.New()
engine.OutputGen.IsTesting = true
engine.IsTesting = true

cfg.TemplateLoader(&engine.ErrorTemplates)

Expand Down
39 changes: 9 additions & 30 deletions output_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type OutputGenerator struct {
IsTesting bool
Builder *strings.Builder
GenAfterExplain func(*OutputGenerator)
Builder *strings.Builder
}

func (gen *OutputGenerator) Heading(level int, text string) {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (gen *OutputGenerator) WriteLines(lines ...string) {
}
}

func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator, bugFix *BugFixGenerator) string {
func (gen *OutputGenerator) Generate(explain *ExplainGenerator, bugFix *BugFixGenerator) string {
if gen.Builder == nil {
gen.Builder = &strings.Builder{}
}
Expand All @@ -80,33 +80,8 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator,
}

gen.ExpGen(1, explain)
doc := cd.MainError.Document

if doc != nil && gen.IsTesting && !cd.MainError.Nearest.IsNull() {
startLineNr := cd.MainError.Nearest.StartPosition().Line
startLines := doc.LinesAt(max(startLineNr-1, 0), startLineNr)
endLines := doc.LinesAt(min(startLineNr+1, doc.TotalLines()), min(startLineNr+2, doc.TotalLines()))
arrowLength := int(cd.MainError.Nearest.EndByte() - cd.MainError.Nearest.StartByte())
if arrowLength == 0 {
arrowLength = 1
}

startArrowPos := cd.MainError.Nearest.StartPosition().Column
gen.Writeln("```")
gen.WriteLines(startLines...)
for i := 0; i < startArrowPos; i++ {
if startLines[len(startLines)-1][i] == '\t' {
gen.Builder.WriteString(" ")
} else {
gen.Builder.WriteByte(' ')
}
}
for i := 0; i < arrowLength; i++ {
gen.Builder.WriteByte('^')
}
gen._break()
gen.WriteLines(endLines...)
gen.Writeln("```")
if gen.GenAfterExplain != nil {
gen.GenAfterExplain(gen)
}

gen.Heading(2, "Steps to fix")
Expand All @@ -131,6 +106,7 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator,
}

if len(step.Fixes) != 0 {
doc := step.Doc.Document
descriptionBuilder := &strings.Builder{}

// get the start and end line after applying the diff
Expand Down Expand Up @@ -225,5 +201,8 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator,
}

func (gen *OutputGenerator) Reset() {
if gen.GenAfterExplain != nil {
gen.GenAfterExplain = nil
}
gen.Builder.Reset()
}

0 comments on commit a5937e0

Please sign in to comment.