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 3ab71c0b61fa..bd1d6d00879d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -616,4 +616,10 @@ class Foo { assert myFixture.lookupElementStrings == ['value', 'value1'] } + public void "test invoke surround template by tab"() { + myFixture.configureByText "a.txt", "B" + myFixture.type('\t') + myFixture.checkResult("{}") + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCompletionContributor.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCompletionContributor.java index bfe6d921015f..47ea649fa042 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCompletionContributor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCompletionContributor.java @@ -100,7 +100,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { final ArrayList result = ContainerUtil.newArrayList(); for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) { - if (!template.isDeactivated() && !template.isSelectionTemplate() && TemplateManagerImpl.isApplicable(template, contextTypes)) { + if (!template.isDeactivated() && TemplateManagerImpl.isApplicable(template, contextTypes)) { result.add(template); } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java index 352acfbc854f..08034b248fe9 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/SurroundWithTemplateHandler.java @@ -104,10 +104,10 @@ public class SurroundWithTemplateHandler implements CodeInsightActionHandler { return template.isApplicable(file, editor.getSelectionModel().getSelectionStart(), true); } - public static ArrayList getApplicableTemplates(Editor editor, PsiFile file, boolean selection) { + public static ArrayList getApplicableTemplates(Editor editor, PsiFile file, boolean selectionOnly) { int startOffset = editor.getCaretModel().getOffset(); - if (selection && editor.getSelectionModel().hasSelection()) { + if (editor.getSelectionModel().hasSelection()) { startOffset = editor.getSelectionModel().getSelectionStart(); } @@ -116,7 +116,7 @@ public class SurroundWithTemplateHandler implements CodeInsightActionHandler { ArrayList list = new ArrayList(); for (TemplateImpl template : TemplateSettings.getInstance().getTemplates()) { if (!template.isDeactivated() && - template.isSelectionTemplate() == selection && + (!selectionOnly || template.isSelectionTemplate()) && TemplateManagerImpl.isApplicable(file, startOffset, template)) { list.add(template); } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateSettings.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateSettings.java index 1bf6071eb5d8..fb2350b2dbe4 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateSettings.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateSettings.java @@ -734,9 +734,6 @@ public class TemplateSettings implements PersistentStateComponent, Expo if (shortcutChar != null && getShortcutChar(template) != shortcutChar) { continue; } - if (template.isSelectionTemplate()) { - continue; - } if (hasArgument && !template.hasArgument()) { continue; } 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 cd46113bcd7f..dc9a918af2e3 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 @@ -846,6 +846,11 @@ public class TemplateState implements Disposable { else { if (!myTemplate.isSelectionTemplate() && !myTemplate.isInline()) { //do not move caret to the end of range for selection templates offset = myTemplateRange.getEndOffset(); + } else { + int selectionSegment = myTemplate.getVariableSegmentNumber(TemplateImpl.SELECTION); + if (selectionSegment >= 0 && mySegments.getSegmentStart(selectionSegment) == mySegments.getSegmentEnd(selectionSegment)) { + offset = mySegments.getSegmentStart(selectionSegment); + } } }