Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/visualization.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*--------------------------------------------------------------------------------------------*/45import { findFirstIdxMonotonousOrArrLen, findLastIdxMonotonous } from '../../../../util/vs/base/common/arraysFind';6import { OffsetRange } from '../../../../util/vs/editor/common/core/ranges/offsetRange';78export function toAstNode<T>(9node: T,10fn: (node: T) => Omit<IAstNode, 'children' | 'range'> & { range: OffsetRange; children?: readonly T[] }11): IAstNode {12const data = fn(node);13return {14...data,15range: [data.range.start, data.range.endExclusive],16children: data.children?.map(child => toAstNode(child, fn)),17};18}1920export interface IAstVisualization {21source: ISource | string;22root: IAstNode;23}2425interface IAstNode {26label: string;27isMarked?: boolean;28range: IOffsetRange;29children?: IAstNode[];30}3132interface ISource {33value: string;34decorations: { range: IOffsetRange; color: string }[];35}3637type IOffsetRange = [start: number, endEx: number];383940export function subtractRange(range: OffsetRange, ranges: OffsetRange[]): OffsetRange[] {41// idx of first element that touches range or that is after range42const joinRangeStartIdx = findFirstIdxMonotonousOrArrLen(ranges, r => r.endExclusive >= range.start);43// idx of element after { last element that touches range or that is before range }44const joinRangeEndIdxExclusive = findLastIdxMonotonous(ranges, r => r.start <= range.endExclusive) + 1;4546if (joinRangeStartIdx === joinRangeEndIdxExclusive) {47return [range];48}4950const result: OffsetRange[] = [];51let start = range.start;52for (let i = joinRangeStartIdx; i < joinRangeEndIdxExclusive; i++) {53const r = ranges[i];54if (r.start > start) {55result.push(new OffsetRange(start, r.start));56}57start = r.endExclusive;58}59if (start < range.endExclusive) {60result.push(new OffsetRange(start, range.endExclusive));61}6263return result;64}656667