Live Templates: finish template if completion insert handler moved caret

outside of current segment (WEB-20592)
This commit is contained in:
Alexander Zolotov
2016-04-13 21:28:16 +03:00
parent 150f042860
commit 3a56424070
2 changed files with 31 additions and 4 deletions
@@ -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', '<selection><p></p></selection>')
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("<noframes><caret><p></p></noframes>")
assertNull(templateManager.getActiveTemplate(myFixture.editor))
}
}
@@ -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() {