Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/node/querying.ts
13401 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 type { Language, Query, QueryMatch, SyntaxNode } from 'web-tree-sitter';
7
import { pushMany } from '../../../util/vs/base/common/arrays';
8
9
10
class LanguageQueryCache {
11
12
private readonly map = new Map<string, Query>();
13
14
constructor(
15
private readonly language: Language
16
) { }
17
18
getQuery(query: string): Query {
19
if (!this.map.has(query)) {
20
this.map.set(query, this.language.query(query));
21
}
22
return this.map.get(query)!;
23
}
24
}
25
26
class QueryCache {
27
28
public static INSTANCE = new QueryCache();
29
30
private readonly map = new Map<Language, LanguageQueryCache>();
31
32
getQuery(language: Language, query: string): Query {
33
if (!this.map.has(language)) {
34
this.map.set(language, new LanguageQueryCache(language));
35
}
36
return this.map.get(language)!.getQuery(query);
37
}
38
}
39
40
export function runQueries(queries: string[], root: SyntaxNode): QueryMatch[] {
41
const matches: QueryMatch[] = [];
42
for (const query of queries) {
43
const compiledQuery = QueryCache.INSTANCE.getQuery(root.tree.getLanguage(), query);
44
const queryMatches = compiledQuery.matches(root);
45
pushMany(matches, queryMatches);
46
}
47
return matches;
48
}
49
50