after instanceof, prefer interfaces and abstract classes

This commit is contained in:
peter.gromov
2010-10-26 20:45:27 +04:00
parent 690f841f65
commit 1e148bbbda
5 changed files with 78 additions and 2 deletions

View File

@@ -0,0 +1,54 @@
/*
* 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.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author peter
*/
public class PreferAbstractWeigher extends CompletionWeigher {
public Comparable weigh(@NotNull final LookupElement item, @Nullable final CompletionLocation location) {
if (location == null || location.getCompletionType() != CompletionType.SMART) {
return null;
}
final PsiElement position = location.getCompletionParameters().getPosition();
final PsiElement parent = position.getParent();
if (!(parent instanceof PsiJavaCodeReferenceElement) ||
!(parent.getParent() instanceof PsiTypeElement) ||
!(parent.getParent().getParent() instanceof PsiInstanceOfExpression)) {
return null;
}
final Object object = item.getObject();
if (object instanceof PsiClass) {
final PsiClass aClass = (PsiClass)object;
if (aClass.isInterface()) {
return 2;
}
if (aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
return 1;
}
}
return 0;
}
}

View File

@@ -38,7 +38,9 @@ public class ExplicitlyImportedWeigher extends ProximityWeigher {
final PsiImportList importList = psiJavaFile.getImportList();
if (importList != null) {
for (final PsiImportStatement importStatement : importList.getImportStatements()) {
if (!importStatement.isOnDemand() && qname.equals(importStatement.getQualifiedName())) {
final boolean onDemand = importStatement.isOnDemand();
final String imported = importStatement.getQualifiedName();
if (onDemand && qname.startsWith(imported + ".") || !onDemand && qname.equals(imported)) {
return true;
}
}

View File

@@ -0,0 +1,14 @@
package zzzzz;
public class Holder {
{
Foo foo = null;
if (foo instanceof <caret>)
}
}
interface Foo {}
interface IFoo extends Foo {}
abstract class Goo implements IFoo {}
class Bar extends Goo {}

View File

@@ -203,6 +203,10 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase {
checkPreferredItems(0, "Goo", "InnerGoo", "Bar", "AGoo");
}
public void testPreferInterfaces() throws Throwable {
checkPreferredItems(0, "IFoo", "Goo", "Bar");
}
public void testLocalVariablesOutweighStats() throws Throwable {
final LookupImpl lookup = invokeCompletion(BASE_PATH + "/" + getTestName(false) + ".java");
assertPreferredItems(0, "foo", "param", "this", "bar", "goo");

View File

@@ -803,8 +803,10 @@
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.NameEndMatchingDegreeWeigher" id="nameEnd"
order="after expectedType, before stats"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferNonGenericWeigher" id="nonGeneric"
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferAbstractWeigher" id="abstract"
order="after prefix"/>
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferNonGenericWeigher" id="nonGeneric"
order="after abstract"/>
<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"