1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { CancellationToken } from 'vs/base/common/cancellation';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { Position } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
import { SelectionRangeProvider } from 'vs/editor/common/languages';
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
import { IModelService } from 'vs/editor/common/services/model';
import { BracketSelectionRangeProvider } from 'vs/editor/contrib/smartSelect/browser/bracketSelections';
import { provideSelectionRanges } from 'vs/editor/contrib/smartSelect/browser/smartSelect';
import { WordSelectionRangeProvider } from 'vs/editor/contrib/smartSelect/browser/wordSelections';
import { createModelServices } from 'vs/editor/test/common/testTextModel';
import { StaticLanguageSelector } from 'vs/editor/test/common/mocks/mockMode';
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry';
import { ILanguageService } from 'vs/editor/common/languages/language';
suite('SmartSelect', () => {
const OriginalBracketSelectionRangeProviderMaxDuration = BracketSelectionRangeProvider._maxDuration;
suiteSetup(() => {
BracketSelectionRangeProvider._maxDuration = 5000; // 5 seconds
});
suiteTeardown(() => {
BracketSelectionRangeProvider._maxDuration = OriginalBracketSelectionRangeProviderMaxDuration;
});
const languageId = 'mockJSMode';
let disposables: DisposableStore;
let modelService: IModelService;
let providers = new LanguageFeatureRegistry<SelectionRangeProvider>();
setup(() => {
disposables = new DisposableStore();
const instantiationService = createModelServices(disposables);
modelService = instantiationService.get(IModelService);
const languagConfigurationService = instantiationService.get(ILanguageConfigurationService);
const languageService = instantiationService.get(ILanguageService);
disposables.add(languageService.registerLanguage({ id: languageId }));
disposables.add(languagConfigurationService.register(languageId, {
brackets: [
['(', ')'],
['{', '}'],
['[', ']']
],
onEnterRules: javascriptOnEnterRules,
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\$\%\^\&\*\(\)\=\+\[\{\]\}\\\;\:\'\"\,\.\<\>\/\?\s]+)/g
}));
});
teardown(() => {
disposables.dispose();
});
async function assertGetRangesToPosition(text: string[], lineNumber: number, column: number, ranges: Range[], selectLeadingAndTrailingWhitespace = true): Promise<void> {
let uri = URI.file('test.js');
let model = modelService.createModel(text.join('\n'), new StaticLanguageSelector(languageId), uri);
let [actual] = await provideSelectionRanges(providers, model, [new Position(lineNumber, column)], { selectLeadingAndTrailingWhitespace }, CancellationToken.None);
let actualStr = actual!.map(r => new Range(r.startLineNumber, r.startColumn, r.endLineNumber, r.endColumn).toString());
let desiredStr = ranges.reverse().map(r => String(r));
assert.deepStrictEqual(actualStr, desiredStr, `\nA: ${actualStr} VS \nE: ${desiredStr}`);
modelService.destroyModel(uri);
}
test('getRangesToPosition #1', () => {
return assertGetRangesToPosition([
'function a(bar, foo){',
'\tif (bar) {',
'\t\treturn (bar + (2 * foo))',
'\t}',
'}'
], 3, 20, [
new Range(1, 1, 5, 2), // all
new Range(1, 21, 5, 2), // {} outside
new Range(1, 22, 5, 1), // {} inside
new Range(2, 1, 4, 3), // block
new Range(2, 1, 4, 3),
new Range(2, 2, 4, 3),
new Range(2, 11, 4, 3),
new Range(2, 12, 4, 2),
new Range(3, 1, 3, 27), // line w/ triva
new Range(3, 3, 3, 27), // line w/o triva
new Range(3, 10, 3, 27), // () outside
new Range(3, 11, 3, 26), // () inside
new Range(3, 17, 3, 26), // () outside
new Range(3, 18, 3, 25), // () inside
]);
});
test('config: selectLeadingAndTrailingWhitespace', async () => {
await assertGetRangesToPosition([
'aaa',
'\tbbb',
''
], 2, 3, [
new Range(1, 1, 3, 1), // all
new Range(2, 1, 2, 5), // line w/ triva
new Range(2, 2, 2, 5), // bbb
], true);
await assertGetRangesToPosition([
'aaa',
'\tbbb',
''
], 2, 3, [
new Range(1, 1, 3, 1), // all
new Range(2, 2, 2, 5), // () inside
], false);
});
test('getRangesToPosition #56886. Skip empty lines correctly.', () => {
return assertGetRangesToPosition([
'function a(bar, foo){',
'\tif (bar) {',
'',
'\t}',
'}'
], 3, 1, [
new Range(1, 1, 5, 2),
new Range(1, 21, 5, 2),
new Range(1, 22, 5, 1),
new Range(2, 1, 4, 3),
new Range(2, 1, 4, 3),
new Range(2, 2, 4, 3),
new Range(2, 11, 4, 3),
new Range(2, 12, 4, 2),
]);
});
test('getRangesToPosition #56886. Do not skip lines with only whitespaces.', () => {
return assertGetRangesToPosition([
'function a(bar, foo){',
'\tif (bar) {',
' ',
'\t}',
'}'
], 3, 1, [
new Range(1, 1, 5, 2), // all
new Range(1, 21, 5, 2), // {} outside
new Range(1, 22, 5, 1), // {} inside
new Range(2, 1, 4, 3),
new Range(2, 1, 4, 3),
new Range(2, 2, 4, 3),
new Range(2, 11, 4, 3),
new Range(2, 12, 4, 2),
new Range(3, 1, 3, 2), // block
new Range(3, 1, 3, 2) // empty line
]);
});
test('getRangesToPosition #40658. Cursor at first position inside brackets should select line inside.', () => {
return assertGetRangesToPosition([
' [ ]',
' { } ',
'( ) '
], 2, 3, [
new Range(1, 1, 3, 5),
new Range(2, 1, 2, 6), // line w/ triava
new Range(2, 2, 2, 5), // {} inside, line w/o triva
new Range(2, 3, 2, 4) // {} inside
]);
});
test('getRangesToPosition #40658. Cursor in empty brackets should reveal brackets first.', () => {
return assertGetRangesToPosition([
' [] ',
' { } ',
' ( ) '
], 1, 3, [
new Range(1, 1, 3, 7), // all
new Range(1, 1, 1, 5), // line w/ trival
new Range(1, 2, 1, 4), // [] outside, line w/o trival
new Range(1, 3, 1, 3), // [] inside
]);
});
test('getRangesToPosition #40658. Tokens before bracket will be revealed first.', () => {
return assertGetRangesToPosition([
' [] ',
' { } ',
'selectthis( ) '
], 3, 11, [
new Range(1, 1, 3, 15), // all
new Range(3, 1, 3, 15), // line w/ trivia
new Range(3, 1, 3, 14), // line w/o trivia
new Range(3, 1, 3, 11) // word
]);
});
// -- bracket selections
async function assertRanges(provider: SelectionRangeProvider, value: string, ...expected: IRange[]): Promise<void> {
let index = value.indexOf('|');
value = value.replace('|', '');
let model = modelService.createModel(value, new StaticLanguageSelector(languageId), URI.parse('fake:lang'));
let pos = model.getPositionAt(index);
let all = await provider.provideSelectionRanges(model, [pos], CancellationToken.None);
let ranges = all![0];
modelService.destroyModel(model.uri);
assert.strictEqual(expected.length, ranges!.length);
for (const range of ranges!) {
let exp = expected.shift() || null;
assert.ok(Range.equalsRange(range.range, exp), `A=${range.range} <> E=${exp}`);
}
}
test('bracket selection', async () => {
await assertRanges(new BracketSelectionRangeProvider(), '(|)',
new Range(1, 2, 1, 2), new Range(1, 1, 1, 3)
);
await assertRanges(new BracketSelectionRangeProvider(), '[[[](|)]]',
new Range(1, 6, 1, 6), new Range(1, 5, 1, 7), // ()
new Range(1, 3, 1, 7), new Range(1, 2, 1, 8), // [[]()]
new Range(1, 2, 1, 8), new Range(1, 1, 1, 9), // [[[]()]]
);
await assertRanges(new BracketSelectionRangeProvider(), '[a[](|)a]',
new Range(1, 6, 1, 6), new Range(1, 5, 1, 7),
new Range(1, 2, 1, 8), new Range(1, 1, 1, 9),
);
// no bracket
await assertRanges(new BracketSelectionRangeProvider(), 'fofof|fofo');
// empty
await assertRanges(new BracketSelectionRangeProvider(), '[[[]()]]|');
await assertRanges(new BracketSelectionRangeProvider(), '|[[[]()]]');
// edge
await assertRanges(new BracketSelectionRangeProvider(), '[|[[]()]]', new Range(1, 2, 1, 8), new Range(1, 1, 1, 9));
await assertRanges(new BracketSelectionRangeProvider(), '[[[]()]|]', new Range(1, 2, 1, 8), new Range(1, 1, 1, 9));
await assertRanges(new BracketSelectionRangeProvider(), 'aaa(aaa)bbb(b|b)ccc(ccc)', new Range(1, 13, 1, 15), new Range(1, 12, 1, 16));
await assertRanges(new BracketSelectionRangeProvider(), '(aaa(aaa)bbb(b|b)ccc(ccc))', new Range(1, 14, 1, 16), new Range(1, 13, 1, 17), new Range(1, 2, 1, 25), new Range(1, 1, 1, 26));
});
test('bracket with leading/trailing', async () => {
await assertRanges(new BracketSelectionRangeProvider(), 'for(a of b){\n foo(|);\n}',
new Range(2, 7, 2, 7), new Range(2, 6, 2, 8),
new Range(1, 13, 3, 1), new Range(1, 12, 3, 2),
new Range(1, 1, 3, 2), new Range(1, 1, 3, 2),
);
await assertRanges(new BracketSelectionRangeProvider(), 'for(a of b)\n{\n foo(|);\n}',
new Range(3, 7, 3, 7), new Range(3, 6, 3, 8),
new Range(2, 2, 4, 1), new Range(2, 1, 4, 2),
new Range(1, 1, 4, 2), new Range(1, 1, 4, 2),
);
});
test('in-word ranges', async () => {
await assertRanges(new WordSelectionRangeProvider(), 'f|ooBar',
new Range(1, 1, 1, 4), // foo
new Range(1, 1, 1, 7), // fooBar
new Range(1, 1, 1, 7), // doc
);
await assertRanges(new WordSelectionRangeProvider(), 'f|oo_Ba',
new Range(1, 1, 1, 4),
new Range(1, 1, 1, 7),
new Range(1, 1, 1, 7),
);
await assertRanges(new WordSelectionRangeProvider(), 'f|oo-Ba',
new Range(1, 1, 1, 4),
new Range(1, 1, 1, 7),
new Range(1, 1, 1, 7),
);
});
test('Default selection should select current word/hump first in camelCase #67493', async function () {
await assertRanges(new WordSelectionRangeProvider(), 'Abs|tractSmartSelect',
new Range(1, 1, 1, 9),
new Range(1, 1, 1, 20),
new Range(1, 1, 1, 20),
);
await assertRanges(new WordSelectionRangeProvider(), 'AbstractSma|rtSelect',
new Range(1, 9, 1, 14),
new Range(1, 1, 1, 20),
new Range(1, 1, 1, 20),
);
await assertRanges(new WordSelectionRangeProvider(), 'Abstrac-Sma|rt-elect',
new Range(1, 9, 1, 14),
new Range(1, 1, 1, 20),
new Range(1, 1, 1, 20),
);
await assertRanges(new WordSelectionRangeProvider(), 'Abstrac_Sma|rt_elect',
new Range(1, 9, 1, 14),
new Range(1, 1, 1, 20),
new Range(1, 1, 1, 20),
);
await assertRanges(new WordSelectionRangeProvider(), 'Abstrac_Sma|rt-elect',
new Range(1, 9, 1, 14),
new Range(1, 1, 1, 20),
new Range(1, 1, 1, 20),
);
await assertRanges(new WordSelectionRangeProvider(), 'Abstrac_Sma|rtSelect',
new Range(1, 9, 1, 14),
new Range(1, 1, 1, 20),
new Range(1, 1, 1, 20),
);
});
test('Smart select: only add line ranges if they\'re contained by the next range #73850', async function () {
const reg = providers.register('*', {
provideSelectionRanges() {
return [[
{ range: { startLineNumber: 1, startColumn: 10, endLineNumber: 1, endColumn: 11 } },
{ range: { startLineNumber: 1, startColumn: 10, endLineNumber: 3, endColumn: 2 } },
{ range: { startLineNumber: 1, startColumn: 1, endLineNumber: 3, endColumn: 2 } },
]];
}
});
await assertGetRangesToPosition(['type T = {', '\tx: number', '}'], 1, 10, [
new Range(1, 1, 3, 2), // all
new Range(1, 10, 3, 2), // { ... }
new Range(1, 10, 1, 11), // {
]);
reg.dispose();
});
test('Expand selection in words with underscores is inconsistent #90589', async function () {
await assertRanges(new WordSelectionRangeProvider(), 'Hel|lo_World',
new Range(1, 1, 1, 6),
new Range(1, 1, 1, 12),
new Range(1, 1, 1, 12),
);
await assertRanges(new WordSelectionRangeProvider(), 'Hello_Wo|rld',
new Range(1, 7, 1, 12),
new Range(1, 1, 1, 12),
new Range(1, 1, 1, 12),
);
await assertRanges(new WordSelectionRangeProvider(), 'Hello|_World',
new Range(1, 1, 1, 6),
new Range(1, 1, 1, 12),
new Range(1, 1, 1, 12),
);
await assertRanges(new WordSelectionRangeProvider(), 'Hello_|World',
new Range(1, 7, 1, 12),
new Range(1, 1, 1, 12),
new Range(1, 1, 1, 12),
);
await assertRanges(new WordSelectionRangeProvider(), 'Hello|-World',
new Range(1, 1, 1, 6),
new Range(1, 1, 1, 12),
new Range(1, 1, 1, 12),
);
await assertRanges(new WordSelectionRangeProvider(), 'Hello-|World',
new Range(1, 7, 1, 12),
new Range(1, 1, 1, 12),
new Range(1, 1, 1, 12),
);
await assertRanges(new WordSelectionRangeProvider(), 'Hello|World',
new Range(1, 6, 1, 11),
new Range(1, 1, 1, 11),
new Range(1, 1, 1, 11),
);
});
});
|