From f6f31abd887d55d9629aae4ffd74a36014f3d1d9 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 12 Oct 2011 20:11:09 +0200 Subject: [PATCH] don't select autopopup live templates --- .../completion/JavaAutoPopupTest.groovy | 6 +++--- .../codeInsight/lookup/impl/LookupImpl.java | 9 +++++++-- .../template/impl/ListTemplatesHandler.java | 2 +- .../LiveTemplateCompletionContributor.java | 16 +++++++++++++--- .../impl/LiveTemplateLookupElement.java | 19 +++++++++++++++++-- .../src/META-INF/LangExtensions.xml | 2 ++ 6 files changed, 43 insertions(+), 11 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 84a884851ae6..858f42ec57e2 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -66,7 +66,7 @@ class JavaAutoPopupTest extends CompletionAutoPopupTestCase { type('er') assertOrderedEquals myFixture.lookupElementStrings, "iter", "iterable" - assertEquals 'iter', lookup.currentItem.lookupString + assertEquals 'iterable', lookup.currentItem.lookupString assert lookup.focused type 'a' @@ -651,7 +651,7 @@ class Foo { assertOrderedEquals myFixture.lookupElementStrings, "iter", "itera" type ',' assert !lookup - assert myFixture.editor.document.text.contains('iter,') + assert myFixture.editor.document.text.contains('itera,') } public void testTemplateSelectionBySpace() { @@ -995,7 +995,7 @@ public class UTest { assert 'xxxxx.SYSTEM_EXCEPTION' == ((JavaPsiClassReferenceElement) myFixture.lookupElements[2]).qualifiedName } - public void testTabShouldPreferLookupsToLiveTemplate() { + public void _testTabShouldPreferLookupsToLiveTemplate() { myFixture.configureByText "a.java", """ class LiveComplete { public void innerThing() { } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java index 896e5e1a863e..2e566f311c0c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupImpl.java @@ -24,6 +24,7 @@ import com.intellij.codeInsight.completion.impl.CamelHumpMatcher; import com.intellij.codeInsight.hint.HintManager; import com.intellij.codeInsight.hint.HintManagerImpl; import com.intellij.codeInsight.lookup.*; +import com.intellij.codeInsight.template.impl.LiveTemplateLookupElement; import com.intellij.featureStatistics.FeatureUsageTracker; import com.intellij.ide.ui.UISettings; import com.intellij.lang.LangBundle; @@ -453,7 +454,7 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable if (!model.isEmpty()) { LookupElement first = model.iterator().next(); - if (isFocused() && (!isExactPrefixItem(first, true) || mySelectionTouched)) { + if (isFocused() && (!isExactPrefixItem(first, true) || mySelectionTouched || shouldSkip(first))) { restoreSelection(oldSelected, hasPreselected, oldInvariant, snapshot.second); } else { @@ -462,6 +463,10 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable } } + private static boolean shouldSkip(LookupElement element) { + return element instanceof LiveTemplateLookupElement && ((LiveTemplateLookupElement)element).sudden; + } + private boolean isSelectionVisible() { return myList.getFirstVisibleIndex() <= myList.getSelectedIndex() && myList.getSelectedIndex() <= myList.getLastVisibleIndex(); } @@ -1264,7 +1269,7 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable for (int i = 0; i < items.size(); i++) { LookupElement item = items.get(i); - if (isExactPrefixItem(item, true)) { + if (isExactPrefixItem(item, true) && !shouldSkip(item)) { return i; } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/ListTemplatesHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/ListTemplatesHandler.java index 195c2b9962e1..4d5092d11f37 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/ListTemplatesHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/ListTemplatesHandler.java @@ -68,7 +68,7 @@ public class ListTemplatesHandler implements CodeInsightActionHandler { public static void showTemplatesLookup(final Project project, final Editor editor, String prefix, List matchingTemplates) { List array = new ArrayList(); for (TemplateImpl template : matchingTemplates) { - array.add(new LiveTemplateLookupElement(template)); + array.add(new LiveTemplateLookupElement(template, false)); } LookupElement[] items = array.toArray(new LookupElement[array.size()]); 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 a919b2958611..8679d726deb0 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,6 +16,7 @@ package com.intellij.codeInsight.template.impl; import com.intellij.codeInsight.completion.*; +import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.template.TemplateContextType; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Ref; @@ -67,7 +68,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(template)); + result.addElement(new LiveTemplateLookupElement(template, true)); } for (final TemplateImpl possible : templates) { result.restartCompletionOnPrefixChange(possible.getKey()); @@ -77,11 +78,11 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { }); } - private void ensureTemplatesShown(Ref templatesShown, List templates, CompletionResultSet result) { + private static void ensureTemplatesShown(Ref templatesShown, List templates, CompletionResultSet result) { if (!templatesShown.get()) { templatesShown.set(true); for (final TemplateImpl possible : templates) { - result.addElement(new LiveTemplateLookupElement(possible)); + result.addElement(new LiveTemplateLookupElement(possible, false)); } } } @@ -107,4 +108,13 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { } }); } + + public static class Skipper extends CompletionPreselectSkipper { + + @Override + public boolean skipElement(LookupElement element, CompletionLocation location) { + return element instanceof LiveTemplateLookupElement && ((LiveTemplateLookupElement)element).sudden; + } + } + } 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 2a53b3e03c8a..6254c6d05c3b 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 @@ -21,14 +21,18 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation; import com.intellij.codeInsight.template.TemplateManager; import org.jetbrains.annotations.NotNull; +import java.awt.event.KeyEvent; + /** * @author peter */ public class LiveTemplateLookupElement extends LookupElement { private final String myPrefix; private final TemplateImpl myTemplate; + public final boolean sudden; - public LiveTemplateLookupElement(TemplateImpl template) { + public LiveTemplateLookupElement(TemplateImpl template, boolean sudden) { + this.sudden = sudden; myPrefix = template.getKey(); myTemplate = template; } @@ -45,7 +49,18 @@ public class LiveTemplateLookupElement extends LookupElement { @Override public void renderElement(LookupElementPresentation presentation) { super.renderElement(presentation); - presentation.setTypeText(myTemplate.getDescription()); + if (sudden) { + presentation.setItemTextBold(true); + char shortcutChar = myTemplate.getShortcutChar(); + if (shortcutChar == TemplateSettings.DEFAULT_CHAR) { + shortcutChar = TemplateSettings.getInstance().getDefaultShortcutChar(); + } + presentation.setTypeText(" [" + KeyEvent.getKeyText(shortcutChar) + "] "); + presentation.setTailText(" (" + myTemplate.getDescription() + ")", true); + } else { + presentation.setTypeText(myTemplate.getDescription()); + + } } @Override diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 11aabf96956a..1af462c9d7da 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -438,6 +438,8 @@ + +