Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/inlineEdits/test/common/textEditLength.spec.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
7
import { describe, expect, it } from 'vitest';
8
import { Range } from '../../../../util/vs/editor/common/core/range';
9
import { TextLength } from '../../../../util/vs/editor/common/core/text/textLength';
10
import { SingleTextEditLength, TextLengthEdit } from '../../common/dataTypes/textEditLength';
11
12
describe('getRange', () => {
13
14
it('should return undefined for empty edits', () => {
15
const textLengthEdit = TextLengthEdit.empty;
16
expect(textLengthEdit.getRange()).toMatchInlineSnapshot(`undefined`);
17
});
18
19
it('should return the correct range for single edit', () => {
20
const range = new Range(1, 1, 1, 5);
21
const textLength = new TextLength(0, 4);
22
const singleEdit = new SingleTextEditLength(range, textLength);
23
const textLengthEdit = new TextLengthEdit([singleEdit]);
24
expect(textLengthEdit.getRange()?.toString()).toMatchInlineSnapshot(`"[1,1 -> 1,5]"`);
25
});
26
27
it('should return the correct range for multiple edits', () => {
28
const range1 = new Range(1, 1, 1, 5);
29
const textLength1 = new TextLength(0, 4);
30
const singleEdit1 = new SingleTextEditLength(range1, textLength1);
31
32
const range2 = new Range(2, 1, 2, 5);
33
const textLength2 = new TextLength(0, 4);
34
const singleEdit2 = new SingleTextEditLength(range2, textLength2);
35
36
const textLengthEdit = new TextLengthEdit([singleEdit1, singleEdit2]);
37
expect(textLengthEdit.getRange()?.toString()).toMatchInlineSnapshot(`"[1,1 -> 2,5]"`);
38
});
39
});
40
41
describe('compose', () => {
42
43
it('should return empty for composing two empty edits', () => {
44
const edit1 = TextLengthEdit.empty;
45
const edit2 = TextLengthEdit.empty;
46
const composedEdit = edit1.compose(edit2);
47
expect(composedEdit.edits).toMatchInlineSnapshot(`[]`);
48
});
49
50
it('should compose two non-overlapping edits correctly', () => {
51
const range1 = new Range(1, 1, 1, 5);
52
const textLength1 = new TextLength(0, 4);
53
const singleEdit1 = new SingleTextEditLength(range1, textLength1);
54
const edit1 = new TextLengthEdit([singleEdit1]);
55
56
const range2 = new Range(2, 1, 2, 5);
57
const textLength2 = new TextLength(0, 4);
58
const singleEdit2 = new SingleTextEditLength(range2, textLength2);
59
const edit2 = new TextLengthEdit([singleEdit2]);
60
61
const composedEdit = edit1.compose(edit2);
62
expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 0,4 },{ range: [2,1 -> 2,5], newLength: 0,4 }"`);
63
});
64
65
it('should compose two non-overlapping edits correctly - 2', () => {
66
const range1 = new Range(1, 1, 1, 5);
67
const textLength1 = new TextLength(2, 4);
68
const singleEdit1 = new SingleTextEditLength(range1, textLength1);
69
const edit1 = new TextLengthEdit([singleEdit1]);
70
71
const range2 = new Range(2, 1, 2, 5);
72
const textLength2 = new TextLength(0, 4);
73
const singleEdit2 = new SingleTextEditLength(range2, textLength2);
74
const edit2 = new TextLengthEdit([singleEdit2]);
75
76
const composedEdit = edit1.compose(edit2);
77
expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 2,4 }"`);
78
});
79
80
it('should compose two non-overlapping edits correctly - 3', () => {
81
const range1 = new Range(1, 1, 1, 5);
82
const textLength1 = new TextLength(2, 4);
83
const singleEdit1 = new SingleTextEditLength(range1, textLength1);
84
const edit1 = new TextLengthEdit([singleEdit1]);
85
86
const range2 = new Range(12, 1, 12, 5);
87
const textLength2 = new TextLength(4, 4);
88
const singleEdit2 = new SingleTextEditLength(range2, textLength2);
89
const edit2 = new TextLengthEdit([singleEdit2]);
90
91
const composedEdit = edit1.compose(edit2);
92
expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 2,4 },{ range: [10,1 -> 10,5], newLength: 4,4 }"`);
93
94
const composedEdit2 = edit2.compose(edit1);
95
expect(composedEdit2.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,5], newLength: 2,4 },{ range: [12,1 -> 12,5], newLength: 4,4 }"`);
96
});
97
98
it('should compose overlapping edits correctly', () => {
99
const range1 = new Range(1, 1, 1, 5);
100
const textLength1 = new TextLength(0, 4);
101
const singleEdit1 = new SingleTextEditLength(range1, textLength1);
102
const edit1 = new TextLengthEdit([singleEdit1]);
103
104
const range2 = new Range(1, 3, 1, 7);
105
const textLength2 = new TextLength(0, 4);
106
const singleEdit2 = new SingleTextEditLength(range2, textLength2);
107
const edit2 = new TextLengthEdit([singleEdit2]);
108
109
const composedEdit = edit1.compose(edit2);
110
expect(composedEdit.edits.toString()).toMatchInlineSnapshot(`"{ range: [1,1 -> 1,7], newLength: 0,6 }"`);
111
});
112
});
113
114