Skip to content

Commit

Permalink
astprinter: opts. Print oneline with spaces
Browse files Browse the repository at this point in the history
commit-id:9fade0ff
  • Loading branch information
kolia-kaploniuk committed Jul 19, 2024
1 parent 6965092 commit bf4eb13
Showing 1 changed file with 74 additions and 17 deletions.
91 changes: 74 additions & 17 deletions pkg/astprinter/astprinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,33 @@ import (
"github.com/wundergraph/graphql-go-tools/pkg/lexer/literal"
)

func WithIdent(ident []byte) func(p *Printer) {
return func(p *Printer) {
p.indent = ident
}
}

func WithSpace() func(p *Printer) {
return func(p *Printer) {
p.space = true
}
}

func WithNoDescription() func(p *Printer) {
return func(p *Printer) {
p.skipDescription = true
}
}

// Print takes a document as well as a definition (optional) and prints it to the io.Writer.
// The definition is only necessary in case a GraphQL Operation should be printed.
func Print(document, definition *ast.Document, out io.Writer) error {
func Print(document, definition *ast.Document, out io.Writer, options ...func(*Printer)) error {
printer := Printer{}

for _, o := range options {
o(&printer)
}

return printer.Print(document, definition, out)
}

Expand All @@ -26,21 +49,9 @@ func PrintIndent(document, definition *ast.Document, indent []byte, out io.Write
}

// PrintString is the same as Print but returns a string instead of writing to an io.Writer
func PrintString(document, definition *ast.Document) (string, error) {
func PrintString(document, definition *ast.Document, options ...func(*Printer)) (string, error) {
buff := &bytes.Buffer{}
err := Print(document, definition, buff)
out := buff.String()
return out, err
}

// PrintStringNoDescription is the same as PrintString but doesn't print descriptions
func PrintStringNoDescription(document, definition *ast.Document) (string, error) {
printer := Printer{
skipDescription: true,
}

buff := &bytes.Buffer{}
err := printer.Print(document, definition, buff)
err := Print(document, definition, buff, options...)
out := buff.String()

return out, err
Expand All @@ -57,6 +68,7 @@ func PrintStringIndent(document, definition *ast.Document, indent string) (strin
// Printer walks a GraphQL document and prints it as a string
type Printer struct {
indent []byte
space bool // space and indent cannot be used together
skipDescription bool

visitor printVisitor
Expand All @@ -69,6 +81,7 @@ type Printer struct {
func (p *Printer) Print(document, definition *ast.Document, out io.Writer) error {
p.visitor.indent = p.indent
p.visitor.skipDescription = p.skipDescription
p.visitor.space = p.space
p.visitor.err = nil
p.visitor.document = document
p.visitor.out = out
Expand All @@ -91,6 +104,7 @@ type printVisitor struct {
isFirstDirectiveLocation bool
isDirectiveRepeatable bool
skipDescription bool
space bool
}

func (p *printVisitor) write(data []byte) {
Expand Down Expand Up @@ -128,9 +142,9 @@ func (p *printVisitor) writeIndented(data []byte) {
}
depth := p.indentationDepth()
for i := 0; i < depth; i++ {
_, p.err = p.out.Write(p.indent)
p.write(p.indent)
}
_, p.err = p.out.Write(data)
p.write(data)
}

func (p *printVisitor) must(err error) {
Expand Down Expand Up @@ -223,6 +237,9 @@ func (p *printVisitor) EnterVariableDefinition(ref int) {
func (p *printVisitor) LeaveVariableDefinition(ref int) {
if !p.document.VariableDefinitionsAfter(ref) {
p.write(literal.RPAREN)
if p.space && p.indent == nil {
p.write(literal.SPACE)
}
} else {
p.write(literal.COMMA)
p.write(literal.SPACE)
Expand All @@ -242,6 +259,13 @@ func (p *printVisitor) EnterArgument(ref int) {
func (p *printVisitor) LeaveArgument(ref int) {
if len(p.document.ArgumentsAfter(p.Ancestors[len(p.Ancestors)-1], ref)) == 0 {
p.write(literal.RPAREN)

if p.space && p.indent == nil {
if len(p.Ancestors) > 0 && p.Ancestors[len(p.Ancestors)-1].Kind == ast.NodeKindField &&
p.document.FieldHasSelections(p.Ancestors[len(p.Ancestors)-1].Ref) {
p.write(literal.SPACE)
}
}
}
}

Expand Down Expand Up @@ -289,12 +313,26 @@ func (p *printVisitor) EnterSelectionSet(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}

if p.space && p.indent == nil {
// if selectionSet has something inside
if len(p.document.SelectionSets[ref].SelectionRefs) != 0 {
p.write(literal.SPACE)
}
}
}

func (p *printVisitor) LeaveSelectionSet(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}

if p.space && p.indent == nil {
// if selectionSet has something inside
if len(p.document.SelectionSets[ref].SelectionRefs) != 0 {
p.write(literal.SPACE)
}
}
p.writeIndented(literal.RBRACE)
}

Expand Down Expand Up @@ -462,6 +500,9 @@ func (p *printVisitor) EnterFieldDefinition(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}
if p.space && p.indent == nil {
p.write(literal.SPACE)
}
}
if p.document.FieldDefinitions[ref].Description.IsDefined && !p.skipDescription {
p.must(p.document.PrintDescription(p.document.FieldDefinitions[ref].Description, p.indent, p.indentationDepth(), p.out))
Expand All @@ -479,6 +520,10 @@ func (p *printVisitor) LeaveFieldDefinition(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}

if p.space && p.indent == nil {
p.write(literal.SPACE)
}
p.write(literal.RBRACE)
} else {
if p.indent != nil {
Expand Down Expand Up @@ -799,6 +844,9 @@ func (p *printVisitor) LeaveEnumValueDefinition(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}
if p.space && p.indent == nil {
p.write(literal.SPACE)
}
p.write(literal.RBRACE)
} else {
if p.indent != nil {
Expand Down Expand Up @@ -970,6 +1018,9 @@ func (p *printVisitor) LeaveSchemaDefinition(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}
if p.space && p.indent == nil {
p.write(literal.SPACE)
}
p.write(literal.RBRACE)
if !p.document.NodeIsLastRootNode(ast.Node{Kind: ast.NodeKindSchemaDefinition, Ref: ref}) {
if p.indent != nil {
Expand All @@ -992,6 +1043,9 @@ func (p *printVisitor) LeaveSchemaExtension(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}
if p.space && p.indent == nil {
p.write(literal.SPACE)
}
p.write(literal.RBRACE)
if !p.document.NodeIsLastRootNode(ast.Node{Kind: ast.NodeKindSchemaExtension, Ref: ref}) {
if p.indent != nil {
Expand All @@ -1009,6 +1063,9 @@ func (p *printVisitor) EnterRootOperationTypeDefinition(ref int) {
if p.indent != nil {
p.write(literal.LINETERMINATOR)
}
if p.space && p.indent == nil {
p.write(literal.SPACE)
}
}
switch p.document.RootOperationTypeDefinitions[ref].OperationType {
case ast.OperationTypeQuery:
Expand Down

0 comments on commit bf4eb13

Please sign in to comment.