diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java index f4bca9e90dc5..be65138e5d35 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -192,8 +192,10 @@ public class SurroundWithHandler implements CodeInsightActionHandler { PsiDocumentManager.getInstance(project).commitAllDocuments(); int col = editor.getCaretModel().getLogicalPosition().column; int line = editor.getCaretModel().getLogicalPosition().line; - LogicalPosition pos = new LogicalPosition(0, 0); - editor.getCaretModel().moveToLogicalPosition(pos); + if (!editor.getCaretModel().supportsMultipleCarets()) { + LogicalPosition pos = new LogicalPosition(0, 0); + editor.getCaretModel().moveToLogicalPosition(pos); + } TextRange range = surrounder.surroundElements(project, editor, elements); if (TemplateManager.getInstance(project).getActiveTemplate(editor) == null) { LogicalPosition pos1 = new LogicalPosition(line, col); @@ -201,6 +203,7 @@ public class SurroundWithHandler implements CodeInsightActionHandler { } if (range != null) { int offset = range.getStartOffset(); + editor.getCaretModel().removeSecondaryCarets(); editor.getCaretModel().moveToOffset(offset); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset()); diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/MultiCaretBlockSelectionTest.groovy b/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/MultiCaretBlockSelectionTest.groovy new file mode 100644 index 000000000000..648bf43dcd33 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/MultiCaretBlockSelectionTest.groovy @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.editor.actions + +import com.intellij.testFramework.EditorTestUtil + +class MultiCaretBlockSelectionTest extends BlockSelectionTest { + @Override + protected void setUp() throws Exception { + super.setUp() + EditorTestUtil.enableMultipleCarets() + } + + @Override + protected void tearDown() throws Exception { + EditorTestUtil.disableMultipleCarets() + super.tearDown() + } +} diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorTest.java index be8801949905..fecb6561d11e 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -204,4 +204,15 @@ public abstract class AbstractEditorTest extends LightPlatformCodeInsightTestCas mapper.rawAdd(visualLine, startOffset, endOffset, startLogicalLine, startLogicalColumn, endLogicalLine, endLogicalColumn, endVisualColumn, foldRegions, tabData); } } + + public void assertSelectionRanges(int[][] ranges) { + int[] selectionStarts = myEditor.getSelectionModel().getBlockSelectionStarts(); + int[] selectionEnds = myEditor.getSelectionModel().getBlockSelectionEnds(); + int actualRangeCount = selectionStarts.length; + int[][] actualRanges = new int[actualRangeCount][]; + for (int i = 0; i < actualRangeCount; i++) { + actualRanges[i] = new int[] {selectionStarts[i], selectionEnds[i]}; + } + assertEquals("Wrong selected ranges", Arrays.deepToString(ranges), Arrays.deepToString(actualRanges)); + } } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/IterationStateTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/IterationStateTest.java index d9d925ea835b..2b8f99e2693a 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/IterationStateTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/IterationStateTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package com.intellij.openapi.editor.impl; import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.fileTypes.PlainTextFileType; +import com.intellij.testFramework.EditorTestUtil; import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; import org.junit.Assert; @@ -38,6 +39,16 @@ public class IterationStateTest extends LightPlatformCodeInsightFixtureTestCase new Segment(10, 11, Color.BLACK)); } + public void testMultiCaretBlockSelection() { + EditorTestUtil.enableMultipleCarets(); + try { + testBlockSelection(); + } + finally { + EditorTestUtil.disableMultipleCarets(); + } + } + private void verifySplitting(String text, Segment... expectedSegments) { myFixture.configureByText(PlainTextFileType.INSTANCE, text); EditorEx editor = (EditorEx)myFixture.getEditor(); diff --git a/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java index 2400ed5ee355..d4772f77a195 100644 --- a/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator; import com.intellij.openapi.editor.impl.DefaultEditorTextRepresentationHelper; import com.intellij.openapi.editor.impl.SoftWrapModelImpl; import com.intellij.openapi.editor.impl.softwrap.mapping.SoftWrapApplianceManager; +import com.intellij.openapi.util.registry.Registry; import com.intellij.psi.tree.IElementType; import junit.framework.Assert; import org.jetbrains.annotations.NotNull; @@ -173,4 +174,12 @@ public class EditorTestUtil { applianceManager.registerSoftWrapIfNecessary(); return !model.getRegisteredSoftWraps().isEmpty(); } + + public static void enableMultipleCarets() { + Registry.get("editor.allow.multiple.carets").setValue(true); + } + + public static void disableMultipleCarets() { + Registry.get("editor.allow.multiple.carets").setValue(false); + } } diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java index a2df4d7481b9..81468e753eab 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -624,7 +624,7 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest public void run() { EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionHandler actionHandler = actionManager.getActionHandler(actionId); - actionHandler.execute(getEditor(), DataManager.getInstance().getDataContext()); + actionHandler.executeForAllCarets(getEditor(), DataManager.getInstance().getDataContext()); } }, "", null); }