Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/test/node/getStructure.util.ts
13405 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
import * as fs from 'fs';
6
import * as path from '../../../../util/vs/base/common/path';
7
import { OverlayNode } from '../../node/nodes';
8
import { structureComputer } from '../../node/structure';
9
import { WASMLanguage } from '../../node/treeSitterLanguages';
10
import { insertRangeMarkers } from './markers';
11
12
export function pathInFixture(pathWithinFixturesDir: string) {
13
const filePath = path.join(__dirname, 'fixtures', pathWithinFixturesDir);
14
return filePath;
15
}
16
17
export function snapshotPathInFixture(pathWithinFixturesDir: string) {
18
return pathInFixture(pathWithinFixturesDir + '.getStructure.html');
19
}
20
21
export async function fromFixture(pathWithinFixturesDir: string) {
22
const filePath = pathInFixture(pathWithinFixturesDir);
23
const contents = (await fs.promises.readFile(filePath)).toString();
24
return contents;
25
}
26
27
function treeToFlatList(
28
node: OverlayNode
29
): { startIndex: number; endIndex: number; kind: string }[] {
30
const result: { startIndex: number; endIndex: number; kind: string }[] =
31
[];
32
for (const child of node.children) {
33
result.push({
34
startIndex: child.startIndex,
35
endIndex: child.endIndex,
36
kind: child.kind.toUpperCase(),
37
});
38
result.push(...treeToFlatList(child));
39
}
40
return result;
41
}
42
43
export async function srcWithAnnotatedStructure(
44
language: WASMLanguage,
45
source: string
46
) {
47
const structure = await structureComputer.getStructure(language, source);
48
const flatList = structure ? treeToFlatList(structure) : [];
49
return insertRangeMarkers(source, flatList);
50
}
51
52