From bc7d98c10708b6ec08ddab31cf9a08dc61bbcfbb Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Mon, 28 Sep 2015 11:51:49 +0300 Subject: [PATCH] Prefer most recent exact-matched template in completion --- .../completion/JavaAutoPopupTest.groovy | 21 ++++++++++++++++++- .../completion/CompletionLookupArranger.java | 15 +++++++------ 2 files changed, 29 insertions(+), 7 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 9aa3d4df9904..e0a221524537 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -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 + } +}""") + 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 { public static void foo() {}; public static final int CONSTANT = 2; }") + myFixture.addClass("package foo; public class Util { 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 }}' type '.' diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java index abdd0793ac31..9f039ba59324 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionLookupArranger.java @@ -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; } }