Skip to content

Commit 64781fc

Browse files
Copilotgit-hulk
andcommitted
Add chaining methods WithBeautify and WithIdent to formatter
Co-authored-by: git-hulk <4987594+git-hulk@users.noreply.github.com>
1 parent b0054cc commit 64781fc

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

parser/format.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,33 @@ type Formatter struct {
2121
mode FormatMode
2222
indentLevel int
2323
lineStart bool
24+
ident string
2425
}
2526

2627
func NewFormatter() *Formatter {
2728
return &Formatter{
2829
mode: FormatModeCompact,
2930
lineStart: true,
31+
ident: " ",
3032
}
3133
}
3234

33-
func (f *Formatter) WithBeautify() {
35+
func (f *Formatter) WithBeautify() *Formatter {
3436
f.mode = FormatModeBeautify
37+
return f
38+
}
39+
40+
func (f *Formatter) WithIdent(ident string) *Formatter {
41+
f.ident = ident
42+
return f
3543
}
3644

3745
func (f *Formatter) writeIndentIfNeeded() {
3846
if !f.lineStart {
3947
return
4048
}
4149
for i := 0; i < f.indentLevel; i++ {
42-
f.builder.WriteString(" ")
50+
f.builder.WriteString(f.ident)
4351
}
4452
f.lineStart = false
4553
}
@@ -71,7 +79,6 @@ func (f *Formatter) WriteExpr(expr Expr) {
7179
expr.FormatSQL(f)
7280
}
7381

74-
7582
func (f *Formatter) NewLine() {
7683
if f.mode != FormatModeBeautify {
7784
return

parser/format_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package parser
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestFormatter_WithBeautify_Chaining(t *testing.T) {
10+
// Test that WithBeautify returns the formatter for chaining
11+
formatter := NewFormatter().WithBeautify()
12+
require.NotNil(t, formatter)
13+
require.Equal(t, FormatModeBeautify, formatter.mode)
14+
}
15+
16+
func TestFormatter_WithIdent_Chaining(t *testing.T) {
17+
// Test that WithIdent returns the formatter for chaining
18+
formatter := NewFormatter().WithIdent(" ")
19+
require.NotNil(t, formatter)
20+
require.Equal(t, " ", formatter.ident)
21+
}
22+
23+
func TestFormatter_ChainedMethods(t *testing.T) {
24+
// Test that methods can be chained together
25+
formatter := NewFormatter().WithBeautify().WithIdent("\t")
26+
require.NotNil(t, formatter)
27+
require.Equal(t, FormatModeBeautify, formatter.mode)
28+
require.Equal(t, "\t", formatter.ident)
29+
}
30+
31+
func TestFormatter_WithIdent_CustomIndentation(t *testing.T) {
32+
// Test actual formatting with custom indent using parsed SQL
33+
sql := "SELECT col1, col2 FROM table1 WHERE col1 > 10"
34+
35+
parser := NewParser(sql)
36+
stmts, err := parser.ParseStmts()
37+
require.NoError(t, err)
38+
require.Len(t, stmts, 1)
39+
40+
// Test with default 2-space indent
41+
formatter1 := NewFormatter().WithBeautify()
42+
formatter1.WriteExpr(stmts[0])
43+
result1 := formatter1.String()
44+
45+
// Test with 4-space indent
46+
formatter2 := NewFormatter().WithBeautify().WithIdent(" ")
47+
formatter2.WriteExpr(stmts[0])
48+
result2 := formatter2.String()
49+
50+
// Test with tab indent
51+
formatter3 := NewFormatter().WithBeautify().WithIdent("\t")
52+
formatter3.WriteExpr(stmts[0])
53+
result3 := formatter3.String()
54+
55+
// Verify all results are different (due to different indentation)
56+
require.NotEqual(t, result1, result2)
57+
require.NotEqual(t, result1, result3)
58+
require.NotEqual(t, result2, result3)
59+
60+
// Verify they all contain the basic SQL keywords
61+
require.Contains(t, result1, "SELECT")
62+
require.Contains(t, result2, "SELECT")
63+
require.Contains(t, result3, "SELECT")
64+
require.Contains(t, result1, "FROM")
65+
require.Contains(t, result2, "FROM")
66+
require.Contains(t, result3, "FROM")
67+
}
68+
69+
func TestFormatter_DefaultIdent(t *testing.T) {
70+
// Test that default indent is 2 spaces
71+
formatter := NewFormatter()
72+
require.Equal(t, " ", formatter.ident)
73+
}

0 commit comments

Comments
 (0)