IDEA-76263 toar live template fails to work with vararg parameter

This commit is contained in:
peter
2011-11-02 19:04:45 +01:00
parent 49e648a827
commit 746f1380a6
4 changed files with 44 additions and 25 deletions
@@ -53,7 +53,6 @@ class ConstructorInsertHandler implements InsertHandler<LookupElementDecorator<L
final boolean inAnonymous = anonymousClass != null && anonymousClass.getParent() == enclosing;
PsiClass psiClass = (PsiClass)item.getObject();
boolean withTail = item.getUserData(LookupItem.BRACKETS_COUNT_ATTR) == null && !inAnonymous;
boolean isAbstract = psiClass.hasModifierProperty(PsiModifier.ABSTRACT);
if (Lookup.REPLACE_SELECT_CHAR == context.getCompletionChar()) {
@@ -74,13 +73,13 @@ class ConstructorInsertHandler implements InsertHandler<LookupElementDecorator<L
PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider());
}
insertParentheses(context, delegate, psiClass, withTail && isAbstract);
insertParentheses(context, delegate, psiClass, !inAnonymous && isAbstract);
if (item.getDelegate() instanceof JavaPsiClassReferenceElement) {
DefaultInsertHandler.addImportForItem(context, delegate);
}
if (!withTail) {
if (inAnonymous) {
return;
}
@@ -37,11 +37,22 @@ import org.jetbrains.annotations.Nullable;
public class PsiTypeLookupItem extends LookupItem {
public static final ClassConditionKey<PsiTypeLookupItem> CLASS_CONDITION_KEY = ClassConditionKey.create(PsiTypeLookupItem.class);
private final boolean myDiamond;
private final int myBracketsCount;
private boolean myIndicateAnonymous;
private PsiTypeLookupItem(Object o, @NotNull @NonNls String lookupString, boolean diamond) {
private PsiTypeLookupItem(Object o, @NotNull @NonNls String lookupString, boolean diamond, int bracketsCount) {
super(o, lookupString);
myDiamond = diamond;
myBracketsCount = bracketsCount;
}
public PsiType getPsiType() {
Object object = getObject();
PsiType type = object instanceof PsiType ? (PsiType)object : JavaPsiFacade.getElementFactory(((PsiClass) object).getProject()).createType((PsiClass)object);
for (int i = 0; i < getBracketsCount(); i++) {
type = new PsiArrayType(type);
}
return type;
}
@@ -119,8 +130,7 @@ public class PsiTypeLookupItem extends LookupItem {
}
public int getBracketsCount() {
final Integer integer = (Integer)getUserData(BRACKETS_COUNT_ATTR);
return integer == null ? 0 : integer;
return myBracketsCount;
}
public static PsiTypeLookupItem createLookupItem(@NotNull PsiType type, @Nullable PsiElement context) {
@@ -131,18 +141,17 @@ public class PsiTypeLookupItem extends LookupItem {
dim++;
}
PsiTypeLookupItem item = doCreateItem(type, context);
PsiTypeLookupItem item = doCreateItem(type, context, dim);
if (dim > 0) {
item.setAttribute(TAIL_TEXT_ATTR, " " + StringUtil.repeat("[]", dim));
item.setAttribute(TAIL_TEXT_SMALL_ATTR, "");
item.putUserData(BRACKETS_COUNT_ATTR, dim);
}
item.setAttribute(TYPE, original);
return item;
}
private static PsiTypeLookupItem doCreateItem(final PsiType type, PsiElement context) {
private static PsiTypeLookupItem doCreateItem(final PsiType type, PsiElement context, int bracketsCount) {
if (type instanceof PsiClassType) {
PsiClassType.ClassResolveResult classResolveResult = ((PsiClassType)type).resolveGenerics();
final PsiClass psiClass = classResolveResult.getElement();
@@ -169,13 +178,13 @@ public class PsiTypeLookupItem extends LookupItem {
}
}
PsiTypeLookupItem item = new PsiTypeLookupItem(psiClass, lookupString, diamond);
PsiTypeLookupItem item = new PsiTypeLookupItem(psiClass, lookupString, diamond, bracketsCount);
item.setAttribute(SUBSTITUTOR, substitutor);
return item;
}
}
return new PsiTypeLookupItem(type, type.getPresentableText(), false);
return new PsiTypeLookupItem(type, type.getPresentableText(), false, bracketsCount);
}
@NotNull
@@ -16,15 +16,18 @@
package com.intellij.codeInsight.template.macro;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.PsiTypeLookupItem;
import com.intellij.codeInsight.template.*;
import com.intellij.psi.PsiArrayType;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiType;
import com.intellij.util.containers.CollectionFactory;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class ComponentTypeOfMacro extends Macro {
public String getName() {
return "componentTypeOf";
@@ -39,12 +42,14 @@ public class ComponentTypeOfMacro extends Macro {
LookupElement[] lookupItems = params[0].calculateLookupItems(context);
if (lookupItems == null) return null;
List<LookupElement> result = CollectionFactory.arrayList();
for (LookupElement element : lookupItems) {
if (element instanceof LookupItem) {
final LookupItem item = (LookupItem)element;
Integer bracketsCount = (Integer)item.getUserData(LookupItem.BRACKETS_COUNT_ATTR);
if (bracketsCount == null) return null;
item.putUserData(LookupItem.BRACKETS_COUNT_ATTR, new Integer(bracketsCount.intValue() - 1));
PsiTypeLookupItem lookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (lookupItem != null) {
PsiType psiType = lookupItem.getPsiType();
if (psiType instanceof PsiArrayType) {
result.add(PsiTypeLookupItem.createLookupItem(((PsiArrayType)psiType).getComponentType(), null));
}
}
}
@@ -65,17 +70,24 @@ public class ComponentTypeOfMacro extends Macro {
}
PsiExpression expr = MacroUtil.resultToPsiExpression(result, context);
PsiType type;
if (expr == null) {
type = MacroUtil.resultToPsiType(result, context);
}
else{
type = expr.getType();
}
PsiType type = expr == null ? MacroUtil.resultToPsiType(result, context) : expr.getType();
if (type instanceof PsiArrayType) {
return new PsiTypeResult(((PsiArrayType) type).getComponentType(), context.getProject());
}
LookupElement[] elements = params[0].calculateLookupItems(context);
if (elements != null) {
for (LookupElement element : elements) {
PsiTypeLookupItem typeLookupItem = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
if (typeLookupItem != null) {
PsiType psiType = typeLookupItem.getPsiType();
if (psiType instanceof PsiArrayType) {
return new PsiTypeResult(((PsiArrayType)psiType).getComponentType(), context.getProject());
}
}
}
}
return new PsiElementResult(null);
}
@@ -51,7 +51,6 @@ public class LookupItem<T> extends MutableLookupElement<T> implements Comparable
public static final Object TAIL_TEXT_SMALL_ATTR = Key.create("tailTextSmall");
public static final Key<Object> FORCE_SHOW_SIGNATURE_ATTR = Key.create("forceShowSignature");
public static final Key<Object> BRACKETS_COUNT_ATTR = Key.create("BRACKETS_COUNT_ATTR");
public static final Object FORCE_QUALIFY = Key.create("FORCE_QUALIFY");
public static final Object SUBSTITUTOR = Key.create("SUBSTITUTOR");
public static final Object TYPE = Key.create("TYPE");