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