-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
274 lines (247 loc) · 9.04 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package server
import (
"context"
"encoding/json"
"net/url"
"github.com/huderlem/poryscript-pls/parse"
"github.com/huderlem/poryscript/parser"
)
// Gets the aggregate list of Commands from the collection of files that define
// the Commands. The Commands are cached for the given file uri so that parsing is
// avoided in future calls.
func (s *poryscriptServer) getCommands(ctx context.Context, uri string) (map[string]parse.Command, error) {
settings, err := s.config.GetFileSettings(ctx, s.connection, uri)
if err != nil {
return nil, err
}
// Aggregate a slice of Commands from all of the relevant files.
commands := map[string]parse.Command{}
for _, includeFilepath := range settings.CommandIncludes {
fileCommands, err := s.getCommandsInFile(ctx, includeFilepath)
if err != nil {
// TODO: log error? we don't want to fail if a single file resulted in an error.
continue
}
for _, command := range fileCommands {
commands[command.Name] = command
}
}
for _, command := range parse.KeywordCommands {
commands[command.Name] = command
}
return commands, nil
}
// Gets the list of Commands from the given file uri. The Commands
// are cached for the file so that parsing is avoided in future
// calls.
func (s *poryscriptServer) getCommandsInFile(ctx context.Context, uri string) (map[string]parse.Command, error) {
s.commandsMutex.Lock()
defer s.commandsMutex.Unlock()
uri, _ = url.QueryUnescape(uri)
if commands, ok := s.cachedCommands[uri]; ok {
return commands, nil
}
return s.getAndCacheCommandsInFile(ctx, uri)
}
// Fetches and caches the Commands from the given file uri.
func (s *poryscriptServer) getAndCacheCommandsInFile(ctx context.Context, uri string) (map[string]parse.Command, error) {
uri, _ = url.QueryUnescape(uri)
var content string
if err := s.connection.Call(ctx, "poryscript/readfile", uri, &content); err != nil {
return nil, err
}
if !s.config.HasWorkspaceFolderCapability {
return nil, nil
}
commands := parse.ParseCommands(content)
commandSet := map[string]parse.Command{}
for _, c := range commands {
commandSet[c.Name] = c
}
s.cachedCommands[uri] = commandSet
return commandSet, nil
}
func (s *poryscriptServer) getAutovarCommands(ctx context.Context, uri string) (parser.CommandConfig, error) {
s.commandConfigMutex.Lock()
defer s.commandConfigMutex.Unlock()
uri, _ = url.QueryUnescape(uri)
if autovarCommands, ok := s.cachedAutovarCommands[uri]; ok {
return autovarCommands, nil
}
return s.getAndCacheAutovarCommands(ctx, uri)
}
func (s *poryscriptServer) getAndCacheAutovarCommands(ctx context.Context, uri string) (parser.CommandConfig, error) {
settings, err := s.config.GetFileSettings(ctx, s.connection, uri)
if err != nil {
return parser.CommandConfig{}, err
}
if len(settings.CommandConfigFilepath) == 0 {
return parser.CommandConfig{}, nil
}
commandConfigUri, _ := url.QueryUnescape(settings.CommandConfigFilepath)
var content string
if err := s.connection.Call(ctx, "poryscript/readfile", commandConfigUri, &content); err != nil {
return parser.CommandConfig{}, err
}
if !s.config.HasWorkspaceFolderCapability {
return parser.CommandConfig{}, nil
}
var config parser.CommandConfig
if err := json.Unmarshal([]byte(content), &config); err != nil {
return parser.CommandConfig{}, err
}
return config, nil
}
// Gets the list of poryscript constants from the given file uri. The constants
// are cached for the file so that parsing is avoided in future calls.
func (s *poryscriptServer) getConstantsInFile(ctx context.Context, uri string) (map[string]parse.ConstantSymbol, error) {
s.constantsMutex.Lock()
defer s.constantsMutex.Unlock()
uri, _ = url.QueryUnescape(uri)
if constants, ok := s.cachedConstants[uri]; ok {
return constants, nil
}
return s.getAndCacheConstantsInFile(ctx, uri)
}
// Fetches and caches the poryscript constants from the given file uri.
func (s *poryscriptServer) getAndCacheConstantsInFile(ctx context.Context, uri string) (map[string]parse.ConstantSymbol, error) {
uri, _ = url.QueryUnescape(uri)
content, err := s.getDocumentContent(ctx, uri)
if err != nil {
return nil, err
}
constants := parse.ParseConstants(content, uri)
constantSet := map[string]parse.ConstantSymbol{}
for _, c := range constants {
constantSet[c.Name] = c
}
s.cachedConstants[uri] = constantSet
return constantSet, nil
}
// Gets the list of poryscript symbols from the given file uri. The symbols
// are cached for the file so that parsing is avoided in future calls.
func (s *poryscriptServer) getSymbolsInFile(ctx context.Context, uri string) (map[string]parse.Symbol, error) {
s.symbolsMutex.Lock()
defer s.symbolsMutex.Unlock()
uri, _ = url.QueryUnescape(uri)
if symbols, ok := s.cachedSymbols[uri]; ok {
return symbols, nil
}
return s.getAndCacheSymbolsInFile(ctx, uri)
}
// Fetches and caches the poryscript symbols from the given file uri.
func (s *poryscriptServer) getAndCacheSymbolsInFile(ctx context.Context, uri string) (map[string]parse.Symbol, error) {
uri, _ = url.QueryUnescape(uri)
content, err := s.getDocumentContent(ctx, uri)
if err != nil {
return nil, err
}
symbols := parse.ParseSymbols(content, uri)
symbolSet := map[string]parse.Symbol{}
for _, s := range symbols {
symbolSet[s.Name] = s
}
s.cachedSymbols[uri] = symbolSet
return symbolSet, nil
}
// Gets the aggregate list of miscellaneous tokens from the collection of files
// specified in the settings.
func (s *poryscriptServer) getMiscTokens(ctx context.Context, uri string) (map[string]parse.MiscToken, error) {
uri, _ = url.QueryUnescape(uri)
settings, err := s.config.GetFileSettings(ctx, s.connection, uri)
if err != nil {
return nil, err
}
miscTokens := map[string]parse.MiscToken{}
for _, includeSetting := range settings.SymbolIncludes {
s.miscTokensMutex.Lock()
tokens, err := s.getMiscTokensInFile(ctx, includeSetting.Expression, includeSetting.Type, includeSetting.File)
s.miscTokensMutex.Unlock()
if err != nil {
// TODO: log error?
continue
}
for _, t := range tokens {
miscTokens[t.Name] = t
}
}
return miscTokens, nil
}
// Gets the list of miscellaneous tokens from the given file uri. The tokens
// are cached for the file so that parsing is avoided in future calls.
func (s *poryscriptServer) getMiscTokensInFile(ctx context.Context, expression, tokenType, uri string) (map[string]parse.MiscToken, error) {
uri, _ = url.QueryUnescape(uri)
if tokens, ok := s.cachedMiscTokens[uri+expression]; ok {
return tokens, nil
}
return s.getAndCacheMiscTokensInFile(ctx, expression, tokenType, uri)
}
// Fetches and caches the miscellaneous tokens from the given file uri.
func (s *poryscriptServer) getAndCacheMiscTokensInFile(ctx context.Context, expression, tokenType, uri string) (map[string]parse.MiscToken, error) {
uri, _ = url.QueryUnescape(uri)
var content string
if err := s.connection.Call(ctx, "poryscript/readfile", uri, &content); err != nil {
return nil, err
}
var fileUri string
if err := s.connection.Call(ctx, "poryscript/getfileuri", uri, &fileUri); err != nil {
return nil, err
}
tokens := parse.ParseMiscTokens(content, expression, tokenType, fileUri)
tokenSet := map[string]parse.MiscToken{}
for _, t := range tokens {
tokenSet[t.Name] = t
}
s.cachedMiscTokens[uri+expression] = tokenSet
return tokenSet, nil
}
// Gets the content for the given file uri. The content is cached
// for the file so that parsing is avoided in future calls.
func (s *poryscriptServer) getDocumentContent(ctx context.Context, uri string) (string, error) {
s.documentsMutex.Lock()
defer s.documentsMutex.Unlock()
uri, _ = url.QueryUnescape(uri)
if content, ok := s.cachedDocuments[uri]; ok {
return content, nil
}
return s.getAndCacheDocumentContent(ctx, uri)
}
// Fetches and caches the content for the given file uri.
func (s *poryscriptServer) getAndCacheDocumentContent(ctx context.Context, uri string) (string, error) {
uri, _ = url.QueryUnescape(uri)
var content string
if err := s.connection.Call(ctx, "poryscript/readfs", uri, &content); err != nil {
return "", err
}
s.cachedDocuments[uri] = content
return content, nil
}
// Clears the various cached artifacts for the given file uri.
func (s *poryscriptServer) clearCaches(uri string) {
s.commandsMutex.Lock()
defer s.commandsMutex.Unlock()
s.constantsMutex.Lock()
defer s.constantsMutex.Unlock()
s.symbolsMutex.Lock()
defer s.symbolsMutex.Unlock()
s.miscTokensMutex.Lock()
defer s.miscTokensMutex.Unlock()
// The documents mutex lock must be acquired last, in order
// to avoid race conditions when loading the symbols and commands.
s.documentsMutex.Lock()
defer s.documentsMutex.Unlock()
delete(s.cachedCommands, uri)
delete(s.cachedConstants, uri)
delete(s.cachedSymbols, uri)
delete(s.cachedMiscTokens, uri)
delete(s.cachedDocuments, uri)
}
// Clears the various cached artifacts for watched files (.inc and .h files).
func (s *poryscriptServer) clearWatchedFileCaches() {
s.commandsMutex.Lock()
defer s.commandsMutex.Unlock()
s.miscTokensMutex.Lock()
defer s.miscTokensMutex.Unlock()
s.cachedCommands = map[string]map[string]parse.Command{}
s.cachedMiscTokens = map[string]map[string]parse.MiscToken{}
}