a more generic way to disprefer *Ex, *Impl classes and String.subSequence method

This commit is contained in:
peter.gromov
2010-11-17 21:14:27 +03:00
parent ebff15c9eb
commit 1dc9d6426e
10 changed files with 45 additions and 64 deletions

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2000-2010 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.completion;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.patterns.PsiJavaPatterns;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
/**
* @author peter
*/
public class DispreferImplementationsWeigher extends CompletionWeigher {
public Comparable weigh(@NotNull final LookupElement item, @NotNull final CompletionLocation location) {
final Object object = item.getObject();
if (object instanceof PsiClass) {
if (PsiJavaPatterns.psiElement().afterLeaf(PsiKeyword.NEW).accepts(location.getCompletionParameters().getPosition())) {
return 0;
}
final String qname = ((PsiClass)object).getQualifiedName();
if (qname != null && qname.endsWith("Impl")) {
return -1;
}
}
return 0;
}
}

View File

@@ -7,5 +7,6 @@ public class Aaaaaaa {
}
class XxxEx {}
class XxxImpl {}
class Xxy {}

View File

@@ -2,10 +2,9 @@ public class Aaaaaaa {
private final String aaa;
Aaaaaaa(Object aabbb) {
new Xx<caret>
Xxx x = new Xx<caret>
}
}
class XxxImpl {}
class Xxy {}
class XxxImpl implements Xxx {}

View File

@@ -0,0 +1,12 @@
public class Aaaaaaa {
private final String aaa;
Aaaaaaa(Object aabbb) {
if (aabbb instanceof XY<caret>)
}
}
class XaYaEx {}
class XaYaImpl {}
class XyYaXa {}

View File

@@ -2,7 +2,7 @@ class A{
{
String str;
str.subSequence(<caret>);
str.substring(<caret>);
jhasgfjhsdgf();
}
}

View File

@@ -223,7 +223,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testTryInExpression() throws Throwable {
configureByFile(BASE_PATH + "/" + getTestName(true) + ".java");
assertStringItems("toString", "this");
assertStringItems("this", "toString");
}
public void testNull() throws Exception{

View File

@@ -7,6 +7,7 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiMethod;
@SuppressWarnings({"ALL"})
@@ -108,12 +109,19 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
public void testDispreferImpls() throws Throwable {
VfsUtil.saveText(getSourceRoot().createChildDirectory(this, "foo").createChildData(this, "Xxx.java"), "package foo; public class Xxx {}");
checkPreferredItems(0, "Xxy", "Xxx", "XxxImpl");
checkPreferredItems(0, "Xxy", "Xxx", "XxxEx", "XxxImpl");
}
public void testDontDispreferImplsAfterNew() throws Throwable {
VfsUtil.saveText(getSourceRoot().createChildDirectory(this, "foo").createChildData(this, "Xxx.java"), "package foo; public class Xxx {}");
checkPreferredItems(0, "XxxImpl", "Xxy", "Xxx");
VfsUtil.saveText(getSourceRoot().createChildDirectory(this, "foo").createChildData(this, "Xxx.java"), "package foo; public interface Xxx {}");
checkPreferredItems(0, "Xxx", "XxxImpl");
}
public void testPreferLessHumps() throws Throwable {
final VirtualFile foo = getSourceRoot().createChildDirectory(this, "foo");
VfsUtil.saveText(foo.createChildData(this, "XaYa.java"), "package foo; public interface XaYa {}");
VfsUtil.saveText(foo.createChildData(this, "XyYa.java"), "package foo; public interface XyYa {}");
checkPreferredItems(0, "XaYa", "XyYa", "XaYaEx", "XaYaImpl", "XyYaXa");
}
public void testPreferLessParameters() throws Throwable {

View File

@@ -90,8 +90,8 @@ public class NormalCompletionTest extends LightFixtureCompletionTestCase {
configureByFile("QualifiedNew1.java");
assertEquals(2, myItems.length);
assertEquals("IInner", myItems[0].getLookupString());
assertEquals("Inner", myItems[1].getLookupString());
assertEquals("Inner", myItems[0].getLookupString());
assertEquals("IInner", myItems[1].getLookupString());
}
public void testQualifiedNew2() throws Exception {

View File

@@ -18,7 +18,6 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author peter
@@ -26,14 +25,21 @@ import org.jetbrains.annotations.Nullable;
public class PrefixMatchingWeigher extends CompletionWeigher {
public Comparable weigh(@NotNull final LookupElement item, @NotNull final CompletionLocation location) {
if (location == null) {
return null;
final String prefix = item.getPrefixMatcher().getPrefix();
if (prefix.isEmpty()) {
return 0;
}
if (location.getCompletionType() == CompletionType.CLASS_NAME) return 0;
final String lookupString = item.getLookupString();
return (StringUtil.capitalsOnly(lookupString).startsWith(StringUtil.capitalsOnly(item.getPrefixMatcher().getPrefix())) ? 4 : 0) +
(lookupString.startsWith(item.getPrefixMatcher().getPrefix()) ? 2 : 0) +
(StringUtil.startsWithIgnoreCase(lookupString, item.getPrefixMatcher().getPrefix()) ? 1 : 0);
final String prefixHumps = StringUtil.capitalsOnly(prefix);
final String itemHumps = StringUtil.capitalsOnly(lookupString);
if (itemHumps.equals(prefixHumps)) return 20;
if (itemHumps.startsWith(prefixHumps)) return 10;
if (lookupString.startsWith(prefix)) return 5;
if (StringUtil.startsWithIgnoreCase(lookupString, prefix)) return 1;
return 0;
}
}

View File

@@ -801,16 +801,14 @@
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.NameEndMatchingDegreeWeigher" id="nameEnd"
order="after expectedType, before stats"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.DispreferImplementationsWeigher" id="implementations"
order="after prefix"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferNonGenericWeigher" id="nonGeneric"
order="after implementations"/>
order="after stats"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferAccessibleWeigher" id="accessible"
order="after nonGeneric"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferSimpleWeigher" id="simple"
order="after accessible"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferEnumConstantsWeigher" id="constants"
order="after simple, before proximity"/>
order="after simple, before prefix"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.SameWordsWeigher" id="sameWords"
order="after proximity"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferFieldsAndGettersWeigher" id="fieldsAndGetters"