-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcache.ts
73 lines (62 loc) · 1.81 KB
/
cache.ts
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
import type * as monaco from 'monaco-editor'
import type { ImportsContext } from '~/workers/language/types'
import { buildImportContext } from './parser/imports'
import { stripSlash } from './utils'
/**
* Stores document metadata (such as symbols, imports) in cache.
*/
export class DocumentMetadataCache {
private readonly cache = new Map<string, ImportsContext>()
/**
* Flush cache contents.
*
* If fileName is not empty, flushes only record for specified file name.
*/
flush(fileName?: string) {
if (fileName) {
this.cache.delete(fileName)
return
}
this.cache.clear()
}
/**
* Invalidates cache if document is changed in imports range.
*/
handleUpdate(fileName: string, event: monaco.editor.IModelContentChangedEvent) {
const entry = this.cache.get(fileName)
if (!entry) {
return
}
if (event.isFlush || !entry.totalRange) {
this.cache.delete(fileName)
return
}
const { totalRange } = entry
for (const change of event.changes) {
const { startLineNumber } = change.range
if (startLineNumber >= totalRange.startLineNumber && startLineNumber <= totalRange.endLineNumber) {
this.cache.delete(fileName)
return
}
}
}
/**
* Returns document imports metadata from context.
*
* Populates data from model if it's not cached.
*/
getMetadata(model: monaco.editor.ITextModel) {
// model file name has slash at root
const fileName = stripSlash(model.uri.path)
const data = this.cache.get(fileName)
if (data && !data.hasError) {
return data
}
return this.updateCache(fileName, model)
}
private updateCache(fileName: string, model: monaco.editor.ITextModel) {
const context = buildImportContext(model)
this.cache.set(fileName, context)
return context
}
}