Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/emmet/src/parseDocument.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { TextDocument } from 'vscode';
7
import { Node as FlatNode } from 'EmmetFlatNode';
8
import parse from '@emmetio/html-matcher';
9
import parseStylesheet from '@emmetio/css-parser';
10
import { isStyleSheet } from './util';
11
12
type Pair<K, V> = {
13
key: K;
14
value: V;
15
};
16
17
// Map(filename, Pair(fileVersion, rootNodeOfParsedContent))
18
const _parseCache = new Map<string, Pair<number, FlatNode> | undefined>();
19
20
export function getRootNode(document: TextDocument, useCache: boolean): FlatNode {
21
const key = document.uri.toString();
22
const result = _parseCache.get(key);
23
const documentVersion = document.version;
24
if (useCache && result) {
25
if (documentVersion === result.key) {
26
return result.value;
27
}
28
}
29
30
const parseContent = isStyleSheet(document.languageId) ? parseStylesheet : parse;
31
const rootNode = parseContent(document.getText());
32
if (useCache) {
33
_parseCache.set(key, { key: documentVersion, value: rootNode });
34
}
35
return rootNode;
36
}
37
38
export function addFileToParseCache(document: TextDocument) {
39
const filename = document.uri.toString();
40
_parseCache.set(filename, undefined);
41
}
42
43
export function removeFileFromParseCache(document: TextDocument) {
44
const filename = document.uri.toString();
45
_parseCache.delete(filename);
46
}
47
48
export function clearParseCache() {
49
_parseCache.clear();
50
}
51
52