turbo-editors/turbo-corepublic Fork 0
28d59854361aeda8541d853093e732126f3d7bff
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

cache.go · 68 lines · 2.0 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 20h ago1package syntax
2
3// Cache holds the spans of one source text and re-scans it only when the text
4// has actually changed.
5//
6// Scanning a file is cheap but not free, and the editor redraws far more often
7// than the text changes — every cursor move, every scroll. The revision number
8// the buffer hands out is what tells the two apart.
9//
10// cache.Update(buf.Text(), buf.Revision())
11// for line := top; line < bottom; line++ {
12// draw(line, cache.Line(line))
13// }
14type Cache struct {
15 language Language
16 scanned bool
17 revision int
18 lines [][]Span
19}
20
21// NewCache returns a cache that colours one language, or one that colours
22// nothing for LanguageNone — which is what a file this package does not
23// understand gets.
24//
25// cache := syntax.NewCache(syntax.LanguageOf(path))
26func NewCache(language Language) *Cache {
27 return &Cache{language: language}
28}
29
30// Language returns the language this cache colours.
31func (c *Cache) Language() Language { return c.language }
32
33// Enabled reports whether this cache colours anything.
34func (c *Cache) Enabled() bool { return c.language != LanguageNone }
35
36// SetLanguage changes what is coloured, discarding what was scanned. Call it
37// when the window's file changes, as Save As does.
38func (c *Cache) SetLanguage(language Language) {
39 c.language = language
40 c.scanned = false
41 c.lines = nil
42}
43
44// Update re-scans src when revision differs from the one already scanned.
45func (c *Cache) Update(src string, revision int) {
46 if !c.Enabled() {
47 return
48 }
49 if c.scanned && c.revision == revision {
50 return
51 }
52
53 c.lines = Highlight(c.language, src)
54 c.revision = revision
55 c.scanned = true
56}
57
58// Line returns the spans of line i from the last scan, or nil when there are
59// none — an empty line, a line past the end, or colouring turned off.
60func (c *Cache) Line(i int) []Span {
61 if i < 0 || i >= len(c.lines) {
62 return nil
63 }
64 return c.lines[i]
65}
66
67// LineCount returns how many lines the last scan covered.
68func (c *Cache) LineCount() int { return len(c.lines) }