From d3d93034b97774f265f43ea04be31dfc27c5bf3c Mon Sep 17 00:00:00 2001 From: Genqila <102831561+genqila@users.noreply.github.com> Date: Sun, 28 Dec 2025 02:16:30 +0700 Subject: [PATCH] style: autoformat codegen files --- .vscode/settings.json | 12 ++++++------ cmd/txttocode/main.go | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8d97a2d..55d761b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,15 +1,15 @@ { "go.formatTool": "default", - "[json]": { - "editor.formatOnSave": true, - }, - "[jsonc]": { - "editor.formatOnSave": true, - }, "[go]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": "always" } + }, + "[json]": { + "editor.formatOnSave": true, + }, + "[jsonc]": { + "editor.formatOnSave": true, } } \ No newline at end of file diff --git a/cmd/txttocode/main.go b/cmd/txttocode/main.go index 7c61bb9..efc7f8f 100644 --- a/cmd/txttocode/main.go +++ b/cmd/txttocode/main.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "go/format" "os" "regexp" "strings" @@ -117,13 +118,21 @@ func generateFile(sourcePath, destinationPath, tpl string) error { t := template.Must(template.New("tpl").Funcs(funcMap).Parse(tpl)) - file, err := os.Create(destinationPath) - if err != nil { + var b bytes.Buffer + if err := t.Execute(&b, fileContent); err != nil { return err } - defer file.Close() - return t.Execute(file, fileContent) + output := b.Bytes() + if strings.HasSuffix(destinationPath, ".go") { + formatted, err := format.Source(output) + if err != nil { + return err + } + output = formatted + } + + return os.WriteFile(destinationPath, output, 0644) } func generateItems() error { @@ -217,9 +226,9 @@ func generateItems() error { } itemsFileContent += "}" - err = os.WriteFile("pkg/data/item/items.go", []byte(itemsFileContent), 0644) + formatted, err := format.Source([]byte(itemsFileContent)) if err != nil { return err } - return nil + return os.WriteFile("pkg/data/item/items.go", formatted, 0644) }