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<Tab>.
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.
This commit is contained in:
Alexander Zolotov
2018-12-17 17:01:20 +03:00
parent e498a28646
commit ef0bf43013
3 changed files with 54 additions and 7 deletions
@@ -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", "<caret>");
createTemplate().setShortcutChar(TemplateSettings.NONE_CHAR);
type("z");
assertNull(myFixture.getLookup());
}
public void testShowTemplateWithShortcutInAutoPopup() {
myFixture.configureByText("a.java", "<caret>");
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;
}
}
@@ -72,26 +72,27 @@ public class LiveTemplateCompletionContributor extends CompletionContributor {
int offset = editor.getCaretModel().getOffset();
final List<TemplateImpl> availableTemplates = TemplateManagerImpl.listApplicableTemplates(file, offset, false);
final Map<TemplateImpl, String> 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<TemplateImpl, String> templates, CompletionResultSet result) {
private static void ensureTemplatesShown(AtomicBoolean templatesShown,
Map<TemplateImpl, String> templates,
CompletionResultSet result,
boolean isAutopopup) {
if (!templatesShown.getAndSet(true)) {
result.restartCompletionOnPrefixChange(StandardPatterns.string().with(new PatternCondition<String>("type after non-identifier") {
@Override
@@ -143,6 +147,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor {
}));
for (final Map.Entry<TemplateImpl, String> 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));
}
-2
View File
@@ -14,7 +14,6 @@
<option name="GROOVY" value="false" />
<option name="GROOVY_STATEMENT" value="false" />
<option name="JSON" value="false" />
<option name="KOTLIN" value="false" />
<option name="OTHER" value="true" />
</context>
</template>
@@ -32,7 +31,6 @@
<option name="GROOVY" value="false" />
<option name="GROOVY_EXPRESSION" value="false" />
<option name="JSON" value="false" />
<option name="KOTLIN" value="false" />
<option name="OTHER" value="true" />
</context>
</template>