Path: blob/main/extensions/copilot/src/platform/parser/node/querying.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { Language, Query, QueryMatch, SyntaxNode } from 'web-tree-sitter';6import { pushMany } from '../../../util/vs/base/common/arrays';789class LanguageQueryCache {1011private readonly map = new Map<string, Query>();1213constructor(14private readonly language: Language15) { }1617getQuery(query: string): Query {18if (!this.map.has(query)) {19this.map.set(query, this.language.query(query));20}21return this.map.get(query)!;22}23}2425class QueryCache {2627public static INSTANCE = new QueryCache();2829private readonly map = new Map<Language, LanguageQueryCache>();3031getQuery(language: Language, query: string): Query {32if (!this.map.has(language)) {33this.map.set(language, new LanguageQueryCache(language));34}35return this.map.get(language)!.getQuery(query);36}37}3839export function runQueries(queries: string[], root: SyntaxNode): QueryMatch[] {40const matches: QueryMatch[] = [];41for (const query of queries) {42const compiledQuery = QueryCache.INSTANCE.getQuery(root.tree.getLanguage(), query);43const queryMatches = compiledQuery.matches(root);44pushMany(matches, queryMatches);45}46return matches;47}484950