Path: blob/main/src/vs/editor/contrib/linesOperations/test/browser/linesOperations.test.ts
4780 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*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';6import { CoreEditingCommands } from '../../../../browser/coreCommands.js';7import type { ICodeEditor } from '../../../../browser/editorBrowser.js';8import { EditorAction } from '../../../../browser/editorExtensions.js';9import { Position } from '../../../../common/core/position.js';10import { Selection } from '../../../../common/core/selection.js';11import { Handler } from '../../../../common/editorCommon.js';12import { ITextModel } from '../../../../common/model.js';13import { ViewModel } from '../../../../common/viewModel/viewModelImpl.js';14import { CamelCaseAction, PascalCaseAction, DeleteAllLeftAction, DeleteAllRightAction, DeleteDuplicateLinesAction, DeleteLinesAction, IndentLinesAction, InsertLineAfterAction, InsertLineBeforeAction, JoinLinesAction, KebabCaseAction, LowerCaseAction, SnakeCaseAction, SortLinesAscendingAction, SortLinesDescendingAction, TitleCaseAction, TransposeAction, UpperCaseAction, ReverseLinesAction } from '../../browser/linesOperations.js';15import { withTestCodeEditor } from '../../../../test/browser/testCodeEditor.js';16import { createTextModel } from '../../../../test/common/testTextModel.js';1718function assertSelection(editor: ICodeEditor, expected: Selection | Selection[]): void {19if (!Array.isArray(expected)) {20expected = [expected];21}22assert.deepStrictEqual(editor.getSelections(), expected);23}2425function executeAction(action: EditorAction, editor: ICodeEditor): void {26action.run(null!, editor, undefined);27}2829suite('Editor Contrib - Line Operations', () => {3031ensureNoDisposablesAreLeakedInTestSuite();3233suite('SortLinesAscendingAction', () => {34test('should sort selected lines in ascending order', function () {35withTestCodeEditor(36[37'omicron',38'beta',39'alpha'40], {}, (editor) => {41const model = editor.getModel()!;42const sortLinesAscendingAction = new SortLinesAscendingAction();4344editor.setSelection(new Selection(1, 1, 3, 5));45executeAction(sortLinesAscendingAction, editor);46assert.deepStrictEqual(model.getLinesContent(), [47'alpha',48'beta',49'omicron'50]);51assertSelection(editor, new Selection(1, 1, 3, 7));52});53});5455test('should sort lines in ascending order', function () {56withTestCodeEditor(57[58'omicron',59'beta',60'alpha'61], {}, (editor) => {62const model = editor.getModel()!;63const sortLinesAscendingAction = new SortLinesAscendingAction();6465executeAction(sortLinesAscendingAction, editor);66assert.deepStrictEqual(model.getLinesContent(), [67'alpha',68'beta',69'omicron'70]);71});72});7374test('should sort multiple selections in ascending order', function () {75withTestCodeEditor(76[77'omicron',78'beta',79'alpha',80'',81'omicron',82'beta',83'alpha'84], {}, (editor) => {85const model = editor.getModel()!;86const sortLinesAscendingAction = new SortLinesAscendingAction();8788editor.setSelections([new Selection(1, 1, 3, 5), new Selection(5, 1, 7, 5)]);89executeAction(sortLinesAscendingAction, editor);90assert.deepStrictEqual(model.getLinesContent(), [91'alpha',92'beta',93'omicron',94'',95'alpha',96'beta',97'omicron'98]);99const expectedSelections = [100new Selection(1, 1, 3, 7),101new Selection(5, 1, 7, 7)102];103editor.getSelections()!.forEach((actualSelection, index) => {104assert.deepStrictEqual(actualSelection.toString(), expectedSelections[index].toString());105});106});107});108109test('applies to whole document when selection is single line', function () {110withTestCodeEditor(111[112'omicron',113'beta',114'alpha'115], {}, (editor) => {116const model = editor.getModel()!;117const sortLinesAscendingAction = new SortLinesAscendingAction();118119editor.setSelection(new Selection(2, 1, 2, 4));120executeAction(sortLinesAscendingAction, editor);121assert.deepStrictEqual(model.getLinesContent(), [122'alpha',123'beta',124'omicron'125]);126});127});128});129130suite('SortLinesDescendingAction', () => {131test('should sort selected lines in descending order', function () {132withTestCodeEditor(133[134'alpha',135'beta',136'omicron'137], {}, (editor) => {138const model = editor.getModel()!;139const sortLinesDescendingAction = new SortLinesDescendingAction();140141editor.setSelection(new Selection(1, 1, 3, 7));142executeAction(sortLinesDescendingAction, editor);143assert.deepStrictEqual(model.getLinesContent(), [144'omicron',145'beta',146'alpha'147]);148assertSelection(editor, new Selection(1, 1, 3, 5));149});150});151152test('should sort multiple selections in descending order', function () {153withTestCodeEditor(154[155'alpha',156'beta',157'omicron',158'',159'alpha',160'beta',161'omicron'162], {}, (editor) => {163const model = editor.getModel()!;164const sortLinesDescendingAction = new SortLinesDescendingAction();165166editor.setSelections([new Selection(1, 1, 3, 7), new Selection(5, 1, 7, 7)]);167executeAction(sortLinesDescendingAction, editor);168assert.deepStrictEqual(model.getLinesContent(), [169'omicron',170'beta',171'alpha',172'',173'omicron',174'beta',175'alpha'176]);177const expectedSelections = [178new Selection(1, 1, 3, 5),179new Selection(5, 1, 7, 5)180];181editor.getSelections()!.forEach((actualSelection, index) => {182assert.deepStrictEqual(actualSelection.toString(), expectedSelections[index].toString());183});184});185});186});187188suite('DeleteDuplicateLinesAction', () => {189test('should remove duplicate lines within selection', function () {190withTestCodeEditor(191[192'alpha',193'beta',194'beta',195'beta',196'alpha',197'omicron',198], {}, (editor) => {199const model = editor.getModel()!;200const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction();201202editor.setSelection(new Selection(1, 3, 6, 4));203executeAction(deleteDuplicateLinesAction, editor);204assert.deepStrictEqual(model.getLinesContent(), [205'alpha',206'beta',207'omicron',208]);209assertSelection(editor, new Selection(1, 1, 3, 8));210});211});212213test('should remove duplicate lines', function () {214withTestCodeEditor(215[216'alpha',217'beta',218'beta',219'beta',220'alpha',221'omicron',222], {}, (editor) => {223const model = editor.getModel()!;224const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction();225226executeAction(deleteDuplicateLinesAction, editor);227assert.deepStrictEqual(model.getLinesContent(), [228'alpha',229'beta',230'omicron',231]);232assert.ok(editor.getSelection().isEmpty());233});234});235236test('should remove duplicate lines in multiple selections', function () {237withTestCodeEditor(238[239'alpha',240'beta',241'beta',242'omicron',243'',244'alpha',245'alpha',246'beta'247], {}, (editor) => {248const model = editor.getModel()!;249const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction();250251editor.setSelections([new Selection(1, 2, 4, 3), new Selection(6, 2, 8, 3)]);252executeAction(deleteDuplicateLinesAction, editor);253assert.deepStrictEqual(model.getLinesContent(), [254'alpha',255'beta',256'omicron',257'',258'alpha',259'beta'260]);261const expectedSelections = [262new Selection(1, 1, 3, 8),263new Selection(5, 1, 6, 5)264];265editor.getSelections()!.forEach((actualSelection, index) => {266assert.deepStrictEqual(actualSelection.toString(), expectedSelections[index].toString());267});268});269});270271test('applies to whole document when selection is single line', function () {272withTestCodeEditor(273[274'alpha',275'beta',276'alpha',277'omicron'278], {}, (editor) => {279const model = editor.getModel()!;280const deleteDuplicateLinesAction = new DeleteDuplicateLinesAction();281282editor.setSelection(new Selection(2, 1, 2, 2));283executeAction(deleteDuplicateLinesAction, editor);284assert.deepStrictEqual(model.getLinesContent(), ['alpha', 'beta', 'omicron']);285});286});287});288289290suite('DeleteAllLeftAction', () => {291test('should delete to the left of the cursor', function () {292withTestCodeEditor(293[294'one',295'two',296'three'297], {}, (editor) => {298const model = editor.getModel()!;299const deleteAllLeftAction = new DeleteAllLeftAction();300301editor.setSelection(new Selection(1, 2, 1, 2));302executeAction(deleteAllLeftAction, editor);303assert.strictEqual(model.getLineContent(1), 'ne');304305editor.setSelections([new Selection(2, 2, 2, 2), new Selection(3, 2, 3, 2)]);306executeAction(deleteAllLeftAction, editor);307assert.strictEqual(model.getLineContent(2), 'wo');308assert.strictEqual(model.getLineContent(3), 'hree');309});310});311312test('should jump to the previous line when on first column', function () {313withTestCodeEditor(314[315'one',316'two',317'three'318], {}, (editor) => {319const model = editor.getModel()!;320const deleteAllLeftAction = new DeleteAllLeftAction();321322editor.setSelection(new Selection(2, 1, 2, 1));323executeAction(deleteAllLeftAction, editor);324assert.strictEqual(model.getLineContent(1), 'onetwo');325326editor.setSelections([new Selection(1, 1, 1, 1), new Selection(2, 1, 2, 1)]);327executeAction(deleteAllLeftAction, editor);328assert.strictEqual(model.getLinesContent()[0], 'onetwothree');329assert.strictEqual(model.getLinesContent().length, 1);330331editor.setSelection(new Selection(1, 1, 1, 1));332executeAction(deleteAllLeftAction, editor);333assert.strictEqual(model.getLinesContent()[0], 'onetwothree');334});335});336337test('should keep deleting lines in multi cursor mode', function () {338withTestCodeEditor(339[340'hi my name is Carlos Matos',341'BCC',342'waso waso waso',343'my wife doesnt believe in me',344'nonononono',345'bitconneeeect'346], {}, (editor) => {347const model = editor.getModel()!;348const deleteAllLeftAction = new DeleteAllLeftAction();349350const beforeSecondWasoSelection = new Selection(3, 5, 3, 5);351const endOfBCCSelection = new Selection(2, 4, 2, 4);352const endOfNonono = new Selection(5, 11, 5, 11);353354editor.setSelections([beforeSecondWasoSelection, endOfBCCSelection, endOfNonono]);355356executeAction(deleteAllLeftAction, editor);357let selections = editor.getSelections()!;358359assert.strictEqual(model.getLineContent(2), '');360assert.strictEqual(model.getLineContent(3), ' waso waso');361assert.strictEqual(model.getLineContent(5), '');362363assert.deepStrictEqual([364selections[0].startLineNumber,365selections[0].startColumn,366selections[0].endLineNumber,367selections[0].endColumn368], [3, 1, 3, 1]);369370assert.deepStrictEqual([371selections[1].startLineNumber,372selections[1].startColumn,373selections[1].endLineNumber,374selections[1].endColumn375], [2, 1, 2, 1]);376377assert.deepStrictEqual([378selections[2].startLineNumber,379selections[2].startColumn,380selections[2].endLineNumber,381selections[2].endColumn382], [5, 1, 5, 1]);383384executeAction(deleteAllLeftAction, editor);385selections = editor.getSelections()!;386387assert.strictEqual(model.getLineContent(1), 'hi my name is Carlos Matos waso waso');388assert.strictEqual(selections.length, 2);389390assert.deepStrictEqual([391selections[0].startLineNumber,392selections[0].startColumn,393selections[0].endLineNumber,394selections[0].endColumn395], [1, 27, 1, 27]);396397assert.deepStrictEqual([398selections[1].startLineNumber,399selections[1].startColumn,400selections[1].endLineNumber,401selections[1].endColumn402], [2, 29, 2, 29]);403});404});405406test('should work in multi cursor mode', function () {407withTestCodeEditor(408[409'hello',410'world',411'hello world',412'hello',413'bonjour',414'hola',415'world',416'hello world',417], {}, (editor) => {418const model = editor.getModel()!;419const deleteAllLeftAction = new DeleteAllLeftAction();420421editor.setSelections([new Selection(1, 2, 1, 2), new Selection(1, 4, 1, 4)]);422executeAction(deleteAllLeftAction, editor);423assert.strictEqual(model.getLineContent(1), 'lo');424425editor.setSelections([new Selection(2, 2, 2, 2), new Selection(2, 4, 2, 5)]);426executeAction(deleteAllLeftAction, editor);427assert.strictEqual(model.getLineContent(2), 'd');428429editor.setSelections([new Selection(3, 2, 3, 5), new Selection(3, 7, 3, 7)]);430executeAction(deleteAllLeftAction, editor);431assert.strictEqual(model.getLineContent(3), 'world');432433editor.setSelections([new Selection(4, 3, 4, 3), new Selection(4, 5, 5, 4)]);434executeAction(deleteAllLeftAction, editor);435assert.strictEqual(model.getLineContent(4), 'jour');436437editor.setSelections([new Selection(5, 3, 6, 3), new Selection(6, 5, 7, 5), new Selection(7, 7, 7, 7)]);438executeAction(deleteAllLeftAction, editor);439assert.strictEqual(model.getLineContent(5), 'world');440});441});442443test('issue #36234: should push undo stop', () => {444withTestCodeEditor(445[446'one',447'two',448'three'449], {}, (editor) => {450const model = editor.getModel()!;451const deleteAllLeftAction = new DeleteAllLeftAction();452453editor.setSelection(new Selection(1, 1, 1, 1));454455editor.trigger('keyboard', Handler.Type, { text: 'Typing some text here on line ' });456assert.strictEqual(model.getLineContent(1), 'Typing some text here on line one');457assert.deepStrictEqual(editor.getSelection(), new Selection(1, 31, 1, 31));458459executeAction(deleteAllLeftAction, editor);460assert.strictEqual(model.getLineContent(1), 'one');461assert.deepStrictEqual(editor.getSelection(), new Selection(1, 1, 1, 1));462463editor.runCommand(CoreEditingCommands.Undo, null);464assert.strictEqual(model.getLineContent(1), 'Typing some text here on line one');465assert.deepStrictEqual(editor.getSelection(), new Selection(1, 31, 1, 31));466});467});468});469470suite('JoinLinesAction', () => {471test('should join lines and insert space if necessary', function () {472withTestCodeEditor(473[474'hello',475'world',476'hello ',477'world',478'hello ',479' world',480'hello ',481' world',482'',483'',484'hello world'485], {}, (editor) => {486const model = editor.getModel()!;487const joinLinesAction = new JoinLinesAction();488489editor.setSelection(new Selection(1, 2, 1, 2));490executeAction(joinLinesAction, editor);491assert.strictEqual(model.getLineContent(1), 'hello world');492assertSelection(editor, new Selection(1, 6, 1, 6));493494editor.setSelection(new Selection(2, 2, 2, 2));495executeAction(joinLinesAction, editor);496assert.strictEqual(model.getLineContent(2), 'hello world');497assertSelection(editor, new Selection(2, 7, 2, 7));498499editor.setSelection(new Selection(3, 2, 3, 2));500executeAction(joinLinesAction, editor);501assert.strictEqual(model.getLineContent(3), 'hello world');502assertSelection(editor, new Selection(3, 7, 3, 7));503504editor.setSelection(new Selection(4, 2, 5, 3));505executeAction(joinLinesAction, editor);506assert.strictEqual(model.getLineContent(4), 'hello world');507assertSelection(editor, new Selection(4, 2, 4, 8));508509editor.setSelection(new Selection(5, 1, 7, 3));510executeAction(joinLinesAction, editor);511assert.strictEqual(model.getLineContent(5), 'hello world');512assertSelection(editor, new Selection(5, 1, 5, 3));513});514});515516test('#50471 Join lines at the end of document', function () {517withTestCodeEditor(518[519'hello',520'world'521], {}, (editor) => {522const model = editor.getModel()!;523const joinLinesAction = new JoinLinesAction();524525editor.setSelection(new Selection(2, 1, 2, 1));526executeAction(joinLinesAction, editor);527assert.strictEqual(model.getLineContent(1), 'hello');528assert.strictEqual(model.getLineContent(2), 'world');529assertSelection(editor, new Selection(2, 6, 2, 6));530});531});532533test('should work in multi cursor mode', function () {534withTestCodeEditor(535[536'hello',537'world',538'hello ',539'world',540'hello ',541' world',542'hello ',543' world',544'',545'',546'hello world'547], {}, (editor) => {548const model = editor.getModel()!;549const joinLinesAction = new JoinLinesAction();550551editor.setSelections([552/** primary cursor */553new Selection(5, 2, 5, 2),554new Selection(1, 2, 1, 2),555new Selection(3, 2, 4, 2),556new Selection(5, 4, 6, 3),557new Selection(7, 5, 8, 4),558new Selection(10, 1, 10, 1)559]);560561executeAction(joinLinesAction, editor);562assert.strictEqual(model.getLinesContent().join('\n'), 'hello world\nhello world\nhello world\nhello world\n\nhello world');563assertSelection(editor, [564/** primary cursor */565new Selection(3, 4, 3, 8),566new Selection(1, 6, 1, 6),567new Selection(2, 2, 2, 8),568new Selection(4, 5, 4, 9),569new Selection(6, 1, 6, 1)570]);571});572});573574test('should push undo stop', function () {575withTestCodeEditor(576[577'hello',578'world'579], {}, (editor) => {580const model = editor.getModel()!;581const joinLinesAction = new JoinLinesAction();582583editor.setSelection(new Selection(1, 6, 1, 6));584585editor.trigger('keyboard', Handler.Type, { text: ' my dear' });586assert.strictEqual(model.getLineContent(1), 'hello my dear');587assert.deepStrictEqual(editor.getSelection(), new Selection(1, 14, 1, 14));588589executeAction(joinLinesAction, editor);590assert.strictEqual(model.getLineContent(1), 'hello my dear world');591assert.deepStrictEqual(editor.getSelection(), new Selection(1, 14, 1, 14));592593editor.runCommand(CoreEditingCommands.Undo, null);594assert.strictEqual(model.getLineContent(1), 'hello my dear');595assert.deepStrictEqual(editor.getSelection(), new Selection(1, 14, 1, 14));596});597});598});599600suite('ReverseLinesAction', () => {601test('reverses lines', function () {602withTestCodeEditor(603[604'alice',605'bob',606'charlie',607], {}, (editor) => {608const model = editor.getModel()!;609const reverseLinesAction = new ReverseLinesAction();610611executeAction(reverseLinesAction, editor);612assert.deepStrictEqual(model.getLinesContent(), ['charlie', 'bob', 'alice']);613});614});615616test('excludes empty last line', function () {617withTestCodeEditor(618[619'alice',620'bob',621'charlie',622'',623], {}, (editor) => {624const model = editor.getModel()!;625const reverseLinesAction = new ReverseLinesAction();626627executeAction(reverseLinesAction, editor);628assert.deepStrictEqual(model.getLinesContent(), ['charlie', 'bob', 'alice', '']);629});630});631632test('updates cursor', function () {633withTestCodeEditor(634[635'alice',636'bob',637'charlie',638], {}, (editor) => {639const reverseLinesAction = new ReverseLinesAction();640// cursor at third column of third line 'charlie'641editor.setPosition(new Position(3, 3));642643executeAction(reverseLinesAction, editor);644// cursor at third column of *first* line 'charlie'645assert.deepStrictEqual(editor.getPosition(), new Position(1, 3));646});647});648649test('preserves cursor on empty last line', function () {650withTestCodeEditor(651[652'alice',653'bob',654'charlie',655'',656], {}, (editor) => {657const reverseLinesAction = new ReverseLinesAction();658editor.setPosition(new Position(4, 1));659660executeAction(reverseLinesAction, editor);661assert.deepStrictEqual(editor.getPosition(), new Position(4, 1));662});663});664665test('preserves selected text when selections do not span lines', function () {666withTestCodeEditor(667[668'alice',669'bob',670'charlie',671'',672], {}, (editor) => {673const model = editor.getModel()!;674const reverseLinesAction = new ReverseLinesAction();675editor.setSelections([new Selection(1, 1, 1, 3), new Selection(2, 1, 2, 4), new Selection(3, 1, 3, 5)]);676const expectedSelectedText: string[] = ['al', 'bob', 'char'];677assert.deepStrictEqual(editor.getSelections().map(s => model.getValueInRange(s)), expectedSelectedText);678679executeAction(reverseLinesAction, editor);680assert.deepStrictEqual(editor.getSelections().map(s => model.getValueInRange(s)), expectedSelectedText);681});682});683684test('reverses lines within selection', function () {685withTestCodeEditor(686[687'line1',688'line2',689'line3',690'line4',691'line5',692], {}, (editor) => {693const model = editor.getModel()!;694const reverseLinesAction = new ReverseLinesAction();695696// Select lines 2-4697editor.setSelection(new Selection(2, 1, 4, 6));698executeAction(reverseLinesAction, editor);699assert.deepStrictEqual(model.getLinesContent(), ['line1', 'line4', 'line3', 'line2', 'line5']);700});701});702703test('reverses lines within partial selection', function () {704withTestCodeEditor(705[706'line1',707'line2',708'line3',709'line4',710'line5',711], {}, (editor) => {712const model = editor.getModel()!;713const reverseLinesAction = new ReverseLinesAction();714715// Select partial lines 2-4 (from middle of line2 to middle of line4)716editor.setSelection(new Selection(2, 3, 4, 3));717executeAction(reverseLinesAction, editor);718assert.deepStrictEqual(model.getLinesContent(), ['line1', 'line4', 'line3', 'line2', 'line5']);719});720});721722test('reverses lines with multiple selections', function () {723withTestCodeEditor(724[725'line1',726'line2',727'line3',728'line4',729'line5',730'line6',731], {}, (editor) => {732const model = editor.getModel()!;733const reverseLinesAction = new ReverseLinesAction();734735// Select lines 1-2 and lines 4-5736editor.setSelections([new Selection(1, 1, 2, 6), new Selection(4, 1, 5, 6)]);737executeAction(reverseLinesAction, editor);738assert.deepStrictEqual(model.getLinesContent(), ['line2', 'line1', 'line3', 'line5', 'line4', 'line6']);739});740});741742test('updates selection positions after reversal', function () {743withTestCodeEditor(744[745'line1',746'line2',747'line3',748'line4',749], {}, (editor) => {750const reverseLinesAction = new ReverseLinesAction();751752// Select lines 1-3753editor.setSelection(new Selection(1, 2, 3, 3));754executeAction(reverseLinesAction, editor);755756// After reversal, selection should be updated to maintain relative position757// Originally line 1 col 2 -> line 3 col 3, so after reversal should be line 3 col 2 -> line 1 col 3758const selection = editor.getSelection()!;759// The selection should cover the same logical text after reversal760// Range normalization ensures startLineNumber <= endLineNumber761assert.strictEqual(selection.startLineNumber, 1);762assert.strictEqual(selection.startColumn, 3);763assert.strictEqual(selection.endLineNumber, 3);764assert.strictEqual(selection.endColumn, 2);765});766});767768test('applies to whole document when selection is single line', function () {769withTestCodeEditor(770[771'line1',772'line2',773'line3',774], {}, (editor) => {775const model = editor.getModel()!;776const reverseLinesAction = new ReverseLinesAction();777778// Select only line 2779editor.setSelection(new Selection(2, 1, 2, 6));780executeAction(reverseLinesAction, editor);781assert.deepStrictEqual(model.getLinesContent(), ['line3', 'line2', 'line1']);782});783});784785test('excludes end line when selection ends at column 1', function () {786withTestCodeEditor(787[788'line1',789'line2',790'line3',791'line4',792'line5',793], {}, (editor) => {794const model = editor.getModel()!;795const reverseLinesAction = new ReverseLinesAction();796797// Select from line 2 to line 4 column 1 (should exclude line 4)798editor.setSelection(new Selection(2, 1, 4, 1));799executeAction(reverseLinesAction, editor);800assert.deepStrictEqual(model.getLinesContent(), ['line1', 'line3', 'line2', 'line4', 'line5']);801});802});803804test('applies to whole document when selection is single line', function () {805withTestCodeEditor(806[807'omicron',808'beta',809'alpha'810], {}, (editor) => {811const model = editor.getModel()!;812const reverseLinesAction = new ReverseLinesAction();813814editor.setSelection(new Selection(2, 1, 2, 4));815executeAction(reverseLinesAction, editor);816assert.deepStrictEqual(model.getLinesContent(), [817'alpha',818'beta',819'omicron'820]);821});822});823});824825test('transpose', () => {826withTestCodeEditor(827[828'hello world',829'',830'',831' ',832], {}, (editor) => {833const model = editor.getModel()!;834const transposeAction = new TransposeAction();835836editor.setSelection(new Selection(1, 1, 1, 1));837executeAction(transposeAction, editor);838assert.strictEqual(model.getLineContent(1), 'hello world');839assertSelection(editor, new Selection(1, 2, 1, 2));840841editor.setSelection(new Selection(1, 6, 1, 6));842executeAction(transposeAction, editor);843assert.strictEqual(model.getLineContent(1), 'hell oworld');844assertSelection(editor, new Selection(1, 7, 1, 7));845846editor.setSelection(new Selection(1, 12, 1, 12));847executeAction(transposeAction, editor);848assert.strictEqual(model.getLineContent(1), 'hell oworl');849assertSelection(editor, new Selection(2, 2, 2, 2));850851editor.setSelection(new Selection(3, 1, 3, 1));852executeAction(transposeAction, editor);853assert.strictEqual(model.getLineContent(3), '');854assertSelection(editor, new Selection(4, 1, 4, 1));855856editor.setSelection(new Selection(4, 2, 4, 2));857executeAction(transposeAction, editor);858assert.strictEqual(model.getLineContent(4), ' ');859assertSelection(editor, new Selection(4, 3, 4, 3));860}861);862863// fix #16633864withTestCodeEditor(865[866'',867'',868'hello',869'world',870'',871'hello world',872'',873'hello world'874], {}, (editor) => {875const model = editor.getModel()!;876const transposeAction = new TransposeAction();877878editor.setSelection(new Selection(1, 1, 1, 1));879executeAction(transposeAction, editor);880assert.strictEqual(model.getLineContent(2), '');881assertSelection(editor, new Selection(2, 1, 2, 1));882883editor.setSelection(new Selection(3, 6, 3, 6));884executeAction(transposeAction, editor);885assert.strictEqual(model.getLineContent(4), 'oworld');886assertSelection(editor, new Selection(4, 2, 4, 2));887888editor.setSelection(new Selection(6, 12, 6, 12));889executeAction(transposeAction, editor);890assert.strictEqual(model.getLineContent(7), 'd');891assertSelection(editor, new Selection(7, 2, 7, 2));892893editor.setSelection(new Selection(8, 12, 8, 12));894executeAction(transposeAction, editor);895assert.strictEqual(model.getLineContent(8), 'hello world');896assertSelection(editor, new Selection(8, 12, 8, 12));897}898);899});900901test('toggle case', function () {902withTestCodeEditor(903[904'hello world',905'öçşğü',906'parseHTMLString',907'getElementById',908'insertHTML',909'PascalCase',910'CSSSelectorsList',911'iD',912'tEST',913'öçşÖÇŞğüĞÜ',914'audioConverter.convertM4AToMP3();',915'snake_case',916'Capital_Snake_Case',917`function helloWorld() {918return someGlobalObject.printHelloWorld("en", "utf-8");919}920helloWorld();`.replace(/^\s+/gm, ''),921`'JavaScript'`,922'parseHTML4String',923'_accessor: ServicesAccessor'924], {}, (editor) => {925const model = editor.getModel()!;926const uppercaseAction = new UpperCaseAction();927const lowercaseAction = new LowerCaseAction();928const titlecaseAction = new TitleCaseAction();929const snakecaseAction = new SnakeCaseAction();930931editor.setSelection(new Selection(1, 1, 1, 12));932executeAction(uppercaseAction, editor);933assert.strictEqual(model.getLineContent(1), 'HELLO WORLD');934assertSelection(editor, new Selection(1, 1, 1, 12));935936editor.setSelection(new Selection(1, 1, 1, 12));937executeAction(lowercaseAction, editor);938assert.strictEqual(model.getLineContent(1), 'hello world');939assertSelection(editor, new Selection(1, 1, 1, 12));940941editor.setSelection(new Selection(1, 3, 1, 3));942executeAction(uppercaseAction, editor);943assert.strictEqual(model.getLineContent(1), 'HELLO world');944assertSelection(editor, new Selection(1, 3, 1, 3));945946editor.setSelection(new Selection(1, 4, 1, 4));947executeAction(lowercaseAction, editor);948assert.strictEqual(model.getLineContent(1), 'hello world');949assertSelection(editor, new Selection(1, 4, 1, 4));950951editor.setSelection(new Selection(1, 1, 1, 12));952executeAction(titlecaseAction, editor);953assert.strictEqual(model.getLineContent(1), 'Hello World');954assertSelection(editor, new Selection(1, 1, 1, 12));955956editor.setSelection(new Selection(2, 1, 2, 6));957executeAction(uppercaseAction, editor);958assert.strictEqual(model.getLineContent(2), 'ÖÇŞĞÜ');959assertSelection(editor, new Selection(2, 1, 2, 6));960961editor.setSelection(new Selection(2, 1, 2, 6));962executeAction(lowercaseAction, editor);963assert.strictEqual(model.getLineContent(2), 'öçşğü');964assertSelection(editor, new Selection(2, 1, 2, 6));965966editor.setSelection(new Selection(2, 1, 2, 6));967executeAction(titlecaseAction, editor);968assert.strictEqual(model.getLineContent(2), 'Öçşğü');969assertSelection(editor, new Selection(2, 1, 2, 6));970971editor.setSelection(new Selection(3, 1, 3, 16));972executeAction(snakecaseAction, editor);973assert.strictEqual(model.getLineContent(3), 'parse_html_string');974assertSelection(editor, new Selection(3, 1, 3, 18));975976editor.setSelection(new Selection(4, 1, 4, 15));977executeAction(snakecaseAction, editor);978assert.strictEqual(model.getLineContent(4), 'get_element_by_id');979assertSelection(editor, new Selection(4, 1, 4, 18));980981editor.setSelection(new Selection(5, 1, 5, 11));982executeAction(snakecaseAction, editor);983assert.strictEqual(model.getLineContent(5), 'insert_html');984assertSelection(editor, new Selection(5, 1, 5, 12));985986editor.setSelection(new Selection(6, 1, 6, 11));987executeAction(snakecaseAction, editor);988assert.strictEqual(model.getLineContent(6), 'pascal_case');989assertSelection(editor, new Selection(6, 1, 6, 12));990991editor.setSelection(new Selection(7, 1, 7, 17));992executeAction(snakecaseAction, editor);993assert.strictEqual(model.getLineContent(7), 'css_selectors_list');994assertSelection(editor, new Selection(7, 1, 7, 19));995996editor.setSelection(new Selection(8, 1, 8, 3));997executeAction(snakecaseAction, editor);998assert.strictEqual(model.getLineContent(8), 'i_d');999assertSelection(editor, new Selection(8, 1, 8, 4));10001001editor.setSelection(new Selection(9, 1, 9, 5));1002executeAction(snakecaseAction, editor);1003assert.strictEqual(model.getLineContent(9), 't_est');1004assertSelection(editor, new Selection(9, 1, 9, 6));10051006editor.setSelection(new Selection(10, 1, 10, 11));1007executeAction(snakecaseAction, editor);1008assert.strictEqual(model.getLineContent(10), 'öçş_öç_şğü_ğü');1009assertSelection(editor, new Selection(10, 1, 10, 14));10101011editor.setSelection(new Selection(11, 1, 11, 34));1012executeAction(snakecaseAction, editor);1013assert.strictEqual(model.getLineContent(11), 'audio_converter.convert_m4a_to_mp3();');1014assertSelection(editor, new Selection(11, 1, 11, 38));10151016editor.setSelection(new Selection(12, 1, 12, 11));1017executeAction(snakecaseAction, editor);1018assert.strictEqual(model.getLineContent(12), 'snake_case');1019assertSelection(editor, new Selection(12, 1, 12, 11));10201021editor.setSelection(new Selection(13, 1, 13, 19));1022executeAction(snakecaseAction, editor);1023assert.strictEqual(model.getLineContent(13), 'capital_snake_case');1024assertSelection(editor, new Selection(13, 1, 13, 19));10251026editor.setSelection(new Selection(14, 1, 17, 14));1027executeAction(snakecaseAction, editor);1028assert.strictEqual(model.getValueInRange(new Selection(14, 1, 17, 15)), `function hello_world() {1029return some_global_object.print_hello_world("en", "utf-8");1030}1031hello_world();`.replace(/^\s+/gm, ''));1032assertSelection(editor, new Selection(14, 1, 17, 15));10331034editor.setSelection(new Selection(18, 1, 18, 13));1035executeAction(snakecaseAction, editor);1036assert.strictEqual(model.getLineContent(18), `'java_script'`);1037assertSelection(editor, new Selection(18, 1, 18, 14));10381039editor.setSelection(new Selection(19, 1, 19, 17));1040executeAction(snakecaseAction, editor);1041assert.strictEqual(model.getLineContent(19), 'parse_html4_string');1042assertSelection(editor, new Selection(19, 1, 19, 19));10431044editor.setSelection(new Selection(20, 1, 20, 28));1045executeAction(snakecaseAction, editor);1046assert.strictEqual(model.getLineContent(20), '_accessor: services_accessor');1047assertSelection(editor, new Selection(20, 1, 20, 29));1048}1049);10501051withTestCodeEditor(1052[1053'foO baR BaZ',1054'foO\'baR\'BaZ',1055'foO[baR]BaZ',1056'foO`baR~BaZ',1057'foO^baR%BaZ',1058'foO$baR!BaZ',1059'\'physician\'s assistant\''1060], {}, (editor) => {1061const model = editor.getModel()!;1062const titlecaseAction = new TitleCaseAction();10631064editor.setSelection(new Selection(1, 1, 1, 12));1065executeAction(titlecaseAction, editor);1066assert.strictEqual(model.getLineContent(1), 'Foo Bar Baz');10671068editor.setSelection(new Selection(2, 1, 2, 12));1069executeAction(titlecaseAction, editor);1070assert.strictEqual(model.getLineContent(2), 'Foo\'bar\'baz');10711072editor.setSelection(new Selection(3, 1, 3, 12));1073executeAction(titlecaseAction, editor);1074assert.strictEqual(model.getLineContent(3), 'Foo[Bar]Baz');10751076editor.setSelection(new Selection(4, 1, 4, 12));1077executeAction(titlecaseAction, editor);1078assert.strictEqual(model.getLineContent(4), 'Foo`Bar~Baz');10791080editor.setSelection(new Selection(5, 1, 5, 12));1081executeAction(titlecaseAction, editor);1082assert.strictEqual(model.getLineContent(5), 'Foo^Bar%Baz');10831084editor.setSelection(new Selection(6, 1, 6, 12));1085executeAction(titlecaseAction, editor);1086assert.strictEqual(model.getLineContent(6), 'Foo$Bar!Baz');10871088editor.setSelection(new Selection(7, 1, 7, 23));1089executeAction(titlecaseAction, editor);1090assert.strictEqual(model.getLineContent(7), '\'Physician\'s Assistant\'');1091}1092);10931094withTestCodeEditor(1095[1096'camel from words',1097'from_snake_case',1098'from-kebab-case',1099'alreadyCamel',1100'ReTain_some_CAPitalization',1101'my_var.test_function()',1102'öçş_öç_şğü_ğü',1103'XMLHttpRequest',1104'\tfunction hello_world() {',1105'\t\treturn some_global_object;',1106'\t}',1107], {}, (editor) => {1108const model = editor.getModel()!;1109const camelcaseAction = new CamelCaseAction();11101111editor.setSelection(new Selection(1, 1, 1, 18));1112executeAction(camelcaseAction, editor);1113assert.strictEqual(model.getLineContent(1), 'camelFromWords');11141115editor.setSelection(new Selection(2, 1, 2, 15));1116executeAction(camelcaseAction, editor);1117assert.strictEqual(model.getLineContent(2), 'fromSnakeCase');11181119editor.setSelection(new Selection(3, 1, 3, 15));1120executeAction(camelcaseAction, editor);1121assert.strictEqual(model.getLineContent(3), 'fromKebabCase');11221123editor.setSelection(new Selection(4, 1, 4, 12));1124executeAction(camelcaseAction, editor);1125assert.strictEqual(model.getLineContent(4), 'alreadyCamel');11261127editor.setSelection(new Selection(5, 1, 5, 26));1128executeAction(camelcaseAction, editor);1129assert.strictEqual(model.getLineContent(5), 'reTainSomeCAPitalization');11301131editor.setSelection(new Selection(6, 1, 6, 23));1132executeAction(camelcaseAction, editor);1133assert.strictEqual(model.getLineContent(6), 'myVar.testFunction()');11341135editor.setSelection(new Selection(7, 1, 7, 14));1136executeAction(camelcaseAction, editor);1137assert.strictEqual(model.getLineContent(7), 'öçşÖçŞğüĞü');11381139editor.setSelection(new Selection(8, 1, 8, 14));1140executeAction(camelcaseAction, editor);1141assert.strictEqual(model.getLineContent(8), 'XMLHttpRequest');11421143editor.setSelection(new Selection(9, 1, 11, 2));1144executeAction(camelcaseAction, editor);1145assert.strictEqual(model.getValueInRange(new Selection(9, 1, 11, 3)), '\tfunction helloWorld() {\n\t\treturn someGlobalObject;\n\t}');1146}1147);11481149withTestCodeEditor(1150[1151'',1152' '1153], {}, (editor) => {1154const model = editor.getModel()!;1155const uppercaseAction = new UpperCaseAction();1156const lowercaseAction = new LowerCaseAction();11571158editor.setSelection(new Selection(1, 1, 1, 1));1159executeAction(uppercaseAction, editor);1160assert.strictEqual(model.getLineContent(1), '');1161assertSelection(editor, new Selection(1, 1, 1, 1));11621163editor.setSelection(new Selection(1, 1, 1, 1));1164executeAction(lowercaseAction, editor);1165assert.strictEqual(model.getLineContent(1), '');1166assertSelection(editor, new Selection(1, 1, 1, 1));11671168editor.setSelection(new Selection(2, 2, 2, 2));1169executeAction(uppercaseAction, editor);1170assert.strictEqual(model.getLineContent(2), ' ');1171assertSelection(editor, new Selection(2, 2, 2, 2));11721173editor.setSelection(new Selection(2, 2, 2, 2));1174executeAction(lowercaseAction, editor);1175assert.strictEqual(model.getLineContent(2), ' ');1176assertSelection(editor, new Selection(2, 2, 2, 2));1177}1178);11791180withTestCodeEditor(1181[1182'hello world',1183'öçşğü',1184'parseHTMLString',1185'getElementById',1186'PascalCase',1187'öçşÖÇŞğüĞÜ',1188'audioConverter.convertM4AToMP3();',1189'Capital_Snake_Case',1190'parseHTML4String',1191'_accessor: ServicesAccessor',1192'Kebab-Case',1193], {}, (editor) => {1194const model = editor.getModel()!;1195const kebabCaseAction = new KebabCaseAction();11961197editor.setSelection(new Selection(1, 1, 1, 12));1198executeAction(kebabCaseAction, editor);1199assert.strictEqual(model.getLineContent(1), 'hello world');1200assertSelection(editor, new Selection(1, 1, 1, 12));12011202editor.setSelection(new Selection(2, 1, 2, 6));1203executeAction(kebabCaseAction, editor);1204assert.strictEqual(model.getLineContent(2), 'öçşğü');1205assertSelection(editor, new Selection(2, 1, 2, 6));12061207editor.setSelection(new Selection(3, 1, 3, 16));1208executeAction(kebabCaseAction, editor);1209assert.strictEqual(model.getLineContent(3), 'parse-html-string');1210assertSelection(editor, new Selection(3, 1, 3, 18));12111212editor.setSelection(new Selection(4, 1, 4, 15));1213executeAction(kebabCaseAction, editor);1214assert.strictEqual(model.getLineContent(4), 'get-element-by-id');1215assertSelection(editor, new Selection(4, 1, 4, 18));12161217editor.setSelection(new Selection(5, 1, 5, 11));1218executeAction(kebabCaseAction, editor);1219assert.strictEqual(model.getLineContent(5), 'pascal-case');1220assertSelection(editor, new Selection(5, 1, 5, 12));12211222editor.setSelection(new Selection(6, 1, 6, 11));1223executeAction(kebabCaseAction, editor);1224assert.strictEqual(model.getLineContent(6), 'öçş-öç-şğü-ğü');1225assertSelection(editor, new Selection(6, 1, 6, 14));12261227editor.setSelection(new Selection(7, 1, 7, 34));1228executeAction(kebabCaseAction, editor);1229assert.strictEqual(model.getLineContent(7), 'audio-converter.convert-m4a-to-mp3();');1230assertSelection(editor, new Selection(7, 1, 7, 38));12311232editor.setSelection(new Selection(8, 1, 8, 19));1233executeAction(kebabCaseAction, editor);1234assert.strictEqual(model.getLineContent(8), 'capital-snake-case');1235assertSelection(editor, new Selection(8, 1, 8, 19));12361237editor.setSelection(new Selection(9, 1, 9, 17));1238executeAction(kebabCaseAction, editor);1239assert.strictEqual(model.getLineContent(9), 'parse-html4-string');1240assertSelection(editor, new Selection(9, 1, 9, 19));12411242editor.setSelection(new Selection(10, 1, 10, 28));1243executeAction(kebabCaseAction, editor);1244assert.strictEqual(model.getLineContent(10), '_accessor: services-accessor');1245assertSelection(editor, new Selection(10, 1, 10, 29));12461247editor.setSelection(new Selection(11, 1, 11, 11));1248executeAction(kebabCaseAction, editor);1249assert.strictEqual(model.getLineContent(11), 'kebab-case');1250assertSelection(editor, new Selection(11, 1, 11, 11));1251}1252);12531254withTestCodeEditor(1255[1256'hello world',1257'öçşğü',1258'parseHTMLString',1259'getElementById',1260'PascalCase',1261'öçşÖÇŞğüĞÜ',1262'audioConverter.convertM4AToMP3();',1263'Capital_Snake_Case',1264'parseHTML4String',1265'Kebab-Case',1266'FOO_BAR',1267'FOO BAR A',1268'xML_HTTP-reQUEsT',1269'ÉCOLE',1270'ΩMEGA_CASE',1271'ДОМ_ТЕСТ',1272], {}, (editor) => {1273const model = editor.getModel()!;1274const pascalCaseAction = new PascalCaseAction();12751276editor.setSelection(new Selection(1, 1, 1, 12));1277executeAction(pascalCaseAction, editor);1278assert.strictEqual(model.getLineContent(1), 'HelloWorld');1279assertSelection(editor, new Selection(1, 1, 1, 11));12801281editor.setSelection(new Selection(2, 1, 2, 6));1282executeAction(pascalCaseAction, editor);1283assert.strictEqual(model.getLineContent(2), 'Öçşğü');1284assertSelection(editor, new Selection(2, 1, 2, 6));12851286editor.setSelection(new Selection(3, 1, 3, 16));1287executeAction(pascalCaseAction, editor);1288assert.strictEqual(model.getLineContent(3), 'ParseHTMLString');1289assertSelection(editor, new Selection(3, 1, 3, 16));12901291editor.setSelection(new Selection(4, 1, 4, 15));1292executeAction(pascalCaseAction, editor);1293assert.strictEqual(model.getLineContent(4), 'GetElementById');1294assertSelection(editor, new Selection(4, 1, 4, 15));12951296editor.setSelection(new Selection(5, 1, 5, 11));1297executeAction(pascalCaseAction, editor);1298assert.strictEqual(model.getLineContent(5), 'PascalCase');1299assertSelection(editor, new Selection(5, 1, 5, 11));13001301editor.setSelection(new Selection(6, 1, 6, 11));1302executeAction(pascalCaseAction, editor);1303assert.strictEqual(model.getLineContent(6), 'ÖçşÖÇŞğüĞÜ');1304assertSelection(editor, new Selection(6, 1, 6, 11));13051306editor.setSelection(new Selection(7, 1, 7, 34));1307executeAction(pascalCaseAction, editor);1308assert.strictEqual(model.getLineContent(7), 'AudioConverter.ConvertM4AToMP3();');1309assertSelection(editor, new Selection(7, 1, 7, 34));13101311editor.setSelection(new Selection(8, 1, 8, 19));1312executeAction(pascalCaseAction, editor);1313assert.strictEqual(model.getLineContent(8), 'CapitalSnakeCase');1314assertSelection(editor, new Selection(8, 1, 8, 17));13151316editor.setSelection(new Selection(9, 1, 9, 17));1317executeAction(pascalCaseAction, editor);1318assert.strictEqual(model.getLineContent(9), 'ParseHTML4String');1319assertSelection(editor, new Selection(9, 1, 9, 17));13201321editor.setSelection(new Selection(10, 1, 10, 11));1322executeAction(pascalCaseAction, editor);1323assert.strictEqual(model.getLineContent(10), 'KebabCase');1324assertSelection(editor, new Selection(10, 1, 10, 10));13251326editor.setSelection(new Selection(9, 1, 10, 11));1327executeAction(pascalCaseAction, editor);1328assert.strictEqual(model.getValueInRange(new Selection(9, 1, 10, 11)), 'ParseHTML4String\nKebabCase');1329assertSelection(editor, new Selection(9, 1, 10, 10));13301331editor.setSelection(new Selection(11, 1, 11, 8));1332executeAction(pascalCaseAction, editor);1333assert.strictEqual(model.getLineContent(11), 'FooBar');1334assertSelection(editor, new Selection(11, 1, 11, 7));13351336editor.setSelection(new Selection(12, 1, 12, 10));1337executeAction(pascalCaseAction, editor);1338assert.strictEqual(model.getLineContent(12), 'FooBarA');1339assertSelection(editor, new Selection(12, 1, 12, 8));13401341editor.setSelection(new Selection(13, 1, 13, 17));1342executeAction(pascalCaseAction, editor);1343assert.strictEqual(model.getLineContent(13), 'XmlHttpReQUEsT');1344assertSelection(editor, new Selection(13, 1, 13, 15));13451346editor.setSelection(new Selection(14, 1, 14, 6));1347executeAction(pascalCaseAction, editor);1348assert.strictEqual(model.getLineContent(14), 'École');1349assertSelection(editor, new Selection(14, 1, 14, 6));13501351editor.setSelection(new Selection(15, 1, 15, 11));1352executeAction(pascalCaseAction, editor);1353assert.strictEqual(model.getLineContent(15), 'ΩmegaCase');1354assertSelection(editor, new Selection(15, 1, 15, 10));13551356editor.setSelection(new Selection(16, 1, 16, 9));1357executeAction(pascalCaseAction, editor);1358assert.strictEqual(model.getLineContent(16), 'ДомТест');1359assertSelection(editor, new Selection(16, 1, 16, 8));13601361}1362);1363});13641365suite('DeleteAllRightAction', () => {1366test('should be noop on empty', () => {1367withTestCodeEditor([''], {}, (editor) => {1368const model = editor.getModel()!;1369const action = new DeleteAllRightAction();13701371executeAction(action, editor);1372assert.deepStrictEqual(model.getLinesContent(), ['']);1373assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 1, 1, 1)]);13741375editor.setSelection(new Selection(1, 1, 1, 1));1376executeAction(action, editor);1377assert.deepStrictEqual(model.getLinesContent(), ['']);1378assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 1, 1, 1)]);13791380editor.setSelections([new Selection(1, 1, 1, 1), new Selection(1, 1, 1, 1), new Selection(1, 1, 1, 1)]);1381executeAction(action, editor);1382assert.deepStrictEqual(model.getLinesContent(), ['']);1383assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 1, 1, 1)]);1384});1385});13861387test('should delete selected range', () => {1388withTestCodeEditor([1389'hello',1390'world'1391], {}, (editor) => {1392const model = editor.getModel()!;1393const action = new DeleteAllRightAction();13941395editor.setSelection(new Selection(1, 2, 1, 5));1396executeAction(action, editor);1397assert.deepStrictEqual(model.getLinesContent(), ['ho', 'world']);1398assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 2, 1, 2)]);13991400editor.setSelection(new Selection(1, 1, 2, 4));1401executeAction(action, editor);1402assert.deepStrictEqual(model.getLinesContent(), ['ld']);1403assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 1, 1, 1)]);14041405editor.setSelection(new Selection(1, 1, 1, 3));1406executeAction(action, editor);1407assert.deepStrictEqual(model.getLinesContent(), ['']);1408assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 1, 1, 1)]);1409});1410});14111412test('should delete to the right of the cursor', () => {1413withTestCodeEditor([1414'hello',1415'world'1416], {}, (editor) => {1417const model = editor.getModel()!;1418const action = new DeleteAllRightAction();14191420editor.setSelection(new Selection(1, 3, 1, 3));1421executeAction(action, editor);1422assert.deepStrictEqual(model.getLinesContent(), ['he', 'world']);1423assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 3, 1, 3)]);14241425editor.setSelection(new Selection(2, 1, 2, 1));1426executeAction(action, editor);1427assert.deepStrictEqual(model.getLinesContent(), ['he', '']);1428assert.deepStrictEqual(editor.getSelections(), [new Selection(2, 1, 2, 1)]);1429});1430});14311432test('should join two lines, if at the end of the line', () => {1433withTestCodeEditor([1434'hello',1435'world'1436], {}, (editor) => {1437const model = editor.getModel()!;1438const action = new DeleteAllRightAction();14391440editor.setSelection(new Selection(1, 6, 1, 6));1441executeAction(action, editor);1442assert.deepStrictEqual(model.getLinesContent(), ['helloworld']);1443assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 6, 1, 6)]);14441445editor.setSelection(new Selection(1, 6, 1, 6));1446executeAction(action, editor);1447assert.deepStrictEqual(model.getLinesContent(), ['hello']);1448assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 6, 1, 6)]);14491450editor.setSelection(new Selection(1, 6, 1, 6));1451executeAction(action, editor);1452assert.deepStrictEqual(model.getLinesContent(), ['hello']);1453assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 6, 1, 6)]);1454});1455});14561457test('should work with multiple cursors', () => {1458withTestCodeEditor([1459'hello',1460'there',1461'world'1462], {}, (editor) => {1463const model = editor.getModel()!;1464const action = new DeleteAllRightAction();14651466editor.setSelections([1467new Selection(1, 3, 1, 3),1468new Selection(1, 6, 1, 6),1469new Selection(3, 4, 3, 4),1470]);1471executeAction(action, editor);1472assert.deepStrictEqual(model.getLinesContent(), ['hethere', 'wor']);1473assert.deepStrictEqual(editor.getSelections(), [1474new Selection(1, 3, 1, 3),1475new Selection(2, 4, 2, 4)1476]);14771478executeAction(action, editor);1479assert.deepStrictEqual(model.getLinesContent(), ['he', 'wor']);1480assert.deepStrictEqual(editor.getSelections(), [1481new Selection(1, 3, 1, 3),1482new Selection(2, 4, 2, 4)1483]);14841485executeAction(action, editor);1486assert.deepStrictEqual(model.getLinesContent(), ['hewor']);1487assert.deepStrictEqual(editor.getSelections(), [1488new Selection(1, 3, 1, 3),1489new Selection(1, 6, 1, 6)1490]);14911492executeAction(action, editor);1493assert.deepStrictEqual(model.getLinesContent(), ['he']);1494assert.deepStrictEqual(editor.getSelections(), [1495new Selection(1, 3, 1, 3)1496]);14971498executeAction(action, editor);1499assert.deepStrictEqual(model.getLinesContent(), ['he']);1500assert.deepStrictEqual(editor.getSelections(), [1501new Selection(1, 3, 1, 3)1502]);1503});1504});15051506test('should work with undo/redo', () => {1507withTestCodeEditor([1508'hello',1509'there',1510'world'1511], {}, (editor) => {1512const model = editor.getModel()!;1513const action = new DeleteAllRightAction();15141515editor.setSelections([1516new Selection(1, 3, 1, 3),1517new Selection(1, 6, 1, 6),1518new Selection(3, 4, 3, 4),1519]);1520executeAction(action, editor);1521assert.deepStrictEqual(model.getLinesContent(), ['hethere', 'wor']);1522assert.deepStrictEqual(editor.getSelections(), [1523new Selection(1, 3, 1, 3),1524new Selection(2, 4, 2, 4)1525]);15261527editor.runCommand(CoreEditingCommands.Undo, null);1528assert.deepStrictEqual(editor.getSelections(), [1529new Selection(1, 3, 1, 3),1530new Selection(1, 6, 1, 6),1531new Selection(3, 4, 3, 4)1532]);1533editor.runCommand(CoreEditingCommands.Redo, null);1534assert.deepStrictEqual(editor.getSelections(), [1535new Selection(1, 3, 1, 3),1536new Selection(2, 4, 2, 4)1537]);1538});1539});1540});15411542test('InsertLineBeforeAction', () => {1543function testInsertLineBefore(lineNumber: number, column: number, callback: (model: ITextModel, viewModel: ViewModel) => void): void {1544const TEXT = [1545'First line',1546'Second line',1547'Third line'1548];1549withTestCodeEditor(TEXT, {}, (editor, viewModel) => {1550editor.setPosition(new Position(lineNumber, column));1551const insertLineBeforeAction = new InsertLineBeforeAction();15521553executeAction(insertLineBeforeAction, editor);1554callback(editor.getModel()!, viewModel);1555});1556}15571558testInsertLineBefore(1, 3, (model, viewModel) => {1559assert.deepStrictEqual(viewModel.getSelection(), new Selection(1, 1, 1, 1));1560assert.strictEqual(model.getLineContent(1), '');1561assert.strictEqual(model.getLineContent(2), 'First line');1562assert.strictEqual(model.getLineContent(3), 'Second line');1563assert.strictEqual(model.getLineContent(4), 'Third line');1564});15651566testInsertLineBefore(2, 3, (model, viewModel) => {1567assert.deepStrictEqual(viewModel.getSelection(), new Selection(2, 1, 2, 1));1568assert.strictEqual(model.getLineContent(1), 'First line');1569assert.strictEqual(model.getLineContent(2), '');1570assert.strictEqual(model.getLineContent(3), 'Second line');1571assert.strictEqual(model.getLineContent(4), 'Third line');1572});15731574testInsertLineBefore(3, 3, (model, viewModel) => {1575assert.deepStrictEqual(viewModel.getSelection(), new Selection(3, 1, 3, 1));1576assert.strictEqual(model.getLineContent(1), 'First line');1577assert.strictEqual(model.getLineContent(2), 'Second line');1578assert.strictEqual(model.getLineContent(3), '');1579assert.strictEqual(model.getLineContent(4), 'Third line');1580});1581});15821583test('InsertLineAfterAction', () => {1584function testInsertLineAfter(lineNumber: number, column: number, callback: (model: ITextModel, viewModel: ViewModel) => void): void {1585const TEXT = [1586'First line',1587'Second line',1588'Third line'1589];1590withTestCodeEditor(TEXT, {}, (editor, viewModel) => {1591editor.setPosition(new Position(lineNumber, column));1592const insertLineAfterAction = new InsertLineAfterAction();15931594executeAction(insertLineAfterAction, editor);1595callback(editor.getModel()!, viewModel);1596});1597}15981599testInsertLineAfter(1, 3, (model, viewModel) => {1600assert.deepStrictEqual(viewModel.getSelection(), new Selection(2, 1, 2, 1));1601assert.strictEqual(model.getLineContent(1), 'First line');1602assert.strictEqual(model.getLineContent(2), '');1603assert.strictEqual(model.getLineContent(3), 'Second line');1604assert.strictEqual(model.getLineContent(4), 'Third line');1605});16061607testInsertLineAfter(2, 3, (model, viewModel) => {1608assert.deepStrictEqual(viewModel.getSelection(), new Selection(3, 1, 3, 1));1609assert.strictEqual(model.getLineContent(1), 'First line');1610assert.strictEqual(model.getLineContent(2), 'Second line');1611assert.strictEqual(model.getLineContent(3), '');1612assert.strictEqual(model.getLineContent(4), 'Third line');1613});16141615testInsertLineAfter(3, 3, (model, viewModel) => {1616assert.deepStrictEqual(viewModel.getSelection(), new Selection(4, 1, 4, 1));1617assert.strictEqual(model.getLineContent(1), 'First line');1618assert.strictEqual(model.getLineContent(2), 'Second line');1619assert.strictEqual(model.getLineContent(3), 'Third line');1620assert.strictEqual(model.getLineContent(4), '');1621});1622});16231624test('Bug 18276:[editor] Indentation broken when selection is empty', () => {16251626const model = createTextModel(1627[1628'function baz() {'1629].join('\n'),1630undefined,1631{1632insertSpaces: false,1633}1634);16351636withTestCodeEditor(model, {}, (editor) => {1637const indentLinesAction = new IndentLinesAction();1638editor.setPosition(new Position(1, 2));16391640executeAction(indentLinesAction, editor);1641assert.strictEqual(model.getLineContent(1), '\tfunction baz() {');1642assert.deepStrictEqual(editor.getSelection(), new Selection(1, 3, 1, 3));16431644editor.runCommand(CoreEditingCommands.Tab, null);1645assert.strictEqual(model.getLineContent(1), '\tf\tunction baz() {');1646});16471648model.dispose();1649});16501651test('issue #80736: Indenting while the cursor is at the start of a line of text causes the added spaces or tab to be selected', () => {1652const model = createTextModel(1653[1654'Some text'1655].join('\n'),1656undefined,1657{1658insertSpaces: false,1659}1660);16611662withTestCodeEditor(model, {}, (editor) => {1663const indentLinesAction = new IndentLinesAction();1664editor.setPosition(new Position(1, 1));16651666executeAction(indentLinesAction, editor);1667assert.strictEqual(model.getLineContent(1), '\tSome text');1668assert.deepStrictEqual(editor.getSelection(), new Selection(1, 2, 1, 2));1669});16701671model.dispose();1672});16731674test('Indenting on empty line should move cursor', () => {1675const model = createTextModel(1676[1677''1678].join('\n')1679);16801681withTestCodeEditor(model, { useTabStops: false }, (editor) => {1682const indentLinesAction = new IndentLinesAction();1683editor.setPosition(new Position(1, 1));16841685executeAction(indentLinesAction, editor);1686assert.strictEqual(model.getLineContent(1), ' ');1687assert.deepStrictEqual(editor.getSelection(), new Selection(1, 5, 1, 5));1688});16891690model.dispose();1691});16921693test('issue #62112: Delete line does not work properly when multiple cursors are on line', () => {1694const TEXT = [1695'a',1696'foo boo',1697'too',1698'c',1699];1700withTestCodeEditor(TEXT, {}, (editor) => {1701editor.setSelections([1702new Selection(2, 4, 2, 4),1703new Selection(2, 8, 2, 8),1704new Selection(3, 4, 3, 4),1705]);1706const deleteLinesAction = new DeleteLinesAction();1707executeAction(deleteLinesAction, editor);17081709assert.strictEqual(editor.getValue(), 'a\nc');1710});1711});17121713function testDeleteLinesCommand(initialText: string[], _initialSelections: Selection | Selection[], resultingText: string[], _resultingSelections: Selection | Selection[]): void {1714const initialSelections = Array.isArray(_initialSelections) ? _initialSelections : [_initialSelections];1715const resultingSelections = Array.isArray(_resultingSelections) ? _resultingSelections : [_resultingSelections];1716withTestCodeEditor(initialText, {}, (editor) => {1717editor.setSelections(initialSelections);1718const deleteLinesAction = new DeleteLinesAction();1719executeAction(deleteLinesAction, editor);17201721assert.strictEqual(editor.getValue(), resultingText.join('\n'));1722assert.deepStrictEqual(editor.getSelections(), resultingSelections);1723});1724}17251726test('empty selection in middle of lines', function () {1727testDeleteLinesCommand(1728[1729'first',1730'second line',1731'third line',1732'fourth line',1733'fifth'1734],1735new Selection(2, 3, 2, 3),1736[1737'first',1738'third line',1739'fourth line',1740'fifth'1741],1742new Selection(2, 3, 2, 3)1743);1744});17451746test('empty selection at top of lines', function () {1747testDeleteLinesCommand(1748[1749'first',1750'second line',1751'third line',1752'fourth line',1753'fifth'1754],1755new Selection(1, 5, 1, 5),1756[1757'second line',1758'third line',1759'fourth line',1760'fifth'1761],1762new Selection(1, 5, 1, 5)1763);1764});17651766test('empty selection at end of lines', function () {1767testDeleteLinesCommand(1768[1769'first',1770'second line',1771'third line',1772'fourth line',1773'fifth'1774],1775new Selection(5, 2, 5, 2),1776[1777'first',1778'second line',1779'third line',1780'fourth line'1781],1782new Selection(4, 2, 4, 2)1783);1784});17851786test('with selection in middle of lines', function () {1787testDeleteLinesCommand(1788[1789'first',1790'second line',1791'third line',1792'fourth line',1793'fifth'1794],1795new Selection(3, 3, 2, 2),1796[1797'first',1798'fourth line',1799'fifth'1800],1801new Selection(2, 2, 2, 2)1802);1803});18041805test('with selection at top of lines', function () {1806testDeleteLinesCommand(1807[1808'first',1809'second line',1810'third line',1811'fourth line',1812'fifth'1813],1814new Selection(1, 4, 1, 5),1815[1816'second line',1817'third line',1818'fourth line',1819'fifth'1820],1821new Selection(1, 5, 1, 5)1822);1823});18241825test('with selection at end of lines', function () {1826testDeleteLinesCommand(1827[1828'first',1829'second line',1830'third line',1831'fourth line',1832'fifth'1833],1834new Selection(5, 1, 5, 2),1835[1836'first',1837'second line',1838'third line',1839'fourth line'1840],1841new Selection(4, 2, 4, 2)1842);1843});18441845test('with full line selection in middle of lines', function () {1846testDeleteLinesCommand(1847[1848'first',1849'second line',1850'third line',1851'fourth line',1852'fifth'1853],1854new Selection(4, 1, 2, 1),1855[1856'first',1857'fourth line',1858'fifth'1859],1860new Selection(2, 1, 2, 1)1861);1862});18631864test('with full line selection at top of lines', function () {1865testDeleteLinesCommand(1866[1867'first',1868'second line',1869'third line',1870'fourth line',1871'fifth'1872],1873new Selection(2, 1, 1, 5),1874[1875'second line',1876'third line',1877'fourth line',1878'fifth'1879],1880new Selection(1, 5, 1, 5)1881);1882});18831884test('with full line selection at end of lines', function () {1885testDeleteLinesCommand(1886[1887'first',1888'second line',1889'third line',1890'fourth line',1891'fifth'1892],1893new Selection(4, 1, 5, 2),1894[1895'first',1896'second line',1897'third line'1898],1899new Selection(3, 2, 3, 2)1900);1901});19021903test('multicursor 1', function () {1904testDeleteLinesCommand(1905[1906'class P {',1907'',1908' getA() {',1909' if (true) {',1910' return "a";',1911' }',1912' }',1913'',1914' getB() {',1915' if (true) {',1916' return "b";',1917' }',1918' }',1919'',1920' getC() {',1921' if (true) {',1922' return "c";',1923' }',1924' }',1925'}',1926],1927[1928new Selection(4, 1, 5, 1),1929new Selection(10, 1, 11, 1),1930new Selection(16, 1, 17, 1),1931],1932[1933'class P {',1934'',1935' getA() {',1936' return "a";',1937' }',1938' }',1939'',1940' getB() {',1941' return "b";',1942' }',1943' }',1944'',1945' getC() {',1946' return "c";',1947' }',1948' }',1949'}',1950],1951[1952new Selection(4, 1, 4, 1),1953new Selection(9, 1, 9, 1),1954new Selection(14, 1, 14, 1),1955]1956);1957});1958});195919601961