diff --git a/java/java-tests/testData/codeInsight/template/editor/BeforeTestData.java b/java/java-tests/testData/codeInsight/template/editor/BeforeTestData.java new file mode 100644 index 000000000000..e6bc24cc608f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/BeforeTestData.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/EndActionOutsideVariableSegment_after.java b/java/java-tests/testData/codeInsight/template/editor/EndActionOutsideVariableSegment_after.java new file mode 100644 index 000000000000..490067b5ce1f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/EndActionOutsideVariableSegment_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/EndActionWithExistingSelection_after.java b/java/java-tests/testData/codeInsight/template/editor/EndActionWithExistingSelection_after.java new file mode 100644 index 000000000000..743964587be8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/EndActionWithExistingSelection_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelectionOutsideVariableSegment_after.java b/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelectionOutsideVariableSegment_after.java new file mode 100644 index 000000000000..b29a09844c3b --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelectionOutsideVariableSegment_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelectionWithExistingSelection_after.java b/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelectionWithExistingSelection_after.java new file mode 100644 index 000000000000..d4b948a61897 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelectionWithExistingSelection_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelection_after.java b/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelection_after.java new file mode 100644 index 000000000000..edc768ad3ad8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/EndActionWithSelection_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/EndAction_after.java b/java/java-tests/testData/codeInsight/template/editor/EndAction_after.java new file mode 100644 index 000000000000..743964587be8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/EndAction_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/HomeActionOutsideVariableSegment_after.java b/java/java-tests/testData/codeInsight/template/editor/HomeActionOutsideVariableSegment_after.java new file mode 100644 index 000000000000..8144868d1873 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/HomeActionOutsideVariableSegment_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/HomeActionWithExistingSelection_after.java b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithExistingSelection_after.java new file mode 100644 index 000000000000..70e370828dc9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithExistingSelection_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelectionOutsideVariableSegment_after.java b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelectionOutsideVariableSegment_after.java new file mode 100644 index 000000000000..f483d4d8ee26 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelectionOutsideVariableSegment_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelectionWithExistingSelection_after.java b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelectionWithExistingSelection_after.java new file mode 100644 index 000000000000..1a054e94e733 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelectionWithExistingSelection_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelection_after.java b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelection_after.java new file mode 100644 index 000000000000..ae1755aba80d --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/HomeActionWithSelection_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/editor/HomeAction_after.java b/java/java-tests/testData/codeInsight/template/editor/HomeAction_after.java new file mode 100644 index 000000000000..70e370828dc9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/editor/HomeAction_after.java @@ -0,0 +1,5 @@ +public class A { + public static void main(String[] args) { + template myVariableValue text + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateEditorActionsTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateEditorActionsTest.java new file mode 100644 index 000000000000..ed29b7feca25 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateEditorActionsTest.java @@ -0,0 +1,128 @@ +/* + * Copyright 2000-2015 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.codeInsight.template; + +import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.template.impl.EmptyNode; +import com.intellij.codeInsight.template.impl.TemplateManagerImpl; +import com.intellij.codeInsight.template.impl.TemplateState; +import com.intellij.openapi.actionSystem.IdeActions; +import com.intellij.openapi.editor.EditorModificationUtil; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; + +public class LiveTemplateEditorActionsTest extends LightCodeInsightFixtureTestCase { + @Override + protected void tearDown() throws Exception { + try { + TemplateState templateState = TemplateManagerImpl.getTemplateState(myFixture.getEditor()); + if (templateState != null) { + templateState.gotoEnd(false); + } + } + finally { + //noinspection ThrowFromFinallyBlock + super.tearDown(); + } + } + + public void testHomeAction() { + doTest(IdeActions.ACTION_EDITOR_MOVE_LINE_START); + } + + public void testHomeActionWithExistingSelection() { + doTestWithSelection(IdeActions.ACTION_EDITOR_MOVE_LINE_START); + } + + public void testHomeActionOutsideVariableSegment() { + doTestOutsideSegment(IdeActions.ACTION_EDITOR_MOVE_LINE_START); + } + + public void testHomeActionWithSelection() { + doTest(IdeActions.ACTION_EDITOR_MOVE_LINE_START_WITH_SELECTION); + } + + public void testHomeActionWithSelectionWithExistingSelection() { + doTestWithSelection(IdeActions.ACTION_EDITOR_MOVE_LINE_START_WITH_SELECTION); + } + + public void testHomeActionWithSelectionOutsideVariableSegment() { + doTestOutsideSegment(IdeActions.ACTION_EDITOR_MOVE_LINE_START_WITH_SELECTION); + } + + public void testEndAction() { + doTest(IdeActions.ACTION_EDITOR_MOVE_LINE_END); + } + + public void testEndActionWithExistingSelection() { + doTestWithSelection(IdeActions.ACTION_EDITOR_MOVE_LINE_END); + } + + public void testEndActionOutsideVariableSegment() { + doTestOutsideSegment(IdeActions.ACTION_EDITOR_MOVE_LINE_END); + } + + public void testEndActionWithSelection() { + doTest(IdeActions.ACTION_EDITOR_MOVE_LINE_END_WITH_SELECTION); + } + + public void testEndActionWithSelectionWithExistingSelection() { + doTestWithSelection(IdeActions.ACTION_EDITOR_MOVE_LINE_END_WITH_SELECTION); + } + + public void testEndActionWithSelectionOutsideVariableSegment() { + doTestOutsideSegment(IdeActions.ACTION_EDITOR_MOVE_LINE_END_WITH_SELECTION); + } + + public void doTest(@NotNull String actionId) { + expendTemplate(); + EditorModificationUtil.moveCaretRelatively(myFixture.getEditor(), -5); + myFixture.performEditorAction(actionId); + myFixture.checkResultByFile(getTestName(false) + "_after.java"); + } + + private void doTestWithSelection(@NotNull String actionId) { + expendTemplate(); + EditorModificationUtil.moveCaretRelatively(myFixture.getEditor(), -5); + myFixture.getEditor().getSelectionModel().setSelection(myFixture.getCaretOffset(), myFixture.getCaretOffset() + 3); + myFixture.performEditorAction(actionId); + myFixture.checkResultByFile(getTestName(false) + "_after.java"); + } + + private void doTestOutsideSegment(@NotNull String actionId) { + expendTemplate(); + EditorModificationUtil.moveCaretRelatively(myFixture.getEditor(), 2); + myFixture.performEditorAction(actionId); + myFixture.checkResultByFile(getTestName(false) + "_after.java"); + } + + private void expendTemplate() { + TemplateManagerImpl.setTemplateTesting(myFixture.getProject(), getTestRootDisposable()); + myFixture.configureByFile("BeforeTestData.java"); + + final TemplateManager manager = TemplateManager.getInstance(getProject()); + final Template template = manager.createTemplate("myTemplate", "test", "template $VAR$ text"); + template.addVariable("VAR", new EmptyNode(), true); + TemplateManager.getInstance(myFixture.getProject()).startTemplate(myFixture.getEditor(), template); + + myFixture.type("myVariableValue"); + } + + @Override + protected String getBasePath() { + return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/template/editor/"; + } +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/LineStartEndWithSelectionHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/LineStartEndWithSelectionHandler.java index 8cd0fb27d062..f7fa619f3fff 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/LineStartEndWithSelectionHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/editorActions/LineStartEndWithSelectionHandler.java @@ -43,10 +43,11 @@ public abstract class LineStartEndWithSelectionHandler extends EditorActionHandl final TextRange range = templateState.getCurrentVariableRange(); final int caretOffset = editor.getCaretModel().getOffset(); if (range != null && range.getStartOffset() <= caretOffset && caretOffset <= range.getEndOffset()) { + int selectionOffset = editor.getSelectionModel().getLeadSelectionOffset(); int offsetToMove = myIsHomeHandler ? range.getStartOffset() : range.getEndOffset(); editor.getCaretModel().moveToOffset(offsetToMove); EditorModificationUtil.scrollToCaret(editor); - editor.getSelectionModel().setSelection(myIsHomeHandler ? offsetToMove : caretOffset, myIsHomeHandler ? caretOffset : offsetToMove); + editor.getSelectionModel().setSelection(selectionOffset, offsetToMove); return; } }