mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
IDEA-89061 Prefix match should be preferred over middle match when suggesting variable names
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import java.util.*;
|
||||
|
||||
class BarGoo {}
|
||||
|
||||
class Foo {
|
||||
{
|
||||
Map<BarGoo, StringBuilder> goos;
|
||||
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
@@ -167,6 +167,16 @@ public class LiveTemplateTest extends LightCodeInsightFixtureTestCase {
|
||||
checkResult();
|
||||
}
|
||||
|
||||
public void testPreferStartMatchesInLookups() throws Throwable {
|
||||
configure();
|
||||
startTemplate("iter", "iterations")
|
||||
myFixture.type('ese\n') //for entrySet
|
||||
assert myFixture.lookupElementStrings == ['barGooStringBuilderEntry', 'gooStringBuilderEntry', 'stringBuilderEntry', 'builderEntry', 'entry']
|
||||
myFixture.type('e')
|
||||
assert myFixture.lookupElementStrings == ['entry', 'barGooStringBuilderEntry', 'gooStringBuilderEntry', 'stringBuilderEntry', 'builderEntry']
|
||||
assert LookupManager.getActiveLookup(editor).currentItem.lookupString == 'entry'
|
||||
}
|
||||
|
||||
private TemplateState getState() {
|
||||
TemplateManagerImpl.getTemplateState(getEditor())
|
||||
}
|
||||
|
||||
@@ -285,18 +285,21 @@ public class CompletionServiceImpl extends CompletionService{
|
||||
|
||||
@Override
|
||||
public Comparable weigh(@NotNull LookupElement element) {
|
||||
PrefixMatcher itemMatcher = getItemMatcher(element, myLocation);
|
||||
for (String ls : element.getAllLookupStrings()) {
|
||||
if (itemMatcher.isStartMatch(ls)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return !isStartMatch(element, myLocation.getCompletionParameters().getLookup());
|
||||
}
|
||||
}
|
||||
|
||||
private static PrefixMatcher getItemMatcher(LookupElement element, CompletionLocation location) {
|
||||
Lookup lookup = location.getCompletionParameters().getLookup();
|
||||
public static boolean isStartMatch(LookupElement element, Lookup lookup) {
|
||||
PrefixMatcher itemMatcher = getItemMatcher(element, lookup);
|
||||
for (String ls : element.getAllLookupStrings()) {
|
||||
if (itemMatcher.isStartMatch(ls)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static PrefixMatcher getItemMatcher(LookupElement element, Lookup lookup) {
|
||||
PrefixMatcher itemMatcher = lookup.itemMatcher(element);
|
||||
String pattern = lookup.itemPattern(element);
|
||||
if (!pattern.equals(itemMatcher.getPrefix())) {
|
||||
@@ -315,7 +318,7 @@ public class CompletionServiceImpl extends CompletionService{
|
||||
|
||||
@Override
|
||||
public Comparable weigh(@NotNull LookupElement element) {
|
||||
final PrefixMatcher matcher = getItemMatcher(element, myLocation);
|
||||
final PrefixMatcher matcher = getItemMatcher(element, myLocation.getCompletionParameters().getLookup());
|
||||
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (String lookupString : element.getAllLookupStrings()) {
|
||||
|
||||
@@ -1,96 +1,102 @@
|
||||
/*
|
||||
* Copyright 2000-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.codeInsight.lookup;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public abstract class LookupArranger {
|
||||
protected final List<LookupElement> myItems = new ArrayList<LookupElement>();
|
||||
|
||||
public void addElement(Lookup lookup, LookupElement item, LookupElementPresentation presentation) {
|
||||
myItems.add(item);
|
||||
}
|
||||
|
||||
public void prefixChanged() {
|
||||
}
|
||||
|
||||
public abstract Pair<List<LookupElement>, Integer> arrangeItems(@NotNull Lookup lookup, boolean onExplicitAction);
|
||||
|
||||
public abstract LookupArranger createEmptyCopy();
|
||||
|
||||
protected static void addPrefixItems(Lookup lookup, LinkedHashSet<LookupElement> result, boolean exactly, Collection<LookupElement> items) {
|
||||
for (LookupElement element : items) {
|
||||
if (isPrefixItem(lookup, element, exactly)) {
|
||||
result.add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean isPrefixItem(Lookup lookup, LookupElement item, final boolean exactly) {
|
||||
final String pattern = lookup.itemPattern(item);
|
||||
if (pattern.equals(item.getLookupString())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!exactly) {
|
||||
for (String s : item.getAllLookupStrings()) {
|
||||
if (s.equalsIgnoreCase(pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected List<LookupElement> matchingItems(Lookup lookup) {
|
||||
final List<LookupElement> items = new ArrayList<LookupElement>();
|
||||
for (LookupElement element : myItems) {
|
||||
if (lookup.prefixMatches(element)) {
|
||||
items.add(element);
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
public Map<LookupElement,StringBuilder> getRelevanceStrings() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
public static class DefaultArranger extends LookupArranger {
|
||||
public Pair<List<LookupElement>, Integer> arrangeItems(@NotNull Lookup lookup, boolean onExplicitAction) {
|
||||
LinkedHashSet<LookupElement> result = new LinkedHashSet<LookupElement>();
|
||||
List<LookupElement> items = matchingItems(lookup);
|
||||
addPrefixItems(lookup, result, true, items);
|
||||
addPrefixItems(lookup, result, false, items);
|
||||
result.addAll(items);
|
||||
ArrayList<LookupElement> list = new ArrayList<LookupElement>(result);
|
||||
int selected = list.indexOf(lookup.getCurrentItem());
|
||||
return new Pair<List<LookupElement>, Integer>(list, selected >= 0 ? selected : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookupArranger createEmptyCopy() {
|
||||
return new DefaultArranger();
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2000-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.codeInsight.lookup;
|
||||
|
||||
import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public abstract class LookupArranger {
|
||||
protected final List<LookupElement> myItems = new ArrayList<LookupElement>();
|
||||
|
||||
public void addElement(Lookup lookup, LookupElement item, LookupElementPresentation presentation) {
|
||||
myItems.add(item);
|
||||
}
|
||||
|
||||
public void prefixChanged() {
|
||||
}
|
||||
|
||||
public abstract Pair<List<LookupElement>, Integer> arrangeItems(@NotNull Lookup lookup, boolean onExplicitAction);
|
||||
|
||||
public abstract LookupArranger createEmptyCopy();
|
||||
|
||||
protected static void addPrefixItems(Lookup lookup, LinkedHashSet<LookupElement> result, boolean exactly, Collection<LookupElement> items) {
|
||||
for (LookupElement element : items) {
|
||||
if (isPrefixItem(lookup, element, exactly)) {
|
||||
result.add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean isPrefixItem(Lookup lookup, LookupElement item, final boolean exactly) {
|
||||
final String pattern = lookup.itemPattern(item);
|
||||
if (pattern.equals(item.getLookupString())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!exactly) {
|
||||
for (String s : item.getAllLookupStrings()) {
|
||||
if (s.equalsIgnoreCase(pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected List<LookupElement> matchingItems(Lookup lookup) {
|
||||
final List<LookupElement> items = new ArrayList<LookupElement>();
|
||||
for (LookupElement element : myItems) {
|
||||
if (lookup.prefixMatches(element)) {
|
||||
items.add(element);
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
public Map<LookupElement,StringBuilder> getRelevanceStrings() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
public static class DefaultArranger extends LookupArranger {
|
||||
public Pair<List<LookupElement>, Integer> arrangeItems(@NotNull Lookup lookup, boolean onExplicitAction) {
|
||||
LinkedHashSet<LookupElement> result = new LinkedHashSet<LookupElement>();
|
||||
List<LookupElement> items = matchingItems(lookup);
|
||||
addPrefixItems(lookup, result, true, items);
|
||||
addPrefixItems(lookup, result, false, items);
|
||||
for (LookupElement item : items) {
|
||||
if (CompletionServiceImpl.isStartMatch(item, lookup)) {
|
||||
result.add(item);
|
||||
}
|
||||
}
|
||||
result.addAll(items);
|
||||
ArrayList<LookupElement> list = new ArrayList<LookupElement>(result);
|
||||
int selected = onExplicitAction ? 0 : list.indexOf(lookup.getCurrentItem());
|
||||
return new Pair<List<LookupElement>, Integer>(list, selected >= 0 ? selected : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookupArranger createEmptyCopy() {
|
||||
return new DefaultArranger();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user