Path: blob/main/src/vs/editor/contrib/inlineCompletions/test/browser/computeGhostText.test.ts
4798 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 { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';7import { Range } from '../../../../common/core/range.js';8import { TextReplacement } from '../../../../common/core/edits/textEdit.js';9import { createTextModel } from '../../../../test/common/testTextModel.js';10import { computeGhostText } from '../../browser/model/computeGhostText.js';1112suite('computeGhostText', () => {13ensureNoDisposablesAreLeakedInTestSuite();1415function getOutput(text: string, suggestion: string): unknown {16const rangeStartOffset = text.indexOf('[');17const rangeEndOffset = text.indexOf(']') - 1;18const cleanedText = text.replace('[', '').replace(']', '');19const tempModel = createTextModel(cleanedText);20const range = Range.fromPositions(tempModel.getPositionAt(rangeStartOffset), tempModel.getPositionAt(rangeEndOffset));21const options = ['prefix', 'subword'] as const;22// eslint-disable-next-line local/code-no-any-casts23const result = {} as any;24for (const option of options) {25result[option] = computeGhostText(new TextReplacement(range, suggestion), tempModel, option)?.render(cleanedText, true);26}2728tempModel.dispose();2930if (new Set(Object.values(result)).size === 1) {31return Object.values(result)[0];32}3334return result;35}3637test('Basic', () => {38assert.deepStrictEqual(getOutput('[foo]baz', 'foobar'), 'foo[bar]baz');39assert.deepStrictEqual(getOutput('[aaa]aaa', 'aaaaaa'), 'aaa[aaa]aaa');40assert.deepStrictEqual(getOutput('[foo]baz', 'boobar'), undefined);41assert.deepStrictEqual(getOutput('[foo]foo', 'foofoo'), 'foo[foo]foo');42assert.deepStrictEqual(getOutput('foo[]', 'bar\nhello'), 'foo[bar\nhello]');43});4445test('Empty ghost text', () => {46assert.deepStrictEqual(getOutput('[foo]', 'foo'), 'foo');47});4849test('Whitespace (indentation)', () => {50assert.deepStrictEqual(getOutput('[ foo]', 'foobar'), ' foo[bar]');51assert.deepStrictEqual(getOutput('[\tfoo]', 'foobar'), '\tfoo[bar]');52assert.deepStrictEqual(getOutput('[\t foo]', '\tfoobar'), ' foo[bar]');53assert.deepStrictEqual(getOutput('[\tfoo]', '\t\tfoobar'), { prefix: undefined, subword: '\t[\t]foo[bar]' });54assert.deepStrictEqual(getOutput('[\t]', '\t\tfoobar'), '\t[\tfoobar]');55assert.deepStrictEqual(getOutput('\t[]', '\t'), '\t[\t]');56assert.deepStrictEqual(getOutput('\t[\t]', ''), '\t\t');5758assert.deepStrictEqual(getOutput('[ ]', 'return 1'), ' [return 1]');59});6061test('Whitespace (outside of indentation)', () => {62assert.deepStrictEqual(getOutput('bar[ foo]', 'foobar'), undefined);63assert.deepStrictEqual(getOutput('bar[\tfoo]', 'foobar'), undefined);64});6566test('Unsupported Case', () => {67assert.deepStrictEqual(getOutput('fo[o\n]', 'x\nbar'), undefined);68});6970test('New Line', () => {71assert.deepStrictEqual(getOutput('fo[o\n]', 'o\nbar'), 'foo\n[bar]');72});7374test('Multi Part Diffing', () => {75assert.deepStrictEqual(getOutput('foo[()]', '(x);'), { prefix: undefined, subword: 'foo([x])[;]' });76assert.deepStrictEqual(getOutput('[\tfoo]', '\t\tfoobar'), { prefix: undefined, subword: '\t[\t]foo[bar]' });77assert.deepStrictEqual(getOutput('[(y ===)]', '(y === 1) { f(); }'), { prefix: undefined, subword: '(y ===[ 1])[ { f(); }]' });78assert.deepStrictEqual(getOutput('[(y ==)]', '(y === 1) { f(); }'), { prefix: undefined, subword: '(y ==[= 1])[ { f(); }]' });7980assert.deepStrictEqual(getOutput('[(y ==)]', '(y === 1) { f(); }'), { prefix: undefined, subword: '(y ==[= 1])[ { f(); }]' });81});8283test('Multi Part Diffing 1', () => {84assert.deepStrictEqual(getOutput('[if () ()]', 'if (1 == f()) ()'), { prefix: undefined, subword: 'if ([1 == f()]) ()' });85});8687test('Multi Part Diffing 2', () => {88assert.deepStrictEqual(getOutput('[)]', '())'), ({ prefix: undefined, subword: '[(])[)]' }));89assert.deepStrictEqual(getOutput('[))]', '(())'), ({ prefix: undefined, subword: '[((]))' }));90});9192test('Parenthesis Matching', () => {93assert.deepStrictEqual(getOutput('[console.log()]', 'console.log({ label: "(" })'), {94prefix: undefined,95subword: 'console.log([{ label: "(" }])'96});97});98});99100101