From cc8eb23f33a41397372be23e338063b788ada144 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 13 Apr 2011 18:29:58 +0200 Subject: [PATCH] don't choose template items by space if normally they're not configured to do so --- .../completion/JavaAutoPopupTest.groovy | 17 ++++++++++++++++- .../template/impl/LiveTemplateCharFilter.java | 7 ++++++- .../impl/LiveTemplateCompletionContributor.java | 4 ++-- .../impl/LiveTemplateLookupElement.java | 8 ++++++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy index 0fb5bb923416..fee8a61ac4c1 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -631,7 +631,7 @@ public interface Test { assert !lookup } - public void testTemplateSelection() { + public void testTemplateSelectionByComma() { myFixture.configureByText("a.java", """ class Foo { int ITER = 2; @@ -651,4 +651,19 @@ class Foo { assert myFixture.editor.document.text.contains('iter,') } + public void testTemplateSelectionBySpace() { + myFixture.configureByText("a.java", """ +class Foo { + int ITER = 2; + int itea = 2; + + { + it + } +} +""") + type 'er ' + assert myFixture.editor.document.text.contains('iter ') + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCharFilter.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCharFilter.java index a34084de0e0f..12c18d53385d 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCharFilter.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCharFilter.java @@ -17,6 +17,7 @@ package com.intellij.codeInsight.template.impl; import com.intellij.codeInsight.lookup.CharFilter; import com.intellij.codeInsight.lookup.Lookup; +import com.intellij.codeInsight.lookup.LookupElement; /** * @author peter @@ -24,7 +25,11 @@ import com.intellij.codeInsight.lookup.Lookup; public class LiveTemplateCharFilter extends CharFilter { @Override public Result acceptChar(char c, int prefixLength, Lookup lookup) { - if (lookup.getCurrentItem() instanceof LiveTemplateLookupElement && c != ' ') { + LookupElement item = lookup.getCurrentItem(); + if (item instanceof LiveTemplateLookupElement) { + if (c == ((LiveTemplateLookupElement)item).getTemplate().getShortcutChar()) { + return Result.SELECT_ITEM_AND_FINISH_LOOKUP; + } return Result.HIDE_LOOKUP; } 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 8638cd9a7ad5..d3a29aa09c16 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 @@ -45,7 +45,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { final int offset = parameters.getOffset(); if (Registry.is("show.live.templates.in.completion")) { for (final TemplateImpl possible : listApplicableTemplates(file, offset)) { - result.addElement(new LiveTemplateLookupElement(possible.getKey(), possible)); + result.addElement(new LiveTemplateLookupElement(possible)); } return; } @@ -53,7 +53,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { final String prefix = result.getPrefixMatcher().getPrefix(); final TemplateImpl template = findApplicableTemplate(file, offset, prefix); if (template != null) { - result.addElement(new LiveTemplateLookupElement(prefix, template)); + result.addElement(new LiveTemplateLookupElement(template)); } else { for (final TemplateImpl possible : listApplicableTemplates(file, offset)) { result.restartCompletionOnPrefixChange(possible.getKey()); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateLookupElement.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateLookupElement.java index 9257262c328c..2a53b3e03c8a 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateLookupElement.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateLookupElement.java @@ -28,8 +28,8 @@ public class LiveTemplateLookupElement extends LookupElement { private final String myPrefix; private final TemplateImpl myTemplate; - public LiveTemplateLookupElement(String prefix, TemplateImpl template) { - myPrefix = prefix; + public LiveTemplateLookupElement(TemplateImpl template) { + myPrefix = template.getKey(); myTemplate = template; } @NotNull @@ -38,6 +38,10 @@ public class LiveTemplateLookupElement extends LookupElement { return myPrefix; } + public TemplateImpl getTemplate() { + return myTemplate; + } + @Override public void renderElement(LookupElementPresentation presentation) { super.renderElement(presentation);