-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils.go
101 lines (82 loc) · 2.32 KB
/
utils.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
95
96
97
98
99
100
101
package gohtml
import (
"bytes"
"strings"
"unicode"
)
type formatterTokenType int
const (
formatterTokenType_Nothing formatterTokenType = iota
formatterTokenType_Tag
formatterTokenType_Text
)
type formattedBuffer struct {
buffer *bytes.Buffer
rawMode bool
indentString string
indentLevel int
lineWrapColumn int
lineWrapMaxSpillover int
curLineLength int
prevTokenType formatterTokenType
}
func (bf *formattedBuffer) writeLineFeed() {
if !bf.rawMode {
// Strip trailing newlines
bf.buffer = bytes.NewBuffer(bytes.TrimRightFunc(
bf.buffer.Bytes(),
func(r rune) bool {
return r != '\n' && unicode.IsSpace(r)
},
))
}
bf.buffer.WriteString("\n")
bf.curLineLength = 0
bf.prevTokenType = formatterTokenType_Nothing
}
func (bf *formattedBuffer) writeIndent() {
bf.buffer.WriteString(strings.Repeat(bf.indentString, bf.indentLevel))
bf.curLineLength += len(bf.indentString) * bf.indentLevel
}
func (bf *formattedBuffer) writeToken(token string, kind formatterTokenType) {
if bf.rawMode {
bf.buffer.WriteString(token)
bf.curLineLength += len(token)
return
}
if bf.prevTokenType == formatterTokenType_Nothing && strings.TrimSpace(token) == "" {
// It's a whitespace token, but we already have indentation which functions
// the same, so we ignore it
return
}
toWrite := token
if kind == formatterTokenType_Text && bf.prevTokenType == formatterTokenType_Text {
toWrite = " " + token
}
if bf.prevTokenType != formatterTokenType_Nothing && bf.lineWrapColumn > 0 {
switch {
case bf.curLineLength > bf.lineWrapColumn:
// Current line is too long
fallthrough
case bf.curLineLength+len(toWrite) > bf.lineWrapColumn+bf.lineWrapMaxSpillover:
// Current line + new token is too long even with allowed spillover
fallthrough
case bf.curLineLength+len(toWrite) > bf.lineWrapColumn &&
bf.curLineLength > bf.lineWrapColumn-bf.lineWrapMaxSpillover:
// Current line + new token is too long and doesn't quality for spillover
bf.writeLineFeed()
bf.writeToken(token, kind)
return
}
}
if bf.curLineLength == 0 {
bf.writeIndent()
}
bf.buffer.WriteString(toWrite)
bf.curLineLength += len(toWrite)
bf.prevTokenType = kind
}
// unifyLineFeed unifies line feeds.
func unifyLineFeed(s string) string {
return strings.Replace(strings.Replace(s, "\r\n", "\n", -1), "\r", "\n", -1)
}