Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/annotatedLineRange.ts
13397 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 { LineRange } from '../vs/editor/common/core/ranges/lineRange';
7
8
export class AnnotatedLineRange<T> extends LineRange {
9
public static fromLineRange(range: LineRange): AnnotatedLineRange<void> {
10
return new AnnotatedLineRange(range.startLineNumber, range.endLineNumberExclusive, undefined);
11
}
12
13
public static fromLineRangeWithData<T>(range: LineRange, data: T): AnnotatedLineRange<T> {
14
return new AnnotatedLineRange(range.startLineNumber, range.endLineNumberExclusive, data);
15
}
16
17
constructor(
18
startLineNumber: number,
19
endLineNumberExclusive: number,
20
public readonly data: T,
21
) {
22
super(startLineNumber, endLineNumberExclusive);
23
}
24
}
25
26
export class AnnotatedLineRanges<T> {
27
constructor(
28
/**
29
* Have to be sorted and disjoined.
30
*/
31
public readonly ranges: readonly AnnotatedLineRange<T>[],
32
) {
33
}
34
35
public getFilled(range: LineRange): AnnotatedLineRanges<T | void> {
36
const filledRanges: AnnotatedLineRange<T | void>[] = [];
37
let lastEndLineNumberExclusive = range.startLineNumber;
38
for (const r of this.ranges) {
39
if (r.startLineNumber > lastEndLineNumberExclusive) {
40
filledRanges.push(new AnnotatedLineRange(lastEndLineNumberExclusive, r.startLineNumber, undefined));
41
}
42
filledRanges.push(r);
43
lastEndLineNumberExclusive = r.endLineNumberExclusive;
44
}
45
if (lastEndLineNumberExclusive < range.endLineNumberExclusive) {
46
filledRanges.push(new AnnotatedLineRange(lastEndLineNumberExclusive, range.endLineNumberExclusive, undefined));
47
}
48
return new AnnotatedLineRanges(filledRanges);
49
}
50
51
public intersects(range: LineRange): boolean {
52
for (const r of this.ranges) {
53
if (r.intersectsStrict(range)) {
54
return true;
55
}
56
}
57
return false;
58
}
59
60
public withAdded<T2>(range: AnnotatedLineRange<T2>): AnnotatedLineRanges<T | T2> {
61
const newRanges: AnnotatedLineRange<T | T2>[] = [...this.ranges];
62
newRanges.push(range);
63
newRanges.sort((a, b) => a.startLineNumber - b.startLineNumber);
64
return new AnnotatedLineRanges(newRanges);
65
}
66
}
67
68