Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/visualization.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 { findFirstIdxMonotonousOrArrLen, findLastIdxMonotonous } from '../../../../util/vs/base/common/arraysFind';
7
import { OffsetRange } from '../../../../util/vs/editor/common/core/ranges/offsetRange';
8
9
export function toAstNode<T>(
10
node: T,
11
fn: (node: T) => Omit<IAstNode, 'children' | 'range'> & { range: OffsetRange; children?: readonly T[] }
12
): IAstNode {
13
const data = fn(node);
14
return {
15
...data,
16
range: [data.range.start, data.range.endExclusive],
17
children: data.children?.map(child => toAstNode(child, fn)),
18
};
19
}
20
21
export interface IAstVisualization {
22
source: ISource | string;
23
root: IAstNode;
24
}
25
26
interface IAstNode {
27
label: string;
28
isMarked?: boolean;
29
range: IOffsetRange;
30
children?: IAstNode[];
31
}
32
33
interface ISource {
34
value: string;
35
decorations: { range: IOffsetRange; color: string }[];
36
}
37
38
type IOffsetRange = [start: number, endEx: number];
39
40
41
export function subtractRange(range: OffsetRange, ranges: OffsetRange[]): OffsetRange[] {
42
// idx of first element that touches range or that is after range
43
const joinRangeStartIdx = findFirstIdxMonotonousOrArrLen(ranges, r => r.endExclusive >= range.start);
44
// idx of element after { last element that touches range or that is before range }
45
const joinRangeEndIdxExclusive = findLastIdxMonotonous(ranges, r => r.start <= range.endExclusive) + 1;
46
47
if (joinRangeStartIdx === joinRangeEndIdxExclusive) {
48
return [range];
49
}
50
51
const result: OffsetRange[] = [];
52
let start = range.start;
53
for (let i = joinRangeStartIdx; i < joinRangeEndIdxExclusive; i++) {
54
const r = ranges[i];
55
if (r.start > start) {
56
result.push(new OffsetRange(start, r.start));
57
}
58
start = r.endExclusive;
59
}
60
if (start < range.endExclusive) {
61
result.push(new OffsetRange(start, range.endExclusive));
62
}
63
64
return result;
65
}
66
67