Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/linesOperations/test/browser/copyLinesCommand.test.ts
5257 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
8
import { Selection } from '../../../../common/core/selection.js';
9
import { CopyLinesCommand } from '../../browser/copyLinesCommand.js';
10
import { DuplicateSelectionAction } from '../../browser/linesOperations.js';
11
import { withTestCodeEditor } from '../../../../test/browser/testCodeEditor.js';
12
import { testCommand } from '../../../../test/browser/testCommand.js';
13
14
function testCopyLinesDownCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
15
testCommand(lines, null, selection, (accessor, sel) => new CopyLinesCommand(sel, true), expectedLines, expectedSelection);
16
}
17
18
function testCopyLinesUpCommand(lines: string[], selection: Selection, expectedLines: string[], expectedSelection: Selection): void {
19
testCommand(lines, null, selection, (accessor, sel) => new CopyLinesCommand(sel, false), expectedLines, expectedSelection);
20
}
21
22
suite('Editor Contrib - Copy Lines Command', () => {
23
24
ensureNoDisposablesAreLeakedInTestSuite();
25
26
test('copy first line down', function () {
27
testCopyLinesDownCommand(
28
[
29
'first',
30
'second line',
31
'third line',
32
'fourth line',
33
'fifth'
34
],
35
new Selection(1, 3, 1, 1),
36
[
37
'first',
38
'first',
39
'second line',
40
'third line',
41
'fourth line',
42
'fifth'
43
],
44
new Selection(2, 3, 2, 1)
45
);
46
});
47
48
test('copy first line up', function () {
49
testCopyLinesUpCommand(
50
[
51
'first',
52
'second line',
53
'third line',
54
'fourth line',
55
'fifth'
56
],
57
new Selection(1, 3, 1, 1),
58
[
59
'first',
60
'first',
61
'second line',
62
'third line',
63
'fourth line',
64
'fifth'
65
],
66
new Selection(1, 3, 1, 1)
67
);
68
});
69
70
test('copy last line down', function () {
71
testCopyLinesDownCommand(
72
[
73
'first',
74
'second line',
75
'third line',
76
'fourth line',
77
'fifth'
78
],
79
new Selection(5, 3, 5, 1),
80
[
81
'first',
82
'second line',
83
'third line',
84
'fourth line',
85
'fifth',
86
'fifth'
87
],
88
new Selection(6, 3, 6, 1)
89
);
90
});
91
92
test('copy last line up', function () {
93
testCopyLinesUpCommand(
94
[
95
'first',
96
'second line',
97
'third line',
98
'fourth line',
99
'fifth'
100
],
101
new Selection(5, 3, 5, 1),
102
[
103
'first',
104
'second line',
105
'third line',
106
'fourth line',
107
'fifth',
108
'fifth'
109
],
110
new Selection(5, 3, 5, 1)
111
);
112
});
113
114
test('issue #1322: copy line up', function () {
115
testCopyLinesUpCommand(
116
[
117
'first',
118
'second line',
119
'third line',
120
'fourth line',
121
'fifth'
122
],
123
new Selection(3, 11, 3, 11),
124
[
125
'first',
126
'second line',
127
'third line',
128
'third line',
129
'fourth line',
130
'fifth'
131
],
132
new Selection(3, 11, 3, 11)
133
);
134
});
135
136
test('issue #1322: copy last line up', function () {
137
testCopyLinesUpCommand(
138
[
139
'first',
140
'second line',
141
'third line',
142
'fourth line',
143
'fifth'
144
],
145
new Selection(5, 6, 5, 6),
146
[
147
'first',
148
'second line',
149
'third line',
150
'fourth line',
151
'fifth',
152
'fifth'
153
],
154
new Selection(5, 6, 5, 6)
155
);
156
});
157
158
test('copy many lines up', function () {
159
testCopyLinesUpCommand(
160
[
161
'first',
162
'second line',
163
'third line',
164
'fourth line',
165
'fifth'
166
],
167
new Selection(4, 3, 2, 1),
168
[
169
'first',
170
'second line',
171
'third line',
172
'fourth line',
173
'second line',
174
'third line',
175
'fourth line',
176
'fifth'
177
],
178
new Selection(4, 3, 2, 1)
179
);
180
});
181
182
test('ignore empty selection', function () {
183
testCopyLinesUpCommand(
184
[
185
'first',
186
'second line',
187
'third line',
188
'fourth line',
189
'fifth'
190
],
191
new Selection(2, 1, 1, 1),
192
[
193
'first',
194
'first',
195
'second line',
196
'third line',
197
'fourth line',
198
'fifth'
199
],
200
new Selection(2, 1, 1, 1)
201
);
202
});
203
});
204
205
suite('Editor Contrib - Duplicate Selection', () => {
206
207
ensureNoDisposablesAreLeakedInTestSuite();
208
209
const duplicateSelectionAction = new DuplicateSelectionAction();
210
211
function testDuplicateSelectionAction(lines: string[], selections: Selection[], expectedLines: string[], expectedSelections: Selection[]): void {
212
withTestCodeEditor(lines.join('\n'), {}, (editor) => {
213
editor.setSelections(selections);
214
duplicateSelectionAction.run(null!, editor, {});
215
assert.deepStrictEqual(editor.getValue(), expectedLines.join('\n'));
216
assert.deepStrictEqual(editor.getSelections()!.map(s => s.toString()), expectedSelections.map(s => s.toString()));
217
});
218
}
219
220
test('empty selection', function () {
221
testDuplicateSelectionAction(
222
[
223
'first',
224
'second line',
225
'third line',
226
'fourth line',
227
'fifth'
228
],
229
[new Selection(2, 2, 2, 2), new Selection(3, 2, 3, 2)],
230
[
231
'first',
232
'second line',
233
'second line',
234
'third line',
235
'third line',
236
'fourth line',
237
'fifth'
238
],
239
[new Selection(3, 2, 3, 2), new Selection(5, 2, 5, 2)]
240
);
241
});
242
243
test('with selection', function () {
244
testDuplicateSelectionAction(
245
[
246
'first',
247
'second line',
248
'third line',
249
'fourth line',
250
'fifth'
251
],
252
[new Selection(2, 1, 2, 4), new Selection(3, 1, 3, 4)],
253
[
254
'first',
255
'secsecond line',
256
'thithird line',
257
'fourth line',
258
'fifth'
259
],
260
[new Selection(2, 4, 2, 7), new Selection(3, 4, 3, 7)]
261
);
262
});
263
});
264
265