Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inlineCompletions/test/browser/inlineEdits.test.ts
4797 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 assert from 'assert';
7
import { timeout } from '../../../../../base/common/async.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
9
import { AnnotatedText, InlineEditContext, IWithAsyncTestCodeEditorAndInlineCompletionsModel, MockSearchReplaceCompletionsProvider, withAsyncTestCodeEditorAndInlineCompletionsModel } from './utils.js';
10
11
suite('Inline Edits', () => {
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
const val = new AnnotatedText(`
15
class Point {
16
constructor(public x: number, public y: number) {}
17
18
getLength2D(): number {
19
return↓ Math.sqrt(this.x * this.x + this.y * this.y↓);
20
}
21
22
getJson(): string {
23
return ↓Ü;
24
}
25
}
26
`);
27
28
async function runTest(cb: (ctx: IWithAsyncTestCodeEditorAndInlineCompletionsModel, provider: MockSearchReplaceCompletionsProvider, view: InlineEditContext) => Promise<void>): Promise<void> {
29
const provider = new MockSearchReplaceCompletionsProvider();
30
await withAsyncTestCodeEditorAndInlineCompletionsModel(val.value,
31
{ fakeClock: true, provider, inlineSuggest: { enabled: true } },
32
async (ctx) => {
33
const view = new InlineEditContext(ctx.model, ctx.editor);
34
ctx.store.add(view);
35
await cb(ctx, provider, view);
36
}
37
);
38
}
39
40
test('Can Accept Inline Edit', async function () {
41
await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {
42
provider.add(`getLength2D(): number {
43
return Math.sqrt(this.x * this.x + this.y * this.y);
44
}`, `getLength3D(): number {
45
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
46
}`);
47
48
await model.trigger();
49
await timeout(10000);
50
assert.deepStrictEqual(view.getAndClearViewStates(), ([
51
undefined,
52
'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'
53
]));
54
55
model.accept();
56
57
assert.deepStrictEqual(editor.getValue(), `
58
class Point {
59
constructor(public x: number, public y: number) {}
60
61
getLength3D(): number {
62
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
63
}
64
65
getJson(): string {
66
return Ü;
67
}
68
}
69
`);
70
});
71
});
72
73
test('Can Type Inline Edit', async function () {
74
await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {
75
provider.add(`getLength2D(): number {
76
return Math.sqrt(this.x * this.x + this.y * this.y);
77
}`, `getLength3D(): number {
78
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
79
}`);
80
await model.trigger();
81
await timeout(10000);
82
assert.deepStrictEqual(view.getAndClearViewStates(), ([
83
undefined,
84
'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'
85
]));
86
87
editor.setPosition(val.getMarkerPosition(1));
88
editorViewModel.type(' + t');
89
90
assert.deepStrictEqual(view.getAndClearViewStates(), ([
91
'\n\tget❰Length2↦Length3❱D(): numbe...\n...this.y + t❰his.z...his.z❱);\n'
92
]));
93
94
editorViewModel.type('his.z * this.z');
95
assert.deepStrictEqual(view.getAndClearViewStates(), ([
96
'\n\tget❰Length2↦Length3❱D(): numbe...'
97
]));
98
});
99
});
100
101
test('Inline Edit Is Correctly Shifted When Typing', async function () {
102
await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {
103
provider.add('Ü', '{x: this.x, y: this.y}');
104
await model.trigger();
105
await timeout(10000);
106
assert.deepStrictEqual(view.getAndClearViewStates(), ([
107
undefined,
108
'...\n\t\treturn ❰Ü↦{x: t...is.y}❱;\n'
109
]));
110
editor.setPosition(val.getMarkerPosition(2));
111
editorViewModel.type('{');
112
113
assert.deepStrictEqual(view.getAndClearViewStates(), ([
114
'...\t\treturn {❰Ü↦x: th...is.y}❱;\n'
115
]));
116
});
117
});
118
119
test('Inline Edit Stays On Unrelated Edit', async function () {
120
await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {
121
provider.add(`getLength2D(): number {
122
return Math.sqrt(this.x * this.x + this.y * this.y);
123
}`, `getLength3D(): number {
124
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
125
}`);
126
await model.trigger();
127
await timeout(10000);
128
assert.deepStrictEqual(view.getAndClearViewStates(), ([
129
undefined,
130
'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'
131
]));
132
133
editor.setPosition(val.getMarkerPosition(0));
134
editorViewModel.type('/* */');
135
136
assert.deepStrictEqual(view.getAndClearViewStates(), ([
137
'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'
138
]));
139
140
await timeout(10000);
141
assert.deepStrictEqual(view.getAndClearViewStates(), ([
142
undefined
143
]));
144
});
145
});
146
});
147
148