-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
79 lines (64 loc) · 2.05 KB
/
cache.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
// jsonnet-language-server: A Language Server Protocol server for Jsonnet.
// Copyright (C) 2021 Jack Baldry
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"errors"
"sync"
"github.com/google/go-jsonnet/ast"
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
)
type document struct {
item protocol.TextDocumentItem
val string
ast ast.Node
err error
// Symbols are hierarchical and there is only ever a single root symbol.
symbols protocol.DocumentSymbol
}
// newCache returns a document cache.
func newCache() *cache {
return &cache{
mu: sync.RWMutex{},
docs: make(map[protocol.DocumentURI]document),
}
}
// cache caches documents.
type cache struct {
mu sync.RWMutex
docs map[protocol.DocumentURI]document
}
// put adds or replaces a document in the cache.
// Documents are only replaced if the new document version is greater than the currently
// cached version.
func (c *cache) put(new document) error {
c.mu.Lock()
defer c.mu.Unlock()
uri := new.item.URI
if old, ok := c.docs[uri]; ok {
if old.item.Version > new.item.Version {
return errors.New("newer version of the document is already in the cache")
}
}
c.docs[uri] = new
return nil
}
// get retrieves a document from the cache.
func (c *cache) get(uri protocol.DocumentURI) (document, error) {
c.mu.RLock()
defer c.mu.RUnlock()
doc, ok := c.docs[uri]
if !ok {
return document{}, errors.New("document not found")
}
return doc, nil
}