mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 06:39:38 +07:00
after instanceof, prefer interfaces and abstract classes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
@@ -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");
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user