/
simple.go
56 lines (47 loc) · 1.09 KB
/
simple.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
package prettier
// simpleDoc is a concatenation of items,
// where each item is either a text
// or a line break indented a given amount
//
type simpleDoc interface {
isSimpleDoc()
}
// simpleText is a concatenation of some text
// and a (lazily, and cached) next item
//
type simpleText struct {
text string
next *simpleDocCache
}
var _ simpleDoc = simpleText{}
func (simpleText) isSimpleDoc() {}
// simpleLine is line break indented a given amount
// and a (lazily, and cached) next item
//
type simpleLine struct {
indent int
next *simpleDocCache
}
var _ simpleDoc = simpleLine{}
func (simpleLine) isSimpleDoc() {}
// simpleDocCache is a lazily computed
// and cached simple document.
//
// Construct with the getter.
// Use get to compute and cache the document
//
type simpleDocCache struct {
doc simpleDoc
getter func() simpleDoc
}
func (c *simpleDocCache) get() simpleDoc {
// If the getter is still set,
// then use it to compute the document,
// cache the result, and indicate the
// computation is done
if c.getter != nil {
c.doc = c.getter()
c.getter = nil
}
return c.doc
}