Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inlineCompletions/test/browser/computeGhostText.test.ts
4798 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 { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
8
import { Range } from '../../../../common/core/range.js';
9
import { TextReplacement } from '../../../../common/core/edits/textEdit.js';
10
import { createTextModel } from '../../../../test/common/testTextModel.js';
11
import { computeGhostText } from '../../browser/model/computeGhostText.js';
12
13
suite('computeGhostText', () => {
14
ensureNoDisposablesAreLeakedInTestSuite();
15
16
function getOutput(text: string, suggestion: string): unknown {
17
const rangeStartOffset = text.indexOf('[');
18
const rangeEndOffset = text.indexOf(']') - 1;
19
const cleanedText = text.replace('[', '').replace(']', '');
20
const tempModel = createTextModel(cleanedText);
21
const range = Range.fromPositions(tempModel.getPositionAt(rangeStartOffset), tempModel.getPositionAt(rangeEndOffset));
22
const options = ['prefix', 'subword'] as const;
23
// eslint-disable-next-line local/code-no-any-casts
24
const result = {} as any;
25
for (const option of options) {
26
result[option] = computeGhostText(new TextReplacement(range, suggestion), tempModel, option)?.render(cleanedText, true);
27
}
28
29
tempModel.dispose();
30
31
if (new Set(Object.values(result)).size === 1) {
32
return Object.values(result)[0];
33
}
34
35
return result;
36
}
37
38
test('Basic', () => {
39
assert.deepStrictEqual(getOutput('[foo]baz', 'foobar'), 'foo[bar]baz');
40
assert.deepStrictEqual(getOutput('[aaa]aaa', 'aaaaaa'), 'aaa[aaa]aaa');
41
assert.deepStrictEqual(getOutput('[foo]baz', 'boobar'), undefined);
42
assert.deepStrictEqual(getOutput('[foo]foo', 'foofoo'), 'foo[foo]foo');
43
assert.deepStrictEqual(getOutput('foo[]', 'bar\nhello'), 'foo[bar\nhello]');
44
});
45
46
test('Empty ghost text', () => {
47
assert.deepStrictEqual(getOutput('[foo]', 'foo'), 'foo');
48
});
49
50
test('Whitespace (indentation)', () => {
51
assert.deepStrictEqual(getOutput('[ foo]', 'foobar'), ' foo[bar]');
52
assert.deepStrictEqual(getOutput('[\tfoo]', 'foobar'), '\tfoo[bar]');
53
assert.deepStrictEqual(getOutput('[\t foo]', '\tfoobar'), ' foo[bar]');
54
assert.deepStrictEqual(getOutput('[\tfoo]', '\t\tfoobar'), { prefix: undefined, subword: '\t[\t]foo[bar]' });
55
assert.deepStrictEqual(getOutput('[\t]', '\t\tfoobar'), '\t[\tfoobar]');
56
assert.deepStrictEqual(getOutput('\t[]', '\t'), '\t[\t]');
57
assert.deepStrictEqual(getOutput('\t[\t]', ''), '\t\t');
58
59
assert.deepStrictEqual(getOutput('[ ]', 'return 1'), ' [return 1]');
60
});
61
62
test('Whitespace (outside of indentation)', () => {
63
assert.deepStrictEqual(getOutput('bar[ foo]', 'foobar'), undefined);
64
assert.deepStrictEqual(getOutput('bar[\tfoo]', 'foobar'), undefined);
65
});
66
67
test('Unsupported Case', () => {
68
assert.deepStrictEqual(getOutput('fo[o\n]', 'x\nbar'), undefined);
69
});
70
71
test('New Line', () => {
72
assert.deepStrictEqual(getOutput('fo[o\n]', 'o\nbar'), 'foo\n[bar]');
73
});
74
75
test('Multi Part Diffing', () => {
76
assert.deepStrictEqual(getOutput('foo[()]', '(x);'), { prefix: undefined, subword: 'foo([x])[;]' });
77
assert.deepStrictEqual(getOutput('[\tfoo]', '\t\tfoobar'), { prefix: undefined, subword: '\t[\t]foo[bar]' });
78
assert.deepStrictEqual(getOutput('[(y ===)]', '(y === 1) { f(); }'), { prefix: undefined, subword: '(y ===[ 1])[ { f(); }]' });
79
assert.deepStrictEqual(getOutput('[(y ==)]', '(y === 1) { f(); }'), { prefix: undefined, subword: '(y ==[= 1])[ { f(); }]' });
80
81
assert.deepStrictEqual(getOutput('[(y ==)]', '(y === 1) { f(); }'), { prefix: undefined, subword: '(y ==[= 1])[ { f(); }]' });
82
});
83
84
test('Multi Part Diffing 1', () => {
85
assert.deepStrictEqual(getOutput('[if () ()]', 'if (1 == f()) ()'), { prefix: undefined, subword: 'if ([1 == f()]) ()' });
86
});
87
88
test('Multi Part Diffing 2', () => {
89
assert.deepStrictEqual(getOutput('[)]', '())'), ({ prefix: undefined, subword: '[(])[)]' }));
90
assert.deepStrictEqual(getOutput('[))]', '(())'), ({ prefix: undefined, subword: '[((]))' }));
91
});
92
93
test('Parenthesis Matching', () => {
94
assert.deepStrictEqual(getOutput('[console.log()]', 'console.log({ label: "(" })'), {
95
prefix: undefined,
96
subword: 'console.log([{ label: "(" }])'
97
});
98
});
99
});
100
101