Skip to content

Commit

Permalink
Split up Processor mega-struct (#219)
Browse files Browse the repository at this point in the history
* move collecting paths to a separate type
* move renaming to a separate type
  • Loading branch information
mlange-42 authored Feb 9, 2025
1 parent 8b2bd88 commit c08a383
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 55 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/document/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
81 changes: 47 additions & 34 deletions internal/document/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 7 additions & 5 deletions internal/document/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
}

Expand Down
35 changes: 20 additions & 15 deletions internal/document/rename.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -27,66 +32,66 @@ 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
}
}
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
}
}
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
}
}
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
}
}
}

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
}
}
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
}
}
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
}
}
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
Expand Down

0 comments on commit c08a383

Please sign in to comment.