Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/test/node/getNodeToDocument.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
6
import { deannotateSrc } from '../../../../util/common/test/annotatedSrc';
7
import { _getNodeToDocument } from '../../node/docGenParsing';
8
import { WASMLanguage } from '../../node/treeSitterLanguages';
9
import { insertRangeMarkers, MarkerRange } from './markers';
10
11
export async function srcWithAnnotatedNodeToDoc(language: WASMLanguage, source: string, includeSelection = false) {
12
const { deannotatedSrc, annotatedRange: selection } = deannotateSrc(source);
13
14
const result = await _getNodeToDocument(
15
language,
16
deannotatedSrc,
17
selection
18
);
19
20
const identifier = result.nodeIdentifier;
21
22
const markers: MarkerRange[] = [];
23
24
if (identifier !== undefined && identifier !== '') {
25
const identIx = deannotatedSrc.indexOf(identifier);
26
if (identIx !== -1) {
27
markers.push({
28
startIndex: identIx,
29
endIndex: identIx + identifier.length,
30
kind: 'IDENT'
31
});
32
}
33
}
34
35
if (includeSelection) {
36
markers.push(
37
{
38
startIndex: selection.startIndex,
39
endIndex: selection.endIndex,
40
kind: 'SELECTION'
41
}
42
);
43
}
44
45
markers.push(
46
{
47
startIndex: result.nodeToDocument.startIndex,
48
endIndex: result.nodeToDocument.endIndex,
49
kind: result.nodeToDocument.type.toUpperCase(),
50
}
51
);
52
53
return insertRangeMarkers(deannotatedSrc, markers);
54
}
55
56