use LookupElement.as instead of ad hoc inlining everywhere

This commit is contained in:
peter
2011-02-07 12:49:27 +01:00
parent 8c69c69acf
commit cb3a2d6e35
11 changed files with 25 additions and 73 deletions
@@ -81,14 +81,4 @@ public class CastingLookupElementDecorator extends LookupElementDecorator<Lookup
static LookupElement createCastingElement(final LookupElement delegate, PsiType castTo) {
return new CastingLookupElementDecorator(delegate, castTo);
}
public static @Nullable CastingLookupElementDecorator from(LookupElement element) {
if (element instanceof CastingLookupElementDecorator) return (CastingLookupElementDecorator)element;
else if (element instanceof LookupElementDecorator) {
element = ((LookupElementDecorator)element).getDelegate();
if (element instanceof CastingLookupElementDecorator) return (CastingLookupElementDecorator)element;
}
return null;
}
}
@@ -124,7 +124,7 @@ public class JavaChainLookupElement extends LookupElementDecorator<LookupElement
@NotNull
private LookupElement getComparableQualifier() {
final CastingLookupElementDecorator casting = CastingLookupElementDecorator.from(myQualifier);
final CastingLookupElementDecorator casting = myQualifier.as(CastingLookupElementDecorator.CLASS_CONDITION_KEY);
return casting == null ? myQualifier : casting.getDelegate();
}
@@ -19,7 +19,6 @@ import com.intellij.codeInsight.ExpectedTypeInfo;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.statistics.JavaStatisticsManager;
import com.intellij.psi.statistics.StatisticsInfo;
@@ -51,7 +50,7 @@ public class JavaCompletionStatistician extends CompletionStatistician{
}
}
LookupItem item = LookupItem.from(element);
LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY);
if (item == null) return null;
PsiType qualifierType = JavaCompletionUtil.getQualifierType(item);
@@ -503,13 +503,13 @@ public class JavaCompletionUtil {
@Nullable
public static PsiType getLookupElementType(final LookupElement element) {
TypedLookupItem typed = typedFrom(element);
TypedLookupItem typed = element.as(TypedLookupItem.CLASS_CONDITION_KEY);
if (typed != null) {
return typed.getType();
}
final PsiType qualifierType = getPsiType(element.getObject());
final LookupItem lookupItem = LookupItem.from(element);
final LookupItem lookupItem = element.as(LookupItem.CLASS_CONDITION_KEY);
if (lookupItem != null) {
final Object o = lookupItem.getAttribute(LookupItem.TYPE);
if (o instanceof PsiType) {
@@ -524,16 +524,6 @@ public class JavaCompletionUtil {
return qualifierType;
}
public static @Nullable TypedLookupItem typedFrom(LookupElement element) {
TypedLookupItem typed = null;
if (element instanceof TypedLookupItem) typed = (TypedLookupItem)element;
else if (element instanceof LookupElementDecorator) {
element = ((LookupElementDecorator)element).getDelegate();
if (element instanceof TypedLookupItem) typed = (TypedLookupItem)element;
}
return typed;
}
@Nullable
public static PsiType getQualifiedMemberReferenceType(@Nullable PsiType qualifierType, @NotNull final PsiMember member) {
final ClassCandidateInfo info = TypeConversionUtil.splitType(qualifierType, member);
@@ -729,13 +719,14 @@ public class JavaCompletionUtil {
}
private static LookupElement highlight(LookupElement decorator) {
return PrioritizedLookupElement.withGrouping(LookupElementDecorator.withRenderer(decorator, new LookupElementRenderer<LookupElementDecorator<LookupElement>>() {
@Override
public void renderElement(LookupElementDecorator<LookupElement> element, LookupElementPresentation presentation) {
element.getDelegate().renderElement(presentation);
presentation.setItemTextBold(true);
}
}), 1);
return PrioritizedLookupElement.withGrouping(
LookupElementDecorator.withRenderer(decorator, new LookupElementRenderer<LookupElementDecorator<LookupElement>>() {
@Override
public void renderElement(LookupElementDecorator<LookupElement> element, LookupElementPresentation presentation) {
element.getDelegate().renderElement(presentation);
presentation.setItemTextBold(true);
}
}), 1);
}
private static LookupItem<?> createLookupElement(CompletionElement completionElement, PsiType qualifierType) {
@@ -200,7 +200,7 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
final CompletionService service = CompletionService.getCompletionService();
new BasicExpressionCompletionContributor().fillCompletionVariants(parameters, service.createResultSet(parameters, new Consumer<LookupElement>() {
public void consume(final LookupElement lookupElement) {
final TypedLookupItem typed = JavaCompletionUtil.typedFrom(lookupElement);
final TypedLookupItem typed = lookupElement.as(TypedLookupItem.CLASS_CONDITION_KEY);
if (typed != null) {
final PsiType psiType = typed.getType();
if (psiType != null && type.isAssignableFrom(psiType)) {
@@ -18,7 +18,6 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.PsiTypeLookupItem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author peter
@@ -26,14 +25,11 @@ import org.jetbrains.annotations.Nullable;
public class PreferSimpleWeigher extends CompletionWeigher {
public Comparable weigh(@NotNull final LookupElement item, @NotNull final CompletionLocation location) {
if (location == null) {
return null;
}
final PsiTypeLookupItem lookupItem = PsiTypeLookupItem.from(item);
final PsiTypeLookupItem lookupItem = item.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (lookupItem != null) {
return -lookupItem.getBracketsCount();
}
if (CastingLookupElementDecorator.from(item) != null) {
if (item.as(CastingLookupElementDecorator.CLASS_CONDITION_KEY) != null) {
return -239;
}
return 0;
@@ -18,9 +18,9 @@ package com.intellij.codeInsight.lookup;
import com.intellij.codeInsight.completion.DefaultInsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.completion.JavaPsiClassReferenceElement;
import com.intellij.openapi.util.ClassConditionKey;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiClassReferenceType;
import com.intellij.psi.util.TypeConversionUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -29,6 +29,8 @@ import org.jetbrains.annotations.Nullable;
* @author peter
*/
public class PsiTypeLookupItem extends LookupItem {
public static final ClassConditionKey<PsiTypeLookupItem> CLASS_CONDITION_KEY = ClassConditionKey.create(PsiTypeLookupItem.class);
public PsiTypeLookupItem(Object o, @NotNull @NonNls String lookupString) {
super(o, lookupString);
}
@@ -130,16 +132,6 @@ public class PsiTypeLookupItem extends LookupItem {
}
return new LookupItem(type, type.getPresentableText());
}
public static @Nullable PsiTypeLookupItem from(LookupElement element) {
if (element instanceof PsiTypeLookupItem) return (PsiTypeLookupItem)element;
else if (element instanceof LookupElementDecorator) {
element = ((LookupElementDecorator)element).getDelegate();
if (element instanceof PsiTypeLookupItem) return (PsiTypeLookupItem)element;
}
return null;
}
@Override
public void renderElement(LookupElementPresentation presentation) {
@@ -15,6 +15,7 @@
*/
package com.intellij.codeInsight.lookup;
import com.intellij.openapi.util.ClassConditionKey;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.Nullable;
@@ -22,6 +23,8 @@ import org.jetbrains.annotations.Nullable;
* @author peter
*/
public interface TypedLookupItem {
ClassConditionKey<TypedLookupItem> CLASS_CONDITION_KEY = ClassConditionKey.create(TypedLookupItem.class);
@Nullable
PsiType getType();
}
@@ -19,7 +19,6 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementDecorator;
import com.intellij.openapi.util.ClassConditionKey;
import org.jetbrains.annotations.Nullable;
/**
* Use only when you want to control lookup sorting & preference in simple cases when you have control over ALL the items in lookup.
@@ -48,18 +47,12 @@ public class PrioritizedLookupElement<T extends LookupElement> extends LookupEle
}
public static LookupElement withPriority(LookupElement element, double priority) {
final PrioritizedLookupElement prioritized = PrioritizedLookupElement.from(element);
final PrioritizedLookupElement prioritized = element.as(CLASS_CONDITION_KEY);
return new PrioritizedLookupElement<LookupElement>(element, priority, prioritized == null ? 0 : prioritized.getGrouping());
}
public static LookupElement withGrouping(LookupElement element, int grouping) {
final PrioritizedLookupElement prioritized = PrioritizedLookupElement.from(element);
final PrioritizedLookupElement prioritized = element.as(CLASS_CONDITION_KEY);
return new PrioritizedLookupElement<LookupElement>(element, prioritized == null ? 0 : prioritized.getPriority(), grouping);
}
public static @Nullable PrioritizedLookupElement from(LookupElement element) {
if (element instanceof PrioritizedLookupElement) return (PrioritizedLookupElement)element;
if (element instanceof LookupElementDecorator) return from (((LookupElementDecorator)element).getDelegate());
return null;
}
}
@@ -19,7 +19,6 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupItem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author peter
@@ -27,15 +26,12 @@ import org.jetbrains.annotations.Nullable;
public class PriorityWeigher extends CompletionWeigher {
@Override
public Double weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
if (location == null) {
return null;
}
final PrioritizedLookupElement prioritized = PrioritizedLookupElement.from(element);
final PrioritizedLookupElement prioritized = element.as(PrioritizedLookupElement.CLASS_CONDITION_KEY);
if (prioritized != null) {
return -prioritized.getPriority();
}
final LookupItem item = LookupItem.from(element);
final LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY);
if (item != null) {
return -item.getPriority();
}
@@ -365,12 +365,4 @@ public class LookupItem<T> extends MutableLookupElement<T> implements Comparable
public boolean isCaseSensitive() {
return !Boolean.TRUE.equals(getAttribute(CASE_INSENSITIVE));
}
public static @Nullable LookupItem from(LookupElement lookupElement) {
while (lookupElement instanceof LookupElementDecorator) {
lookupElement = ((LookupElementDecorator)lookupElement).getDelegate();
}
if (lookupElement instanceof LookupItem) return (LookupItem)lookupElement;
return null;
}
}