use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class GroovySmartCompletionContributor method generateInheritorVariants.
static void generateInheritorVariants(final CompletionParameters parameters, PrefixMatcher matcher, final Consumer<LookupElement> consumer) {
final PsiElement place = parameters.getPosition();
final GrExpression expression = PsiTreeUtil.getParentOfType(place, GrExpression.class);
if (expression == null)
return;
GrExpression placeToInferType = expression;
if (expression.getParent() instanceof GrApplicationStatement && expression.getParent().getParent() instanceof GrAssignmentExpression) {
placeToInferType = (GrExpression) expression.getParent();
}
for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(placeToInferType)) {
if (type instanceof PsiArrayType) {
final PsiType _type = GenericsUtil.eliminateWildcards(type);
final PsiTypeLookupItem item = PsiTypeLookupItem.createLookupItem(_type, place, PsiTypeLookupItem.isDiamond(_type), ChooseTypeExpression.IMPORT_FIXER).setShowPackage();
if (item.getObject() instanceof PsiClass) {
item.setInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
GroovyCompletionUtil.addImportForItem(context.getFile(), context.getStartOffset(), item);
}
});
}
consumer.consume(item);
}
}
final List<PsiClassType> expectedClassTypes = new SmartList<>();
for (PsiType psiType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(placeToInferType)) {
if (psiType instanceof PsiClassType) {
PsiType type = GenericsUtil.eliminateWildcards(JavaCompletionUtil.originalize(psiType));
final PsiClassType classType = (PsiClassType) type;
if (classType.resolve() != null) {
expectedClassTypes.add(classType);
}
}
}
final PsiType diamond = inferDiamond(place);
JavaInheritorsGetter.processInheritors(parameters, expectedClassTypes, matcher, type -> {
final LookupElement element = addExpectedType(type, place, parameters, diamond);
if (element != null) {
consumer.consume(element);
}
});
}
use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class ConstructorInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item) {
@SuppressWarnings({ "unchecked" }) final LookupElement delegate = item.getDelegate();
PsiClass psiClass = (PsiClass) item.getObject();
boolean isAbstract = psiClass.hasModifierProperty(PsiModifier.ABSTRACT);
if (Lookup.REPLACE_SELECT_CHAR == context.getCompletionChar() && context.getOffsetMap().containsOffset(PARAM_LIST_START)) {
final int plStart = context.getOffset(PARAM_LIST_START);
final int plEnd = context.getOffset(PARAM_LIST_END);
if (plStart >= 0 && plEnd >= 0) {
context.getDocument().deleteString(plStart, plEnd);
}
}
context.commitDocument();
OffsetKey insideRef = context.trackOffset(context.getTailOffset(), false);
final PsiElement position = SmartCompletionDecorator.getPosition(context, delegate);
if (position == null)
return;
final PsiExpression enclosing = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true);
final PsiAnonymousClass anonymousClass = PsiTreeUtil.getParentOfType(position, PsiAnonymousClass.class);
final boolean inAnonymous = anonymousClass != null && anonymousClass.getParent() == enclosing;
if (delegate instanceof PsiTypeLookupItem) {
if (context.getDocument().getTextLength() > context.getTailOffset() && context.getDocument().getCharsSequence().charAt(context.getTailOffset()) == '<') {
PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getTailOffset(), PsiJavaCodeReferenceElement.class, false);
if (ref != null) {
PsiReferenceParameterList parameterList = ref.getParameterList();
if (parameterList != null && context.getTailOffset() == parameterList.getTextRange().getStartOffset()) {
context.getDocument().deleteString(parameterList.getTextRange().getStartOffset(), parameterList.getTextRange().getEndOffset());
context.commitDocument();
}
}
}
delegate.handleInsert(context);
PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider());
}
if (item.getDelegate() instanceof JavaPsiClassReferenceElement) {
PsiTypeLookupItem.addImportForItem(context, psiClass);
}
insertParentheses(context, delegate, psiClass, !inAnonymous && isAbstract);
if (inAnonymous) {
return;
}
if (mySmart) {
FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.AFTER_NEW);
}
if (isAbstract) {
PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider());
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
final int offset = context.getTailOffset();
document.insertString(offset, " {}");
OffsetKey insideBraces = context.trackOffset(offset + 2, true);
final PsiFile file = context.getFile();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
reformatEnclosingExpressionListAtOffset(file, offset);
if (promptTypeOrConstructorArgs(context, delegate, insideRef, insideBraces))
return;
editor.getCaretModel().moveToOffset(context.getOffset(insideBraces));
context.setLaterRunnable(generateAnonymousBody(editor, file));
} else {
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
final PsiNewExpression newExpression = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiNewExpression.class, false);
if (newExpression != null) {
final PsiJavaCodeReferenceElement classReference = newExpression.getClassOrAnonymousClassReference();
if (classReference != null) {
CodeStyleManager.getInstance(context.getProject()).reformat(classReference);
}
}
if (mySmart) {
FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.AFTER_NEW);
}
promptTypeOrConstructorArgs(context, delegate, insideRef, null);
}
}
use of com.intellij.codeInsight.lookup.PsiTypeLookupItem in project intellij-community by JetBrains.
the class TypeArgumentCompletionProvider method fillAllArgs.
private void fillAllArgs(CompletionResultSet resultSet, PsiElement context, ExpectedTypeInfo info, List<PsiType> expectedArgs, PsiTypeParameterListOwner paramOwner) {
List<PsiTypeLookupItem> typeItems = ContainerUtil.map(expectedArgs, arg -> PsiTypeLookupItem.createLookupItem(arg, context));
TailType globalTail = mySmart ? info.getTailType() : TailType.NONE;
TypeArgsLookupElement element = new TypeArgsLookupElement(typeItems, globalTail, hasParameters(paramOwner, context));
element.registerSingleClass(mySession);
resultSet.addElement(element);
}
Aggregations