IDEA-146710 issue with multiple carets in column mode
[idea/community.git] / platform / platform-tests / testSrc / com / intellij / openapi / editor / EditorMultiCaretColumnModeTest.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.openapi.editor;
17
18 import com.intellij.openapi.actionSystem.IdeActions;
19 import com.intellij.openapi.editor.ex.EditorEx;
20 import com.intellij.openapi.editor.impl.AbstractEditorTest;
21
22 import java.io.IOException;
23
24 public class EditorMultiCaretColumnModeTest extends AbstractEditorTest {
25   public void testUpDown() throws Exception {
26     init("line1\n" +
27          "li<caret>ne2\n" +
28          "line3");
29
30     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
31     checkResultByText("line1\n" +
32                       "li<caret>ne2\n" +
33                       "li<caret>ne3");
34
35     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION); // hitting document bottom
36     checkResultByText("line1\n" +
37                       "li<caret>ne2\n" +
38                       "li<caret>ne3");
39
40     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
41     checkResultByText("line1\n" +
42                       "li<caret>ne2\n" +
43                       "line3");
44
45     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION); // hitting document top
46     checkResultByText("li<caret>ne1\n" +
47                       "li<caret>ne2\n" +
48                       "line3");
49
50     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
51     checkResultByText("li<caret>ne1\n" +
52                       "li<caret>ne2\n" +
53                       "line3");
54
55     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
56     checkResultByText("line1\n" +
57                       "li<caret>ne2\n" +
58                       "line3");
59   }
60
61   public void testPageUpDown() throws Exception {
62     init("line1\n" +
63          "line2\n" +
64          "line3\n" +
65          "line4\n" +
66          "line<caret>5\n" +
67          "line6\n" +
68          "line7");
69     setEditorVisibleSize(1000, 3);
70
71     executeAction("EditorPageUpWithSelection");
72     checkResultByText("line1\n" +
73                       "line<caret>2\n" +
74                       "line<caret>3\n" +
75                       "line<caret>4\n" +
76                       "line<caret>5\n" +
77                       "line6\n" +
78                       "line7");
79
80     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
81     checkResultByText("line<caret>1\n" +
82                       "line<caret>2\n" +
83                       "line<caret>3\n" +
84                       "line<caret>4\n" +
85                       "line<caret>5\n" +
86                       "line6\n" +
87                       "line7");
88
89     executeAction("EditorPageDownWithSelection");
90     checkResultByText("line1\n" +
91                       "line2\n" +
92                       "line3\n" +
93                       "line<caret>4\n" +
94                       "line<caret>5\n" +
95                       "line6\n" +
96                       "line7");
97
98     executeAction("EditorPageDownWithSelection");
99     checkResultByText("line1\n" +
100                       "line2\n" +
101                       "line3\n" +
102                       "line4\n" +
103                       "line<caret>5\n" +
104                       "line<caret>6\n" +
105                       "line<caret>7");
106
107     executeAction("EditorPageUpWithSelection");
108     checkResultByText("line1\n" +
109                       "line2\n" +
110                       "line3\n" +
111                       "line<caret>4\n" +
112                       "line<caret>5\n" +
113                       "line6\n" +
114                       "line7");
115   }
116
117   public void testSelectionWithKeyboard() throws Exception {
118     init("line1\n" +
119          "li<caret>ne2\n" +
120          "line3");
121
122     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
123     checkResultByText("line1\n" +
124                       "li<selection>n<caret></selection>e2\n" +
125                       "line3");
126
127     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
128     checkResultByText("line1\n" +
129                       "li<selection>n<caret></selection>e2\n" +
130                       "li<selection>n<caret></selection>e3");
131
132     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT_WITH_SELECTION);
133     checkResultByText("line1\n" +
134                       "li<caret>ne2\n" +
135                       "li<caret>ne3");
136
137     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT_WITH_SELECTION);
138     checkResultByText("line1\n" +
139                       "l<selection><caret>i</selection>ne2\n" +
140                       "l<selection><caret>i</selection>ne3");
141
142     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
143     checkResultByText("line1\n" +
144                       "l<selection><caret>i</selection>ne2\n" +
145                       "line3");
146
147     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
148     checkResultByText("l<selection><caret>i</selection>ne1\n" +
149                       "l<selection><caret>i</selection>ne2\n" +
150                       "line3");
151
152     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
153     checkResultByText("li<caret>ne1\n" +
154                       "li<caret>ne2\n" +
155                       "line3");
156
157     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
158     checkResultByText("li<selection>n<caret></selection>e1\n" +
159                       "li<selection>n<caret></selection>e2\n" +
160                       "line3");
161
162     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
163     checkResultByText("line1\n" +
164                       "li<selection>n<caret></selection>e2\n" +
165                       "line3");
166
167     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT_WITH_SELECTION);
168     checkResultByText("line1\n" +
169                       "li<caret>ne2\n" +
170                       "line3");
171   }
172
173   public void testSelectNextPrevWord() throws Exception {
174     init("aaa aaa<caret>\n" +
175          "bbbb bbbb");
176     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
177     executeAction("EditorPreviousWordWithSelection");
178     checkResultByText("aaa <selection><caret>aaa</selection>\n" +
179                       "bbbb <selection><caret>bb</selection>bb");
180     executeAction("EditorNextWordWithSelection");
181     checkResultByText("aaa aaa<caret>\n" +
182                       "bbbb bb<selection>bb<caret></selection>");
183   }
184
185   public void testMoveToSelectionStart() throws Exception {
186     init("a");
187     mouse().clickAt(0, 2).dragTo(0, 4).release();
188     verifyCaretsAndSelections(0, 4, 2, 4);
189
190     executeAction("EditorLeft");
191     verifyCaretsAndSelections(0, 2, 2, 2);
192   }
193
194   public void testMoveToSelectionEnd() throws Exception {
195     init("a");
196     mouse().clickAt(0, 4).dragTo(0, 2).release();
197     verifyCaretsAndSelections(0, 2, 2, 4);
198
199     executeAction("EditorRight");
200     verifyCaretsAndSelections(0, 4, 4, 4);
201   }
202
203   public void testReverseBlockSelection() throws Exception {
204     init("a");
205     mouse().clickAt(0, 4).dragTo(0, 3).release();
206     verifyCaretsAndSelections(0, 3, 3, 4);
207
208     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
209     verifyCaretsAndSelections(0, 4, 4, 4);
210   }
211
212   public void testSelectionWithKeyboardInEmptySpace() throws Exception {
213     init("\n\n");
214     mouse().clickAt(1, 1);
215     verifyCaretsAndSelections(1, 1, 1, 1);
216
217     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
218     verifyCaretsAndSelections(1, 2, 1, 2);
219
220     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
221     verifyCaretsAndSelections(1, 2, 1, 2,
222                               2, 2, 1, 2);
223
224     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT_WITH_SELECTION);
225     verifyCaretsAndSelections(1, 1, 1, 1,
226                               2, 1, 1, 1);
227
228     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT_WITH_SELECTION);
229     verifyCaretsAndSelections(1, 0, 0, 1,
230                               2, 0, 0, 1);
231
232     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
233     verifyCaretsAndSelections(1, 0, 0, 1);
234
235     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
236     verifyCaretsAndSelections(0, 0, 0, 1,
237                               1, 0, 0, 1);
238
239     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
240     verifyCaretsAndSelections(0, 1, 1, 1,
241                               1, 1, 1, 1);
242
243     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT_WITH_SELECTION);
244     verifyCaretsAndSelections(0, 2, 1, 2,
245                               1, 2, 1, 2);
246
247     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
248     verifyCaretsAndSelections(1, 2, 1, 2);
249
250     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT_WITH_SELECTION);
251     verifyCaretsAndSelections(1, 1, 1, 1);
252   }
253
254   public void testBlockSelection() throws Exception {
255     init("a\n" +
256          "bbb\n" +
257          "ccccc");
258     mouse().clickAt(2, 4).dragTo(0, 1).release();
259     verifyCaretsAndSelections(0, 1, 1, 4,
260                               1, 1, 1, 4,
261                               2, 1, 1, 4);
262   }
263
264   public void testTyping() throws Exception {
265     init("a\n" +
266          "bbb\n" +
267          "ccccc");
268     mouse().clickAt(0, 2).dragTo(2, 3).release();
269     type('S');
270     checkResultByText("a S<caret>\n" +
271                       "bbS<caret>\n" +
272                       "ccS<caret>cc");
273   }
274
275   public void testCopyPasteOfShortLines() throws Exception {
276     init("a\n" +
277          "bbb\n" +
278          "ccccc");
279     mouse().clickAt(0, 2).dragTo(2, 4).release();
280     executeAction("EditorCopy");
281     executeAction("EditorLineStart");
282     executeAction("EditorPaste");
283     checkResultByText("  <caret>a\n" +
284                       "b <caret>bbb\n" +
285                       "cc<caret>ccccc");
286   }
287
288   public void testPasteOfBlockToASingleCaret() throws Exception {
289     init("a\n" +
290          "bbb\n" +
291          "ccccc");
292     mouse().clickAt(1, 2).dragTo(2, 4).release();
293     executeAction("EditorCopy");
294     mouse().clickAt(0, 2);
295     executeAction("EditorPaste");
296     checkResultByText("a b <caret>\n" +
297                       "bbcc<caret>b\n" +
298                       "ccccc");
299   }
300
301   public void testSelectToDocumentStart() throws Exception {
302     init("line1\n" +
303          "line2\n" +
304          "line3\n" +
305          "line4");
306     mouse().clickAt(1, 1).dragTo(2, 2).release();
307     executeAction("EditorTextStartWithSelection");
308     checkResultByText("<selection><caret>l</selection>ine1\n" +
309                       "<selection><caret>l</selection>ine2\n" +
310                       "line3\n" +
311                       "line4");
312   }
313
314   public void testSelectToDocumentEnd() throws Exception {
315     init("line1\n" +
316          "line2\n" +
317          "line3\n" +
318          "line4");
319     mouse().clickAt(1, 1).dragTo(2, 2).release();
320     executeAction("EditorTextEndWithSelection");
321     checkResultByText("line1\n" +
322                       "l<selection>ine2<caret></selection>\n" +
323                       "l<selection>ine3<caret></selection>\n" +
324                       "l<selection>ine4<caret></selection>");
325   }
326
327   public void testToggleCaseToLower() throws Exception {
328     init("a\n" +
329          "BBB\n" +
330          "ccccc");
331     mouse().clickAt(1, 2).dragTo(2, 4).release();
332     executeAction("EditorToggleCase");
333     checkResultByText("a\n" +
334                       "BBb\n" +
335                       "ccccc");
336     verifyCaretsAndSelections(1, 4, 2, 4,
337                               2, 4, 2, 4);
338   }
339
340   public void testToggleCaseToUpper() throws Exception {
341     init("a\n" +
342          "BBb\n" +
343          "ccccc");
344     mouse().clickAt(1, 2).dragTo(2, 4).release();
345     executeAction("EditorToggleCase");
346     checkResultByText("a\n" +
347                       "BBB\n" +
348                       "ccCCc");
349     verifyCaretsAndSelections(1, 4, 2, 4,
350                               2, 4, 2, 4);
351   }
352   
353   public void testSeparatedCarets() throws Exception {
354     init("\n" +
355          "\n" +
356          "<caret>\n" +
357          "\n" +
358          "<caret>\n" +
359          "\n" +
360          "");
361     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
362     checkResultByText("\n" +
363                       "\n" +
364                       "<caret>\n" +
365                       "<caret>\n" +
366                       "<caret>\n" +
367                       "<caret>\n" +
368                       "");
369     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
370     checkResultByText("\n" +
371                       "\n" +
372                       "<caret>\n" +
373                       "<caret>\n" +
374                       "<caret>\n" +
375                       "<caret>\n" +
376                       "<caret>");
377     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
378     checkResultByText("\n" +
379                       "\n" +
380                       "<caret>\n" +
381                       "<caret>\n" +
382                       "<caret>\n" +
383                       "<caret>\n" +
384                       "");
385     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
386     checkResultByText("\n" +
387                       "\n" +
388                       "<caret>\n" +
389                       "\n" +
390                       "<caret>\n" +
391                       "\n" +
392                       "");
393     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
394     checkResultByText("\n" +
395                       "<caret>\n" +
396                       "<caret>\n" +
397                       "<caret>\n" +
398                       "<caret>\n" +
399                       "\n" +
400                       "");
401     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP_WITH_SELECTION);
402     checkResultByText("<caret>\n" +
403                       "<caret>\n" +
404                       "<caret>\n" +
405                       "<caret>\n" +
406                       "<caret>\n" +
407                       "\n" +
408                       "");
409     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
410     checkResultByText("\n" +
411                       "<caret>\n" +
412                       "<caret>\n" +
413                       "<caret>\n" +
414                       "<caret>\n" +
415                       "\n" +
416                       "");
417     executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN_WITH_SELECTION);
418     checkResultByText("\n" +
419                       "\n" +
420                       "<caret>\n" +
421                       "\n" +
422                       "<caret>\n" +
423                       "\n" +
424                       "");
425   }
426
427   private void init(String text) throws IOException {
428     configureFromFileText(getTestName(false) + ".txt", text);
429     setEditorVisibleSize(1000, 1000);
430     ((EditorEx)myEditor).setColumnMode(true);
431   }
432 }