feat(cmd): add unified diff output option for formatting

* Implemented `-d`/`--diff` flag to print unified diffs.
* Added `unifiedDiff` function for generating diffs.
* Config discovery now merges fields from `.pgtidy.yaml`.
This commit is contained in:
2026-06-27 19:32:57 +02:00
parent d4e6102932
commit 932c83dbad
10 changed files with 345 additions and 11 deletions
+26
View File
@@ -0,0 +1,26 @@
# PgTidy house style — all fields shown with their default values.
# Place as .pgtidy.yaml in your project root (or any parent directory).
# Any field you omit keeps its default.
# Indentation string for one level (two spaces).
indent: " "
# Line terminator written by the formatter.
newline: "\n"
# Casing for SQL keywords (SELECT, FROM, WHERE, …).
# upper | lower | preserve
keyword_case: upper
# Casing for unquoted identifiers (column names, variable names, …).
# upper | lower | preserve
ident_case: lower
# Casing for built-in type names (text, integer, boolean, …).
# upper | lower | preserve
type_case: lower
# Comma placement in multi-line parameter / column lists.
# leading → comma at the start of the continuation line (,col)
# trailing → comma at the end of the preceding line (col,)
commas: leading
+7
View File
@@ -0,0 +1,7 @@
# Fully lowercase style — keywords, types, and identifiers all lowercased.
# Common in teams that prefer minimal visual noise.
keyword_case: lower
ident_case: lower
type_case: lower
commas: leading
+8
View File
@@ -0,0 +1,8 @@
# Preserve-case style — no casing changes applied.
# Useful for codebases with mixed-convention legacy SQL that you want to
# reformat structurally (indentation, commas) without touching casing.
keyword_case: preserve
ident_case: preserve
type_case: preserve
commas: leading
+7
View File
@@ -0,0 +1,7 @@
# Trailing-comma style — comma at the end of each line rather than the start.
# Matches the SQL style common in tools like dbt and some BI platforms.
keyword_case: upper
ident_case: lower
type_case: lower
commas: trailing
+7 -3
View File
@@ -69,8 +69,10 @@ Legend: ✅ done · 🚧 in progress · ⬜ not started
- **Status:** all tests pass; idempotence verified.
### ✅ Printer + style config — `pkg/format`, `pkg/config`
- `pkg/config`: `Style` struct + `Default()` = house style (UPPERCASE keywords, lowercase
types, 2-space indent, leading commas, spacing rules).
- `pkg/config`: `Style` struct + `Default()` = house style. `Load(startDir)` walks up the
directory tree to find `.pgtidy.yaml` and merges its fields over the defaults.
Supported keys: `indent`, `newline`, `keyword_case`, `ident_case`, `type_case`, `commas`.
Dependency: `gopkg.in/yaml.v3`.
- `pkg/format`: formats CREATE FUNCTION/PROCEDURE **headers** to house style (params
one-per-line leading-comma, option clauses each on own line, AS/`$$` own lines); DECLARE
section formatted (see body parser entry); `Raw` statements emitted verbatim.
@@ -85,7 +87,9 @@ Legend: ✅ done · 🚧 in progress · ⬜ not started
### ✅ CLI `fmt` + safety harness — `cmd/pgtidy`
- `pgtidy fmt` (gofmt model): default stdin→stdout; `-w`/`--write`, `-l`/`--list`,
`--check` (CI exit codes); `version`/`help`. _`.pgtidy.yaml` discovery + `-d` diff: TODO._
`--check` (CI exit codes); `-d`/`--diff` (unified diff output); `version`/`help`.
- Config discovery: walks up from cwd to find `.pgtidy.yaml`; applied before formatting.
- `diff.go`: in-house unified diff (LCS-based, zero additional deps).
- `fmt_test.go`: stdin, --check (un/formatted), -w idempotence, unknown-command.
- Safety invariants #2 (semantic equivalence), #3 (idempotence), #4 (graceful degradation)
are tested in `pkg/format` over the corpus.