Path: blob/main/src/vs/editor/contrib/inlineCompletions/test/browser/inlineEdits.test.ts
4797 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 assert from 'assert';6import { timeout } from '../../../../../base/common/async.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';8import { AnnotatedText, InlineEditContext, IWithAsyncTestCodeEditorAndInlineCompletionsModel, MockSearchReplaceCompletionsProvider, withAsyncTestCodeEditorAndInlineCompletionsModel } from './utils.js';910suite('Inline Edits', () => {11ensureNoDisposablesAreLeakedInTestSuite();1213const val = new AnnotatedText(`14class Point {15constructor(public x: number, public y: number) {}1617getLength2D(): number {18return↓ Math.sqrt(this.x * this.x + this.y * this.y↓);19}2021getJson(): string {22return ↓Ü;23}24}25`);2627async function runTest(cb: (ctx: IWithAsyncTestCodeEditorAndInlineCompletionsModel, provider: MockSearchReplaceCompletionsProvider, view: InlineEditContext) => Promise<void>): Promise<void> {28const provider = new MockSearchReplaceCompletionsProvider();29await withAsyncTestCodeEditorAndInlineCompletionsModel(val.value,30{ fakeClock: true, provider, inlineSuggest: { enabled: true } },31async (ctx) => {32const view = new InlineEditContext(ctx.model, ctx.editor);33ctx.store.add(view);34await cb(ctx, provider, view);35}36);37}3839test('Can Accept Inline Edit', async function () {40await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {41provider.add(`getLength2D(): number {42return Math.sqrt(this.x * this.x + this.y * this.y);43}`, `getLength3D(): number {44return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);45}`);4647await model.trigger();48await timeout(10000);49assert.deepStrictEqual(view.getAndClearViewStates(), ([50undefined,51'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'52]));5354model.accept();5556assert.deepStrictEqual(editor.getValue(), `57class Point {58constructor(public x: number, public y: number) {}5960getLength3D(): number {61return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);62}6364getJson(): string {65return Ü;66}67}68`);69});70});7172test('Can Type Inline Edit', async function () {73await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {74provider.add(`getLength2D(): number {75return Math.sqrt(this.x * this.x + this.y * this.y);76}`, `getLength3D(): number {77return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);78}`);79await model.trigger();80await timeout(10000);81assert.deepStrictEqual(view.getAndClearViewStates(), ([82undefined,83'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'84]));8586editor.setPosition(val.getMarkerPosition(1));87editorViewModel.type(' + t');8889assert.deepStrictEqual(view.getAndClearViewStates(), ([90'\n\tget❰Length2↦Length3❱D(): numbe...\n...this.y + t❰his.z...his.z❱);\n'91]));9293editorViewModel.type('his.z * this.z');94assert.deepStrictEqual(view.getAndClearViewStates(), ([95'\n\tget❰Length2↦Length3❱D(): numbe...'96]));97});98});99100test('Inline Edit Is Correctly Shifted When Typing', async function () {101await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {102provider.add('Ü', '{x: this.x, y: this.y}');103await model.trigger();104await timeout(10000);105assert.deepStrictEqual(view.getAndClearViewStates(), ([106undefined,107'...\n\t\treturn ❰Ü↦{x: t...is.y}❱;\n'108]));109editor.setPosition(val.getMarkerPosition(2));110editorViewModel.type('{');111112assert.deepStrictEqual(view.getAndClearViewStates(), ([113'...\t\treturn {❰Ü↦x: th...is.y}❱;\n'114]));115});116});117118test('Inline Edit Stays On Unrelated Edit', async function () {119await runTest(async ({ context, model, editor, editorViewModel }, provider, view) => {120provider.add(`getLength2D(): number {121return Math.sqrt(this.x * this.x + this.y * this.y);122}`, `getLength3D(): number {123return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);124}`);125await model.trigger();126await timeout(10000);127assert.deepStrictEqual(view.getAndClearViewStates(), ([128undefined,129'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'130]));131132editor.setPosition(val.getMarkerPosition(0));133editorViewModel.type('/* */');134135assert.deepStrictEqual(view.getAndClearViewStates(), ([136'\n\tget❰Length2↦Length3❱D(): numbe...\n...y * this.y❰ + th...his.z❱);\n'137]));138139await timeout(10000);140assert.deepStrictEqual(view.getAndClearViewStates(), ([141undefined142]));143});144});145});146147148