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 d89735064f6f..52f8b78d177d 100644
--- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy
+++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2015 JetBrains s.r.o.
+ * Copyright 2000-2016 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.codeInsight.template
import com.intellij.JavaTestUtil
import com.intellij.codeInsight.CodeInsightSettings
+import com.intellij.codeInsight.completion.CompletionType
import com.intellij.codeInsight.lookup.Lookup
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.impl.LookupImpl
@@ -536,6 +537,7 @@ class Outer {
"test do not replace macro value with null result",
"test escape string characters in soutv",
"test escape shouldn't move caret to the end marker",
+ "test finish template on moving caret by completion insert handler",
"test do not replace macro value with empty result"]) {
runnable.run();
return;
@@ -1146,4 +1148,16 @@ class Foo {
}}
'''
}
-}
+
+ public void "test finish template on moving caret by completion insert handler"() {
+ TemplateManagerImpl templateManager = TemplateManager.getInstance(project) as TemplateManagerImpl
+ myFixture.configureByText('a.html', '')
+ def template = TemplateSettings.instance.getTemplate("T2", "html/xml")
+ myFixture.testAction(new InvokeTemplateAction(template, myFixture.editor, myFixture.project, ContainerUtil.newHashSet()))
+ myFixture.complete(CompletionType.BASIC)
+ myFixture.type("nofra")
+ myFixture.finishLookup(Lookup.REPLACE_SELECT_CHAR)
+ myFixture.checkResult("")
+ assertNull(templateManager.getActiveTemplate(myFixture.editor))
+ }
+}
\ No newline at end of file
diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java
index f4deab49b496..f01239d89928 100644
--- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java
+++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2015 JetBrains s.r.o.
+ * Copyright 2000-2016 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.
@@ -22,6 +22,7 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.options.SchemeElement;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.tree.IElementType;
+import com.intellij.util.containers.IntArrayList;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -125,7 +126,7 @@ public class TemplateImpl extends Template implements SchemeElement {
}
@Override
- public void addVariableSegment (@NotNull String name) {
+ public void addVariableSegment(@NotNull String name) {
mySegments.add(new Segment(name, myTemplateText.length()));
}
@@ -271,6 +272,18 @@ public class TemplateImpl extends Template implements SchemeElement {
return -1;
}
+ public IntArrayList getVariableSegmentNumbers(String variableName) {
+ IntArrayList result = new IntArrayList();
+ parseSegments();
+ for (int i = 0; i < mySegments.size(); i++) {
+ Segment segment = mySegments.get(i);
+ if (segment.name.equals(variableName)) {
+ result.add(i);
+ }
+ }
+ return result;
+ }
+
@NotNull
@Override
public String getTemplateText() {