forked from jackc/sqlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_renderer.go
46 lines (38 loc) · 977 Bytes
/
token_renderer.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
package sqlfmt
import (
"bytes"
)
type TokenRenderer []RenderToken
func (r *TokenRenderer) Text(val string, tokenType int) {
*r = TokenRenderer(append([]RenderToken(*r), RenderToken{Type: tokenType, Value: val}))
}
func (r *TokenRenderer) Control(tokenType int) {
*r = TokenRenderer(append([]RenderToken(*r), RenderToken{Type: tokenType}))
}
func RenderTokens(r Renderer, tokens []RenderToken) {
for _, t := range tokens {
if t.Value != "" {
r.Text(t.Value, t.Type)
} else {
r.Control(t.Type)
}
}
}
func TryOneLine(tokens []RenderToken, maxLineLength int) []RenderToken {
buf := &bytes.Buffer{}
r := NewTextRenderer(buf)
RenderTokens(r, tokens)
if buf.Len() < maxLineLength {
filteredTokens := []RenderToken{}
for _, t := range tokens {
switch t.Type {
case NewLineToken, IndentToken, UnindentToken:
// filter these out
default:
filteredTokens = append(filteredTokens, t)
}
}
tokens = filteredTokens
}
return tokens
}