1
>
/*---------------------------------------------------------------------------------------------
annotations.ts
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 { binarySearch2 } from '../../../../base/common/arrays.js';
7
>
import { StringEdit } from '../../core/edits/stringEdit.js';
8
>
import { OffsetRange } from '../../core/ranges/offsetRange.js';
9
>
10
>
export interface IAnnotation<T> {
11
>
range: OffsetRange;
12
>
annotation: T;
13
>
}
14
>
15
>
export interface IAnnotatedString<T> {
16
>
/**
17
>
* Set annotations for a specific line.
18
>
* Annotations should be sorted and non-overlapping.
19
>
*/
20
>
setAnnotations(annotations: AnnotationsUpdate<T>): void;
21
>
/**
22
>
* Return annotations intersecting with the given offset range.
23
>
*/
24
>
getAnnotationsIntersecting(range: OffsetRange): IAnnotation<T>[];
25
>
/**
26
>
* Get all the annotations. Method is used for testing.
27
>
*/
28
>
getAllAnnotations(): IAnnotation<T>[];
29
>
/**
30
>
* Apply a string edit to the annotated string.
31
>
* @returns The annotations that were deleted (became empty) as a result of the edit.
32
>
*/
33
>
applyEdit(edit: StringEdit): IAnnotation<T>[];
34
>
/**
35
>
* Clone the annotated string.
36
>
*/
37
>
clone(): IAnnotatedString<T>;
38
>
}
39
>
40
>
export class AnnotatedString<T> implements IAnnotatedString<T> {
41
>
42
>
/**
43
>
* Annotations are non intersecting and contiguous in the array.
44
>
*/
45
>
private _annotations: IAnnotation<T>[] = [];
46
>
47
>
constructor(annotations: IAnnotation<T>[] = []) {
48
this._annotations = annotations;
49
}
51
>
/**
52
>
* Set annotations for a specific range.
53
>
* Annotations should be sorted and non-overlapping.
54
>
* If the annotation value is undefined, the annotation is removed.
55
>
*/
56
>
public setAnnotations(annotations: AnnotationsUpdate<T>): void {
57
for (const annotation of annotations.annotations) {
58
const startIndex = this._getStartIndexOfIntersectingAnnotation(annotation.range.start);