Prefer most recent exact-matched template in completion

This commit is contained in:
Alexander Zolotov
2015-09-28 12:23:39 +03:00
parent 2687cd7a22
commit bc7d98c107
2 changed files with 29 additions and 7 deletions
@@ -1224,6 +1224,25 @@ public class Test {
assert myFixture.lookup.currentItem instanceof LiveTemplateLookupElement
}
public void testMoreRecentExactMatchesTemplateFirst() {
TemplateManager manager = TemplateManager.getInstance(getProject());
Template template = manager.createTemplate("itar", "myGroup", null);
JavaCodeContextType contextType = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JavaCodeContextType.Statement);
((TemplateImpl)template).templateContext.setEnabled(contextType, true);
CodeInsightTestUtil.addTemplate(template, testRootDisposable)
LiveTemplateCompletionContributor.setShowTemplatesInTests(true, testRootDisposable)
myFixture.configureByText("a.java", """
public class Test {
void foo() {
ita<caret>
}
}""")
type 'r'
myFixture.assertPreferredCompletionItems(0, 'itar', 'itar')
}
public void testUpdatePrefixMatchingOnTyping() {
myFixture.addClass("class CertificateEncodingException {}")
myFixture.addClass("class CertificateException {}")
@@ -1412,7 +1431,7 @@ class Foo {{
}
public void testAmbiguousClassQualifier() {
myFixture.addClass("package foo; public class Util<T> { public static void foo() {}; public static final int CONSTANT = 2; }")
myFixture.addClass("package foo; public class Util<T> { public static void foo() {} public static final int CONSTANT = 2; }")
myFixture.addClass("package bar; public class Util { public static void bar() {} }")
myFixture.configureByText 'a.java', 'class Foo {{ Util<caret> }}'
type '.'
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 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.
@@ -391,11 +391,14 @@ public class CompletionLookupArranger extends LookupArranger {
for (int i = 0; i < items.size(); i++) {
LookupElement item = items.get(i);
boolean isSuddenLiveTemplate = isSuddenLiveTemplate(item);
if (isPrefixItem(lookup, item, true) && !isSuddenLiveTemplate ||
item.getLookupString().equals(selectedText)) {
if (exactMatchIndex == -1 || item instanceof LiveTemplateLookupElement) {
// prefer most recent item or LiveTemplate item
if (isPrefixItem(lookup, item, true) && !isSuddenLiveTemplate || item.getLookupString().equals(selectedText)) {
if (item instanceof LiveTemplateLookupElement) {
// prefer most recent live template lookup item
exactMatchIndex = i;
break;
}
if (exactMatchIndex == -1) {
// prefer most recent item
exactMatchIndex = i;
}
}