-
Notifications
You must be signed in to change notification settings - Fork 131
/
ast_description.go
94 lines (83 loc) · 2.21 KB
/
ast_description.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package ast
import (
"io"
"strings"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/literal"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/position"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/runes"
)
type Description struct {
IsDefined bool
IsBlockString bool // true if -> """content""" ; else "content"
Content ByteSliceReference // literal
Position position.Position
}
// nolint
func (d *Document) PrintDescription(description Description, indent []byte, depth int, writer io.Writer) (err error) {
for i := 0; i < depth; i++ {
_, err = writer.Write(indent)
}
if description.IsBlockString {
_, err = writer.Write(literal.QUOTE)
_, err = writer.Write(literal.QUOTE)
_, err = writer.Write(literal.QUOTE)
_, err = writer.Write(literal.LINETERMINATOR)
for i := 0; i < depth; i++ {
_, err = writer.Write(indent)
}
} else {
_, err = writer.Write(literal.QUOTE)
}
content := d.Input.ByteSlice(description.Content)
skipWhitespace := false
skippedWhitespace := 0.0
depthToSkip := float64(depth)
for i := range content {
if skipWhitespace && skippedWhitespace < depthToSkip {
switch content[i] {
case runes.TAB:
skippedWhitespace += 1
continue
case runes.SPACE:
skippedWhitespace += 0.5
continue
}
}
switch content[i] {
case runes.LINETERMINATOR:
skipWhitespace = true
default:
if skipWhitespace {
for j := 0; j < depth; j++ {
_, err = writer.Write(indent)
}
skipWhitespace = false
skippedWhitespace = 0.0
}
}
_, err = writer.Write(content[i : i+1])
}
if description.IsBlockString {
_, err = writer.Write(literal.LINETERMINATOR)
for i := 0; i < depth; i++ {
_, err = writer.Write(indent)
}
_, err = writer.Write(literal.QUOTE)
_, err = writer.Write(literal.QUOTE)
_, err = writer.Write(literal.QUOTE)
} else {
_, err = writer.Write(literal.QUOTE)
}
return nil
}
func (d *Document) ImportDescription(desc string) (description Description) {
if desc == "" {
return
}
isBlockString := strings.Contains(desc, "\n") || strings.Contains(desc, `"`)
return Description{
IsDefined: true,
IsBlockString: isBlockString,
Content: d.Input.AppendInputString(desc),
}
}