Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/comment/test/browser/blockCommentCommand.test.ts
3296 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 { DisposableStore } from '../../../../../base/common/lifecycle.js';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
8
import { Selection } from '../../../../common/core/selection.js';
9
import { ICommand } from '../../../../common/editorCommon.js';
10
import { ILanguageService } from '../../../../common/languages/language.js';
11
import { ILanguageConfigurationService } from '../../../../common/languages/languageConfigurationRegistry.js';
12
import { BlockCommentCommand } from '../../browser/blockCommentCommand.js';
13
import { testCommand } from '../../../../test/browser/testCommand.js';
14
import { ServicesAccessor } from '../../../../../platform/instantiation/common/instantiation.js';
15
16
function _testCommentCommand(lines: string[], selection: Selection, commandFactory: (accessor: ServicesAccessor, selection: Selection) => ICommand, expectedLines: string[], expectedSelection: Selection): void {
17
const languageId = 'commentMode';
18
const prepare = (accessor: ServicesAccessor, disposables: DisposableStore) => {
19
const languageConfigurationService = accessor.get(ILanguageConfigurationService);
20
const languageService = accessor.get(ILanguageService);
21
disposables.add(languageService.registerLanguage({ id: languageId }));
22
disposables.add(languageConfigurationService.register(languageId, {
23
comments: { lineComment: '!@#', blockComment: ['<0', '0>'] }
24
}));
25
};
26
testCommand(lines, languageId, selection, commandFactory, expectedLines, expectedSelection, undefined, prepare);
27
}
28
29
function testBlockCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
30
_testCommentCommand(lines, selection, (accessor, sel) => new BlockCommentCommand(sel, true, accessor.get(ILanguageConfigurationService)), expectedLines, expectedSelection);
31
}
32
33
suite('Editor Contrib - Block Comment Command', () => {
34
35
ensureNoDisposablesAreLeakedInTestSuite();
36
37
test('empty selection wraps itself', function () {
38
testBlockCommentCommand(
39
[
40
'first',
41
'\tsecond line',
42
'third line',
43
'fourth line',
44
'fifth'
45
],
46
new Selection(1, 3, 1, 3),
47
[
48
'fi<0 0>rst',
49
'\tsecond line',
50
'third line',
51
'fourth line',
52
'fifth'
53
],
54
new Selection(1, 6, 1, 6)
55
);
56
});
57
58
test('invisible selection ignored', function () {
59
testBlockCommentCommand(
60
[
61
'first',
62
'\tsecond line',
63
'third line',
64
'fourth line',
65
'fifth'
66
],
67
new Selection(2, 1, 1, 1),
68
[
69
'<0 first',
70
' 0>\tsecond line',
71
'third line',
72
'fourth line',
73
'fifth'
74
],
75
new Selection(1, 4, 2, 1)
76
);
77
});
78
79
test('bug9511', () => {
80
testBlockCommentCommand(
81
[
82
'first',
83
'\tsecond line',
84
'third line',
85
'fourth line',
86
'fifth'
87
],
88
new Selection(1, 6, 1, 1),
89
[
90
'<0 first 0>',
91
'\tsecond line',
92
'third line',
93
'fourth line',
94
'fifth'
95
],
96
new Selection(1, 4, 1, 9)
97
);
98
99
testBlockCommentCommand(
100
[
101
'<0first0>',
102
'\tsecond line',
103
'third line',
104
'fourth line',
105
'fifth'
106
],
107
new Selection(1, 8, 1, 3),
108
[
109
'first',
110
'\tsecond line',
111
'third line',
112
'fourth line',
113
'fifth'
114
],
115
new Selection(1, 1, 1, 6)
116
);
117
});
118
119
test('one line selection', function () {
120
testBlockCommentCommand(
121
[
122
'first',
123
'\tsecond line',
124
'third line',
125
'fourth line',
126
'fifth'
127
],
128
new Selection(1, 6, 1, 3),
129
[
130
'fi<0 rst 0>',
131
'\tsecond line',
132
'third line',
133
'fourth line',
134
'fifth'
135
],
136
new Selection(1, 6, 1, 9)
137
);
138
});
139
140
test('one line selection toggle', function () {
141
testBlockCommentCommand(
142
[
143
'first',
144
'\tsecond line',
145
'third line',
146
'fourth line',
147
'fifth'
148
],
149
new Selection(1, 6, 1, 3),
150
[
151
'fi<0 rst 0>',
152
'\tsecond line',
153
'third line',
154
'fourth line',
155
'fifth'
156
],
157
new Selection(1, 6, 1, 9)
158
);
159
160
testBlockCommentCommand(
161
[
162
'fi<0rst0>',
163
'\tsecond line',
164
'third line',
165
'fourth line',
166
'fifth'
167
],
168
new Selection(1, 8, 1, 5),
169
[
170
'first',
171
'\tsecond line',
172
'third line',
173
'fourth line',
174
'fifth'
175
],
176
new Selection(1, 3, 1, 6)
177
);
178
179
testBlockCommentCommand(
180
[
181
'<0 first 0>',
182
'\tsecond line',
183
'third line',
184
'fourth line',
185
'fifth'
186
],
187
new Selection(1, 10, 1, 1),
188
[
189
'first',
190
'\tsecond line',
191
'third line',
192
'fourth line',
193
'fifth'
194
],
195
new Selection(1, 1, 1, 6)
196
);
197
198
testBlockCommentCommand(
199
[
200
'<0 first0>',
201
'\tsecond line',
202
'third line',
203
'fourth line',
204
'fifth'
205
],
206
new Selection(1, 9, 1, 1),
207
[
208
'first',
209
'\tsecond line',
210
'third line',
211
'fourth line',
212
'fifth'
213
],
214
new Selection(1, 1, 1, 6)
215
);
216
217
testBlockCommentCommand(
218
[
219
'<0first 0>',
220
'\tsecond line',
221
'third line',
222
'fourth line',
223
'fifth'
224
],
225
new Selection(1, 9, 1, 1),
226
[
227
'first',
228
'\tsecond line',
229
'third line',
230
'fourth line',
231
'fifth'
232
],
233
new Selection(1, 1, 1, 6)
234
);
235
236
testBlockCommentCommand(
237
[
238
'fi<0rst0>',
239
'\tsecond line',
240
'third line',
241
'fourth line',
242
'fifth'
243
],
244
new Selection(1, 8, 1, 5),
245
[
246
'first',
247
'\tsecond line',
248
'third line',
249
'fourth line',
250
'fifth'
251
],
252
new Selection(1, 3, 1, 6)
253
);
254
});
255
256
test('multi line selection', function () {
257
testBlockCommentCommand(
258
[
259
'first',
260
'\tsecond line',
261
'third line',
262
'fourth line',
263
'fifth'
264
],
265
new Selection(2, 4, 1, 1),
266
[
267
'<0 first',
268
'\tse 0>cond line',
269
'third line',
270
'fourth line',
271
'fifth'
272
],
273
new Selection(1, 4, 2, 4)
274
);
275
});
276
277
test('multi line selection toggle', function () {
278
testBlockCommentCommand(
279
[
280
'first',
281
'\tsecond line',
282
'third line',
283
'fourth line',
284
'fifth'
285
],
286
new Selection(2, 4, 1, 1),
287
[
288
'<0 first',
289
'\tse 0>cond line',
290
'third line',
291
'fourth line',
292
'fifth'
293
],
294
new Selection(1, 4, 2, 4)
295
);
296
297
testBlockCommentCommand(
298
[
299
'<0first',
300
'\tse0>cond line',
301
'third line',
302
'fourth line',
303
'fifth'
304
],
305
new Selection(2, 4, 1, 3),
306
[
307
'first',
308
'\tsecond line',
309
'third line',
310
'fourth line',
311
'fifth'
312
],
313
new Selection(1, 1, 2, 4)
314
);
315
316
testBlockCommentCommand(
317
[
318
'<0 first',
319
'\tse0>cond line',
320
'third line',
321
'fourth line',
322
'fifth'
323
],
324
new Selection(2, 4, 1, 3),
325
[
326
'first',
327
'\tsecond line',
328
'third line',
329
'fourth line',
330
'fifth'
331
],
332
new Selection(1, 1, 2, 4)
333
);
334
335
testBlockCommentCommand(
336
[
337
'<0first',
338
'\tse 0>cond line',
339
'third line',
340
'fourth line',
341
'fifth'
342
],
343
new Selection(2, 4, 1, 3),
344
[
345
'first',
346
'\tsecond line',
347
'third line',
348
'fourth line',
349
'fifth'
350
],
351
new Selection(1, 1, 2, 4)
352
);
353
354
testBlockCommentCommand(
355
[
356
'<0 first',
357
'\tse 0>cond line',
358
'third line',
359
'fourth line',
360
'fifth'
361
],
362
new Selection(2, 4, 1, 3),
363
[
364
'first',
365
'\tsecond line',
366
'third line',
367
'fourth line',
368
'fifth'
369
],
370
new Selection(1, 1, 2, 4)
371
);
372
});
373
374
test('fuzzy removes', function () {
375
testBlockCommentCommand(
376
[
377
'asd <0 qwe',
378
'asd 0> qwe'
379
],
380
new Selection(2, 5, 1, 7),
381
[
382
'asd qwe',
383
'asd qwe'
384
],
385
new Selection(1, 5, 2, 4)
386
);
387
388
testBlockCommentCommand(
389
[
390
'asd <0 qwe',
391
'asd 0> qwe'
392
],
393
new Selection(2, 5, 1, 6),
394
[
395
'asd qwe',
396
'asd qwe'
397
],
398
new Selection(1, 5, 2, 4)
399
);
400
401
testBlockCommentCommand(
402
[
403
'asd <0 qwe',
404
'asd 0> qwe'
405
],
406
new Selection(2, 5, 1, 5),
407
[
408
'asd qwe',
409
'asd qwe'
410
],
411
new Selection(1, 5, 2, 4)
412
);
413
414
testBlockCommentCommand(
415
[
416
'asd <0 qwe',
417
'asd 0> qwe'
418
],
419
new Selection(2, 5, 1, 11),
420
[
421
'asd qwe',
422
'asd qwe'
423
],
424
new Selection(1, 5, 2, 4)
425
);
426
427
testBlockCommentCommand(
428
[
429
'asd <0 qwe',
430
'asd 0> qwe'
431
],
432
new Selection(2, 1, 1, 11),
433
[
434
'asd qwe',
435
'asd qwe'
436
],
437
new Selection(1, 5, 2, 4)
438
);
439
440
testBlockCommentCommand(
441
[
442
'asd <0 qwe',
443
'asd 0> qwe'
444
],
445
new Selection(2, 7, 1, 11),
446
[
447
'asd qwe',
448
'asd qwe'
449
],
450
new Selection(1, 5, 2, 4)
451
);
452
});
453
454
test('bug #30358', function () {
455
testBlockCommentCommand(
456
[
457
'<0 start 0> middle end',
458
],
459
new Selection(1, 20, 1, 23),
460
[
461
'<0 start 0> middle <0 end 0>'
462
],
463
new Selection(1, 23, 1, 26)
464
);
465
466
testBlockCommentCommand(
467
[
468
'<0 start 0> middle <0 end 0>'
469
],
470
new Selection(1, 13, 1, 19),
471
[
472
'<0 start 0> <0 middle 0> <0 end 0>'
473
],
474
new Selection(1, 16, 1, 22)
475
);
476
});
477
478
test('issue #34618', function () {
479
testBlockCommentCommand(
480
[
481
'<0 0> middle end',
482
],
483
new Selection(1, 4, 1, 4),
484
[
485
' middle end'
486
],
487
new Selection(1, 1, 1, 1)
488
);
489
});
490
491
test('insertSpace false', () => {
492
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
493
_testCommentCommand(lines, selection, (accessor, sel) => new BlockCommentCommand(sel, false, accessor.get(ILanguageConfigurationService)), expectedLines, expectedSelection);
494
}
495
496
testLineCommentCommand(
497
[
498
'some text'
499
],
500
new Selection(1, 1, 1, 5),
501
[
502
'<0some0> text'
503
],
504
new Selection(1, 3, 1, 7)
505
);
506
});
507
508
test('insertSpace false does not remove space', () => {
509
function testLineCommentCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
510
_testCommentCommand(lines, selection, (accessor, sel) => new BlockCommentCommand(sel, false, accessor.get(ILanguageConfigurationService)), expectedLines, expectedSelection);
511
}
512
513
testLineCommentCommand(
514
[
515
'<0 some 0> text'
516
],
517
new Selection(1, 4, 1, 8),
518
[
519
' some text'
520
],
521
new Selection(1, 1, 1, 7)
522
);
523
});
524
});
525
526