Maintain inheritance from merging classes

This commit is contained in:
2025-07-20 18:50:38 +02:00
parent 3821952bbf
commit c8961471e4
3 changed files with 57 additions and 1 deletions

View File

@@ -52,6 +52,7 @@ func init() {
type ( type (
Class struct { Class struct {
ClassName string ClassName string
Inheritance string
Fields []Field Fields []Field
Methods []Method Methods []Method
Constructors []Constructor Constructors []Constructor

View File

@@ -1,6 +1,6 @@
--luacheck: ignore 212 111 --luacheck: ignore 212 111
---@diagnostic disable: missing-return, lowercase-global ---@diagnostic disable: missing-return, lowercase-global
---@class {{.ClassName}} ---@class {{.ClassName}}{{if ne .Inheritance ""}} : {{.Inheritance}}{{end}}
{{- range .Fields}} {{- range .Fields}}
---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} ---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{- end}} {{- end}}

55
main.go
View File

@@ -3,12 +3,14 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
_ "embed" _ "embed"
logger "git.site.quack-lab.dev/dave/cylogger" logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/PuerkitoBio/goquery"
) )
func main() { func main() {
@@ -152,6 +154,7 @@ func MergeClasses(files []string) (*Class, error) {
// Parse all classes // Parse all classes
var classes []*Class var classes []*Class
var baseName string var baseName string
var inheritance string
for _, file := range files { for _, file := range files {
class, err := ParseClass(file) class, err := ParseClass(file)
@@ -164,11 +167,42 @@ func MergeClasses(files []string) (*Class, error) {
if baseName == "" { if baseName == "" {
baseName = class.ClassName baseName = class.ClassName
} }
// Check for inheritance information in the original file
originalClassName := getOriginalClassName(file)
if strings.Contains(originalClassName, " : ") {
parts := strings.Split(originalClassName, " : ")
if len(parts) == 2 {
// Extract the inherited class name and clean it
inheritedClass := strings.TrimSpace(parts[1])
inheritedClass = strings.ReplaceAll(inheritedClass, "[Client]", "")
inheritedClass = strings.ReplaceAll(inheritedClass, "[Server]", "")
inheritedClass = strings.ReplaceAll(inheritedClass, "[", "")
inheritedClass = strings.ReplaceAll(inheritedClass, "]", "")
inheritedClass = strings.ReplaceAll(inheritedClass, "-", "_")
inheritedClass = strings.ReplaceAll(inheritedClass, ",", "")
inheritedClass = strings.ReplaceAll(inheritedClass, " ", "_")
// Clean up multiple underscores
for strings.Contains(inheritedClass, "__") {
inheritedClass = strings.ReplaceAll(inheritedClass, "__", "_")
}
inheritedClass = strings.Trim(inheritedClass, "_")
if inheritance == "" {
inheritance = inheritedClass
}
}
}
} }
// Merge all classes into the first one // Merge all classes into the first one
merged := classes[0] merged := classes[0]
// Set inheritance if found
if inheritance != "" {
merged.Inheritance = inheritance
}
// Create maps to track methods and fields by name // Create maps to track methods and fields by name
methodMap := make(map[string]*Method) methodMap := make(map[string]*Method)
fieldMap := make(map[string]*Field) fieldMap := make(map[string]*Field)
@@ -212,3 +246,24 @@ func MergeClasses(files []string) (*Class, error) {
return merged, nil return merged, nil
} }
// getOriginalClassName extracts the original class name from the HTML file
func getOriginalClassName(file string) string {
filehandle, err := os.Open(file)
if err != nil {
return ""
}
defer filehandle.Close()
doc, err := goquery.NewDocumentFromReader(filehandle)
if err != nil {
return ""
}
class := doc.Find("div.floatright > h1")
if class.Length() == 0 {
return ""
}
return strings.TrimSpace(class.Text())
}