From 842afe69f1c5e18608779258232fbead22b26b03 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Fri, 14 Oct 2016 18:19:42 +0200 Subject: [PATCH] treat backspace/delete on interval border as outside template (IDEA-151493) --- .../template/LiveTemplateTest.groovy | 20 +++++++++++++++++++ .../template/impl/TemplateState.java | 17 +++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy index cd11766ed703..df341563b0d7 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -32,6 +32,7 @@ import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiDocumentManager import com.intellij.psi.codeStyle.CodeStyleSettingsManager import com.intellij.psi.codeStyle.CommonCodeStyleSettings +import com.intellij.testFramework.LightPlatformCodeInsightTestCase import com.intellij.testFramework.fixtures.CodeInsightTestUtil import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase import com.intellij.util.containers.ContainerUtil @@ -1235,6 +1236,25 @@ class Foo {{ """ } + void "test delete at the last template position"() { + myFixture.configureByText 'a.java', """ +class Foo {{ + +}} +""" + myFixture.type 'iter\t' + LightPlatformCodeInsightTestCase.delete(myFixture.editor, myFixture.project) + myFixture.checkResult """ +class Foo {{ + for (Object o : { + + } +}} +""" + } + + + void "test multicaret expanding with space"() { myFixture.configureByText "a.java", """\ class Foo { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java index bcd4b755470e..ac5f4e0c2537 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java @@ -22,8 +22,10 @@ import com.intellij.codeInsight.lookup.impl.LookupImpl; import com.intellij.codeInsight.template.*; import com.intellij.codeInsight.template.macro.TemplateCompletionProcessor; import com.intellij.diagnostic.AttachmentFactory; +import com.intellij.idea.ActionsBundle; import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.Disposable; +import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandAdapter; import com.intellij.openapi.command.CommandEvent; @@ -122,7 +124,7 @@ public class TemplateState implements Disposable { myLookupListener = new LookupAdapter() { @Override public void itemSelected(LookupEvent event) { - if (isCaretOutsideCurrentSegment()) { + if (isCaretOutsideCurrentSegment(null)) { if (isCaretInsideNextVariable()) { nextTab(); } @@ -148,7 +150,7 @@ public class TemplateState implements Disposable { @Override public void commandStarted(CommandEvent event) { - myDocumentChangesTerminateTemplate = isCaretOutsideCurrentSegment(); + myDocumentChangesTerminateTemplate = isCaretOutsideCurrentSegment(event.getCommandName()); started = true; } @@ -199,10 +201,15 @@ public class TemplateState implements Disposable { return false; } - private boolean isCaretOutsideCurrentSegment() { + private boolean isCaretOutsideCurrentSegment(String commandName) { if (myEditor != null && myCurrentSegmentNumber >= 0) { final int offset = myEditor.getCaretModel().getOffset(); - return offset < mySegments.getSegmentStart(myCurrentSegmentNumber) || offset > mySegments.getSegmentEnd(myCurrentSegmentNumber); + + final int segmentStart = mySegments.getSegmentStart(myCurrentSegmentNumber); + if (offset < segmentStart || offset == segmentStart && ActionsBundle.actionText(IdeActions.ACTION_EDITOR_BACKSPACE).equals(commandName)) return true; + + final int segmentEnd = mySegments.getSegmentEnd(myCurrentSegmentNumber); + if (offset > segmentEnd || offset == segmentEnd && ActionsBundle.actionText(IdeActions.ACTION_EDITOR_DELETE).equals(commandName)) return true; } return false; } @@ -233,7 +240,7 @@ public class TemplateState implements Disposable { } public boolean isToProcessTab() { - if (isCaretOutsideCurrentSegment()) { + if (isCaretOutsideCurrentSegment(null)) { return false; } if (ourLookupShown) {