Live Templates: proper handling home/end actions with selection in templates

context

+ add tests for editor actions inside template segments
This commit is contained in:
Alexander Zolotov
2015-12-22 18:38:09 +03:00
parent 81f005aba1
commit ebf79c6dfb
15 changed files with 195 additions and 1 deletions
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
<caret>
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template myVariableValue text<caret>
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template myVariableValue<caret> text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template myVariableValue t<selection>ext</selection><caret>
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template myVariableVal<selection>ue</selection><caret> text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template myVariable<selection>Value</selection><caret> text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template myVariableValue<caret> text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
<caret>template myVariableValue text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template <caret>myVariableValue text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
<caret><selection>template myVariableValue t</selection>ext
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template <caret><selection>myVariableVal</selection>ue text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template <caret><selection>myVariable</selection>Value text
}
}
@@ -0,0 +1,5 @@
public class A {
public static void main(String[] args) {
template <caret>myVariableValue text
}
}
@@ -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/";
}
}
@@ -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;
}
}