From ef0bf430139c1f2aff694a46c6071d6b03ff8635 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Mon, 17 Dec 2018 16:42:51 +0300 Subject: [PATCH] Live templates: do not show templates without a shortcut in the autopopup (IDEA-204163, KT-8474) It's unlikely that users use such templates (P and B) as a regular live template: it's easier to type ( than P. It's even more unlikely if then use these template via auto-popup. Let's try to remove them from the auto-popup to address IDEA-204163 and KT-8474. --- .../template/LiveTemplateAutoPopupTest.java | 44 +++++++++++++++++++ .../LiveTemplateCompletionContributor.java | 15 ++++--- resources/src/liveTemplates/surround.xml | 2 - 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateAutoPopupTest.java diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateAutoPopupTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateAutoPopupTest.java new file mode 100644 index 000000000000..eaa74df07b3c --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/LiveTemplateAutoPopupTest.java @@ -0,0 +1,44 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight.template; + + +import com.intellij.codeInsight.completion.CompletionAutoPopupTestCase; +import com.intellij.codeInsight.template.JavaCodeContextType; +import com.intellij.codeInsight.template.TemplateContextType; +import com.intellij.codeInsight.template.TemplateManager; +import com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor; +import com.intellij.codeInsight.template.impl.TemplateImpl; +import com.intellij.codeInsight.template.impl.TemplateSettings; +import com.intellij.testFramework.fixtures.CodeInsightTestUtil; +import com.intellij.util.containers.ContainerUtil; + +public class LiveTemplateAutoPopupTest extends CompletionAutoPopupTestCase { + @Override + protected void setUp() { + super.setUp(); + LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable()); + } + + public void testDoNotShowTemplateWithoutShortcutInAutoPopup() { + myFixture.configureByText("a.java", ""); + createTemplate().setShortcutChar(TemplateSettings.NONE_CHAR); + type("z"); + assertNull(myFixture.getLookup()); + } + + public void testShowTemplateWithShortcutInAutoPopup() { + myFixture.configureByText("a.java", ""); + createTemplate().setShortcutChar(TemplateSettings.TAB_CHAR); + type("z"); + assertNotNull(myFixture.getLookup()); + } + + private TemplateImpl createTemplate() { + TemplateManager manager = TemplateManager.getInstance(getProject()); + TemplateImpl template = (TemplateImpl)manager.createTemplate("z", "user", ""); + TemplateContextType contextType = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JavaCodeContextType.class); + template.getTemplateContext().setEnabled(contextType, true); + CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable()); + return template; + } +} \ No newline at end of file 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 5409c2bc1819..bc0f45889aac 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 @@ -72,26 +72,27 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { int offset = editor.getCaretModel().getOffset(); final List availableTemplates = TemplateManagerImpl.listApplicableTemplates(file, offset, false); final Map templates = filterTemplatesByPrefix(availableTemplates, editor, offset, false, false); + boolean isAutopopup = parameters.getInvocationCount() == 0; if (showAllTemplates()) { final AtomicBoolean templatesShown = new AtomicBoolean(false); final CompletionResultSet finalResult = result; if (Registry.is("ide.completion.show.live.templates.on.top")) { - ensureTemplatesShown(templatesShown, templates, finalResult); + ensureTemplatesShown(templatesShown, templates, finalResult, isAutopopup); } result.runRemainingContributors(parameters, completionResult -> { finalResult.passResult(completionResult); if (completionResult.isStartMatch()) { - ensureTemplatesShown(templatesShown, templates, finalResult); + ensureTemplatesShown(templatesShown, templates, finalResult, isAutopopup); } }); - ensureTemplatesShown(templatesShown, templates, result); + ensureTemplatesShown(templatesShown, templates, result, isAutopopup); showCustomLiveTemplates(parameters, result); return; } - if (parameters.getInvocationCount() > 0) return; //only in autopopups for now + if (!isAutopopup) return; // custom templates should handle this situation by itself (return true from hasCompletionItems() and provide lookup element) // regular templates won't be shown in this case @@ -133,7 +134,10 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { return shouldShowAllTemplates(); } - private static void ensureTemplatesShown(AtomicBoolean templatesShown, Map templates, CompletionResultSet result) { + private static void ensureTemplatesShown(AtomicBoolean templatesShown, + Map templates, + CompletionResultSet result, + boolean isAutopopup) { if (!templatesShown.getAndSet(true)) { result.restartCompletionOnPrefixChange(StandardPatterns.string().with(new PatternCondition("type after non-identifier") { @Override @@ -143,6 +147,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor { })); for (final Map.Entry entry : templates.entrySet()) { ProgressManager.checkCanceled(); + if (isAutopopup && entry.getKey().getShortcutChar() == TemplateSettings.NONE_CHAR) continue; result.withPrefixMatcher(result.getPrefixMatcher().cloneWithPrefix(StringUtil.notNullize(entry.getValue()))) .addElement(new LiveTemplateLookupElementImpl(entry.getKey(), false)); } diff --git a/resources/src/liveTemplates/surround.xml b/resources/src/liveTemplates/surround.xml index 59cec842b3d2..de46d8b94e7a 100644 --- a/resources/src/liveTemplates/surround.xml +++ b/resources/src/liveTemplates/surround.xml @@ -14,7 +14,6 @@