Skip to content

Commit 3b8db28

Browse files
Copilotgit-hulk
andauthored
Add chaining methods to formatter for customizable indentation (#248)
--------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: git-hulk <4987594+git-hulk@users.noreply.github.com>
1 parent 4b92d70 commit 3b8db28

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

parser/format.go

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

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

33-
func (f *Formatter) WithBeautify() {
35+
func (f *Formatter) WithBeautify() *Formatter {
3436
f.mode = FormatModeBeautify
37+
return f
38+
}
39+
40+
// WithIndent sets the indentation string used when beautifying SQL.
41+
// The indent parameter should not be empty to maintain proper formatting.
42+
func (f *Formatter) WithIndent(indent string) *Formatter {
43+
f.indent = indent
44+
return f
3545
}
3646

3747
func (f *Formatter) writeIndentIfNeeded() {
3848
if !f.lineStart {
3949
return
4050
}
4151
for i := 0; i < f.indentLevel; i++ {
42-
f.builder.WriteString(" ")
52+
f.builder.WriteString(f.indent)
4353
}
4454
f.lineStart = false
4555
}

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_WithIndent_Chaining(t *testing.T) {
17+
// Test that WithIndent returns the formatter for chaining
18+
formatter := NewFormatter().WithIndent(" ")
19+
require.NotNil(t, formatter)
20+
require.Equal(t, " ", formatter.indent)
21+
}
22+
23+
func TestFormatter_ChainedMethods(t *testing.T) {
24+
// Test that methods can be chained together
25+
formatter := NewFormatter().WithBeautify().WithIndent("\t")
26+
require.NotNil(t, formatter)
27+
require.Equal(t, FormatModeBeautify, formatter.mode)
28+
require.Equal(t, "\t", formatter.indent)
29+
}
30+
31+
func TestFormatter_WithIndent_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().WithIndent(" ")
47+
formatter2.WriteExpr(stmts[0])
48+
result2 := formatter2.String()
49+
50+
// Test with tab indent
51+
formatter3 := NewFormatter().WithBeautify().WithIndent("\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_DefaultIndent(t *testing.T) {
70+
// Test that default indent is 2 spaces
71+
formatter := NewFormatter()
72+
require.Equal(t, " ", formatter.indent)
73+
}

0 commit comments

Comments
 (0)