From c08a3831133059a23b7b21ad97659eff5c397051 Mon Sep 17 00:00:00 2001 From: Martin Lange <44003176+mlange-42@users.noreply.github.com> Date: Sun, 9 Feb 2025 23:51:34 +0100 Subject: [PATCH] Split up Processor mega-struct (#219) * move collecting paths to a separate type * move renaming to a separate type --- CHANGELOG.md | 6 +++ internal/document/links.go | 2 +- internal/document/paths.go | 81 ++++++++++++++++++++-------------- internal/document/processor.go | 12 ++--- internal/document/rename.go | 35 ++++++++------- 5 files changed, 81 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8f103d..7b95e48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [[unpublished]](https://github.com/mlange-42/modo/compare/v0.11.0...main) + +### Other + +* Internal: splits up the `Processor` mega-struct (#218, #219) + ## [[v0.11.0]](https://github.com/mlange-42/modo/compare/v0.10.1...v0.11.0) ### Features diff --git a/internal/document/links.go b/internal/document/links.go index 4fcb77d..05e8689 100644 --- a/internal/document/links.go +++ b/internal/document/links.go @@ -266,7 +266,7 @@ func findLinks(text string, regex *regexp.Regexp, isReference bool) ([]int, erro if len(text) > r[7] && string(text[r[7]]) == "(" { continue } - // Excludes doucle square brackets + // Excludes double square brackets if r[6] > 0 && string(text[r[6]-1]) == "[" { continue } diff --git a/internal/document/paths.go b/internal/document/paths.go index 9c95df8..18d19ed 100644 --- a/internal/document/paths.go +++ b/internal/document/paths.go @@ -12,129 +12,142 @@ type elemPath struct { type addPathFunc = func(Named, []string, []string, string, bool) +type pathHelper struct { + AddPathFunc addPathFunc + SetLink bool +} + // Collects lookup for link target paths. // Runs on the re-structured package. func (proc *Processor) collectPaths() { - proc.linkTargets = map[string]elemPath{} - proc.collectPathsPackage(proc.ExportDocs.Decl, []string{}, []string{}, proc.addLinkTarget, false) + pc := pathHelper{ + AddPathFunc: proc.addLinkTarget, + SetLink: false, + } + pc.collectPathsPackage(proc.ExportDocs.Decl, []string{}, []string{}) } // Collects the paths of all (sub)-elements in the original structure. func (proc *Processor) collectElementPaths() { - proc.allPaths = map[string]Named{} - proc.collectPathsPackage(proc.Docs.Decl, []string{}, []string{}, proc.addElementPath, true) + pc := pathHelper{ + AddPathFunc: proc.addElementPath, + SetLink: true, + } + pc.collectPathsPackage(proc.Docs.Decl, []string{}, []string{}) } -func (proc *Processor) collectPathsPackage(p *Package, elems []string, pathElem []string, add addPathFunc, isOriginal bool) { +func (pc *pathHelper) collectPathsPackage(p *Package, elems []string, pathElem []string) { newElems := appendNew(elems, p.GetName()) newPath := appendNew(pathElem, p.GetFileName()) - if isOriginal { + if pc.SetLink { p.SetLink(newElems, p.Kind) } - add(p, newElems, newPath, "package", false) + pc.AddPathFunc(p, newElems, newPath, "package", false) for _, pkg := range p.Packages { - proc.collectPathsPackage(pkg, newElems, newPath, add, isOriginal) + pc.collectPathsPackage(pkg, newElems, newPath) } for _, mod := range p.Modules { - proc.collectPathsModule(mod, newElems, newPath, add, isOriginal) + pc.collectPathsModule(mod, newElems, newPath) } for _, e := range p.Structs { - proc.collectPathsStruct(e, newElems, newPath, add, isOriginal) + pc.collectPathsStruct(e, newElems, newPath) } for _, e := range p.Traits { - proc.collectPathsTrait(e, newElems, newPath, add, isOriginal) + pc.collectPathsTrait(e, newElems, newPath) } for _, e := range p.Aliases { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#aliases") - add(e, newElems, newPath, "package", true) // kind=package for correct link paths + pc.AddPathFunc(e, newElems, newPath, "package", true) // kind=package for correct link paths } for _, e := range p.Functions { - if isOriginal { + if pc.SetLink { e.SetLink(newElems, e.Kind) } newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, e.GetFileName()) - add(e, newElems, newPath, "function", false) + pc.AddPathFunc(e, newElems, newPath, "function", false) } } -func (proc *Processor) collectPathsModule(m *Module, elems []string, pathElem []string, add addPathFunc, isOriginal bool) { +func (pc *pathHelper) collectPathsModule(m *Module, elems []string, pathElem []string) { newElems := appendNew(elems, m.GetName()) newPath := appendNew(pathElem, m.GetFileName()) - m.SetLink(newElems, m.Kind) - add(m, newElems, newPath, "module", false) + if pc.SetLink { + m.SetLink(newElems, m.Kind) + } + pc.AddPathFunc(m, newElems, newPath, "module", false) for _, e := range m.Structs { - proc.collectPathsStruct(e, newElems, newPath, add, isOriginal) + pc.collectPathsStruct(e, newElems, newPath) } for _, e := range m.Traits { - proc.collectPathsTrait(e, newElems, newPath, add, isOriginal) + pc.collectPathsTrait(e, newElems, newPath) } for _, e := range m.Aliases { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#aliases") - add(e, newElems, newPath, "module", true) // kind=module for correct link paths + pc.AddPathFunc(e, newElems, newPath, "module", true) // kind=module for correct link paths } for _, f := range m.Functions { - if isOriginal { + if pc.SetLink { f.SetLink(newElems, f.Kind) } newElems := appendNew(newElems, f.GetName()) newPath := appendNew(newPath, f.GetFileName()) - add(f, newElems, newPath, "function", false) + pc.AddPathFunc(f, newElems, newPath, "function", false) } } -func (proc *Processor) collectPathsStruct(s *Struct, elems []string, pathElem []string, add addPathFunc, isOriginal bool) { +func (pc *pathHelper) collectPathsStruct(s *Struct, elems []string, pathElem []string) { newElems := appendNew(elems, s.GetName()) newPath := appendNew(pathElem, s.GetFileName()) - if isOriginal { + if pc.SetLink { s.SetLink(elems, s.Kind) } - add(s, newElems, newPath, "struct", false) + pc.AddPathFunc(s, newElems, newPath, "struct", false) for _, e := range s.Aliases { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#aliases") - add(e, newElems, newPath, "member", true) + pc.AddPathFunc(e, newElems, newPath, "member", true) } for _, e := range s.Parameters { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#parameters") - add(e, newElems, newPath, "member", true) + pc.AddPathFunc(e, newElems, newPath, "member", true) } for _, e := range s.Fields { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#fields") - add(e, newElems, newPath, "member", true) + pc.AddPathFunc(e, newElems, newPath, "member", true) } for _, e := range s.Functions { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#"+strings.ToLower(e.GetName())) - add(e, newElems, newPath, "member", true) + pc.AddPathFunc(e, newElems, newPath, "member", true) } } -func (proc *Processor) collectPathsTrait(t *Trait, elems []string, pathElem []string, add addPathFunc, isOriginal bool) { +func (pc *pathHelper) collectPathsTrait(t *Trait, elems []string, pathElem []string) { newElems := appendNew(elems, t.GetName()) newPath := appendNew(pathElem, t.GetFileName()) - if isOriginal { + if pc.SetLink { t.SetLink(elems, t.Kind) } - add(t, newElems, newPath, "trait", false) + pc.AddPathFunc(t, newElems, newPath, "trait", false) for _, e := range t.Fields { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#fields") - add(e, newElems, newPath, "member", true) + pc.AddPathFunc(e, newElems, newPath, "member", true) } for _, e := range t.Functions { newElems := appendNew(newElems, e.GetName()) newPath := appendNew(newPath, "#"+strings.ToLower(e.GetName())) - add(e, newElems, newPath, "member", true) + pc.AddPathFunc(e, newElems, newPath, "member", true) } } diff --git a/internal/document/processor.go b/internal/document/processor.go index ba526d2..e7a504c 100644 --- a/internal/document/processor.go +++ b/internal/document/processor.go @@ -46,11 +46,13 @@ func NewProcessor(docs *Docs, f Formatter, t *template.Template, config *Config) // NewProcessorWithWriter creates a new Processor instance with a custom writer. func NewProcessorWithWriter(docs *Docs, f Formatter, t *template.Template, config *Config, writer func(file, text string) error) *Processor { return &Processor{ - Config: config, - Template: t, - Formatter: f, - Docs: docs, - writer: writer, + Config: config, + Template: t, + Formatter: f, + Docs: docs, + writer: writer, + allPaths: map[string]Named{}, + linkTargets: map[string]elemPath{}, } } diff --git a/internal/document/rename.go b/internal/document/rename.go index 449524d..b441613 100644 --- a/internal/document/rename.go +++ b/internal/document/rename.go @@ -1,14 +1,19 @@ package document +type renameHelper struct { + rename map[string]string +} + func (proc *Processor) renameAll(p *Package) { - proc.renamePackage(p, p.Name) + r := renameHelper{rename: proc.renameExports} + r.renamePackage(p, p.Name) } -func (proc *Processor) renamePackage(p *Package, ownPath string) { +func (r *renameHelper) renamePackage(p *Package, ownPath string) { for i := range p.Packages { newPath := ownPath + "." + p.Packages[i].Name - proc.renamePackage(p.Packages[i], newPath) - if newName, ok := proc.renameExports[newPath]; ok { + r.renamePackage(p.Packages[i], newPath) + if newName, ok := r.rename[newPath]; ok { tempPkg := *p.Packages[i] tempPkg.MemberName = MemberName{Name: newName} p.Packages[i] = &tempPkg @@ -17,8 +22,8 @@ func (proc *Processor) renamePackage(p *Package, ownPath string) { for i := range p.Modules { newPath := ownPath + "." + p.Modules[i].Name - proc.renameModule(p.Modules[i], newPath) - if newName, ok := proc.renameExports[newPath]; ok { + r.renameModule(p.Modules[i], newPath) + if newName, ok := r.rename[newPath]; ok { tempMod := *p.Modules[i] tempMod.MemberName = MemberName{Name: newName} p.Modules[i] = &tempMod @@ -27,7 +32,7 @@ func (proc *Processor) renamePackage(p *Package, ownPath string) { for i := range p.Aliases { newPath := ownPath + "." + p.Aliases[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *p.Aliases[i] tempMod.MemberName = MemberName{Name: newName} p.Aliases[i] = &tempMod @@ -35,7 +40,7 @@ func (proc *Processor) renamePackage(p *Package, ownPath string) { } for i := range p.Structs { newPath := ownPath + "." + p.Structs[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *p.Structs[i] tempMod.MemberName = MemberName{Name: newName} p.Structs[i] = &tempMod @@ -43,7 +48,7 @@ func (proc *Processor) renamePackage(p *Package, ownPath string) { } for i := range p.Traits { newPath := ownPath + "." + p.Traits[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *p.Traits[i] tempMod.MemberName = MemberName{Name: newName} p.Traits[i] = &tempMod @@ -51,7 +56,7 @@ func (proc *Processor) renamePackage(p *Package, ownPath string) { } for i := range p.Functions { newPath := ownPath + "." + p.Functions[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *p.Functions[i] tempMod.MemberName = MemberName{Name: newName} p.Functions[i] = &tempMod @@ -59,10 +64,10 @@ func (proc *Processor) renamePackage(p *Package, ownPath string) { } } -func (proc *Processor) renameModule(m *Module, ownPath string) { +func (r *renameHelper) renameModule(m *Module, ownPath string) { for i := range m.Aliases { newPath := ownPath + "." + m.Aliases[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *m.Aliases[i] tempMod.MemberName = MemberName{Name: newName} m.Aliases[i] = &tempMod @@ -70,7 +75,7 @@ func (proc *Processor) renameModule(m *Module, ownPath string) { } for i := range m.Structs { newPath := ownPath + "." + m.Structs[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *m.Structs[i] tempMod.MemberName = MemberName{Name: newName} m.Structs[i] = &tempMod @@ -78,7 +83,7 @@ func (proc *Processor) renameModule(m *Module, ownPath string) { } for i := range m.Traits { newPath := ownPath + "." + m.Traits[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *m.Traits[i] tempMod.MemberName = MemberName{Name: newName} m.Traits[i] = &tempMod @@ -86,7 +91,7 @@ func (proc *Processor) renameModule(m *Module, ownPath string) { } for i := range m.Functions { newPath := ownPath + "." + m.Functions[i].Name - if newName, ok := proc.renameExports[newPath]; ok { + if newName, ok := r.rename[newPath]; ok { tempMod := *m.Functions[i] tempMod.MemberName = MemberName{Name: newName} m.Functions[i] = &tempMod