Skip to content

Commit

Permalink
formatter: support setting space indent (#208)
Browse files Browse the repository at this point in the history
* formatter: support setting space indent

* fix: change SpaceIndent to Indent
  • Loading branch information
giautm committed Mar 26, 2022
1 parent 931cd76 commit 2c7bcc9
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,30 @@ type Formatter interface {
FormatQueryDocument(doc *ast.QueryDocument)
}

func NewFormatter(w io.Writer) Formatter {
return &formatter{writer: w}
type FormatterOption func(*formatter)

func WithIndent(indent string) FormatterOption {
return func(f *formatter) {
f.indent = indent
}
}

func NewFormatter(w io.Writer, options ...FormatterOption) Formatter {
f := &formatter{
indent: "\t",
writer: w,
}
for _, opt := range options {
opt(f)
}
return f
}

type formatter struct {
writer io.Writer

indent int
indent string
indentSize int
emitBuiltin bool

padNext bool
Expand All @@ -35,7 +51,7 @@ func (f *formatter) writeString(s string) {

func (f *formatter) writeIndent() *formatter {
if f.lineHead {
f.writeString(strings.Repeat("\t", f.indent))
f.writeString(strings.Repeat(f.indent, f.indentSize))
}
f.lineHead = false
f.padNext = false
Expand Down Expand Up @@ -95,11 +111,11 @@ func (f *formatter) WriteDescription(s string) *formatter {
}

func (f *formatter) IncrementIndent() {
f.indent++
f.indentSize++
}

func (f *formatter) DecrementIndent() {
f.indent--
f.indentSize--
}

func (f *formatter) NoPadding() *formatter {
Expand Down

0 comments on commit 2c7bcc9

Please sign in to comment.