Path: blob/main/extensions/copilot/src/platform/parser/test/node/getStructure.util.ts
13405 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*--------------------------------------------------------------------------------------------*/4import * as fs from 'fs';5import * as path from '../../../../util/vs/base/common/path';6import { OverlayNode } from '../../node/nodes';7import { structureComputer } from '../../node/structure';8import { WASMLanguage } from '../../node/treeSitterLanguages';9import { insertRangeMarkers } from './markers';1011export function pathInFixture(pathWithinFixturesDir: string) {12const filePath = path.join(__dirname, 'fixtures', pathWithinFixturesDir);13return filePath;14}1516export function snapshotPathInFixture(pathWithinFixturesDir: string) {17return pathInFixture(pathWithinFixturesDir + '.getStructure.html');18}1920export async function fromFixture(pathWithinFixturesDir: string) {21const filePath = pathInFixture(pathWithinFixturesDir);22const contents = (await fs.promises.readFile(filePath)).toString();23return contents;24}2526function treeToFlatList(27node: OverlayNode28): { startIndex: number; endIndex: number; kind: string }[] {29const result: { startIndex: number; endIndex: number; kind: string }[] =30[];31for (const child of node.children) {32result.push({33startIndex: child.startIndex,34endIndex: child.endIndex,35kind: child.kind.toUpperCase(),36});37result.push(...treeToFlatList(child));38}39return result;40}4142export async function srcWithAnnotatedStructure(43language: WASMLanguage,44source: string45) {46const structure = await structureComputer.getStructure(language, source);47const flatList = structure ? treeToFlatList(structure) : [];48return insertRangeMarkers(source, flatList);49}505152