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 49efa9ee6dca..0fb5bb923416 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -631,4 +631,24 @@ public interface Test { assert !lookup } + public void testTemplateSelection() { + myFixture.configureByText("a.java", """ +class Foo { + int ITER = 2; + int itea = 2; + + { + it + } +} +""") + type 'e' + assertOrderedEquals myFixture.lookupElementStrings, "itea" + type 'r' + assertOrderedEquals myFixture.lookupElementStrings, "iter", "ITER", "Iterable", "Iterator" + type ',' + assert !lookup + assert myFixture.editor.document.text.contains('iter,') + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/RelaxedMatchingContributor.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/RelaxedMatchingContributor.java index 94b00f7f950e..76b9ef68f4bf 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/RelaxedMatchingContributor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/RelaxedMatchingContributor.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.completion; import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.template.impl.LiveTemplateLookupElement; import com.intellij.patterns.PatternCondition; import com.intellij.patterns.StandardPatterns; import com.intellij.util.Consumer; @@ -63,7 +64,7 @@ public class RelaxedMatchingContributor extends CompletionContributor { } CompletionParameters relaxed; - if (elements.isEmpty() && parameters.getInvocationCount() == 0) { + if (parameters.getInvocationCount() == 0 && (elements.isEmpty() || elements.size() == 1 && elements.iterator().next() instanceof LiveTemplateLookupElement)) { relaxed = parameters.withRelaxedMatching(); } else if (parameters.getInvocationCount() >= 2) { 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 new file mode 100644 index 000000000000..a34084de0e0f --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateCharFilter.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.impl; + +import com.intellij.codeInsight.lookup.CharFilter; +import com.intellij.codeInsight.lookup.Lookup; + +/** + * @author peter + */ +public class LiveTemplateCharFilter extends CharFilter { + @Override + public Result acceptChar(char c, int prefixLength, Lookup lookup) { + if (lookup.getCurrentItem() instanceof LiveTemplateLookupElement && c != ' ') { + return Result.HIDE_LOOKUP; + } + + return null; + } +} 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 bb3b5915a753..70d741d5351f 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 @@ -16,9 +16,6 @@ package com.intellij.codeInsight.template.impl; import com.intellij.codeInsight.completion.*; -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.codeInsight.lookup.LookupElementBuilder; -import com.intellij.codeInsight.template.TemplateManager; import com.intellij.openapi.util.Condition; import com.intellij.patterns.PlatformPatterns; import com.intellij.psi.PsiFile; @@ -48,14 +45,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { final String prefix = result.getPrefixMatcher().getPrefix(); final TemplateImpl template = findApplicableTemplate(file, offset, prefix); if (template != null) { - result.addElement(LookupElementBuilder.create(prefix).setTypeText(template.getDescription()).setInsertHandler(new InsertHandler() { - @Override - public void handleInsert(InsertionContext context, LookupElement item) { - context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); - context.setAddCompletionChar(false); - TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), template); - } - })); + result.addElement(new LiveTemplateLookupElement(prefix, 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 new file mode 100644 index 000000000000..9257262c328c --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/LiveTemplateLookupElement.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.template.impl; + +import com.intellij.codeInsight.completion.InsertionContext; +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.lookup.LookupElementPresentation; +import com.intellij.codeInsight.template.TemplateManager; +import org.jetbrains.annotations.NotNull; + +/** + * @author peter + */ +public class LiveTemplateLookupElement extends LookupElement { + private final String myPrefix; + private final TemplateImpl myTemplate; + + public LiveTemplateLookupElement(String prefix, TemplateImpl template) { + myPrefix = prefix; + myTemplate = template; + } + @NotNull + @Override + public String getLookupString() { + return myPrefix; + } + + @Override + public void renderElement(LookupElementPresentation presentation) { + super.renderElement(presentation); + presentation.setTypeText(myTemplate.getDescription()); + } + + @Override + public void handleInsert(InsertionContext context) { + context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset()); + context.setAddCompletionChar(false); + TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), myTemplate); + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index a5fa52cf904c..a5c953e1988c 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -402,6 +402,7 @@ +