use of org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint in project intellij-community by JetBrains.
the class GppExpectedTypesContributor method addExpectedConstructorParameters.
private static List<TypeConstraint> addExpectedConstructorParameters(GrListOrMap list, GrExpression[] args, GrExpression arg) {
PsiType[] argTypes = ContainerUtil.map2Array(args, PsiType.class, (NullableFunction<GrExpression, PsiType>) grExpression -> grExpression.getType());
final ArrayList<TypeConstraint> result = new ArrayList<>();
for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
if (type instanceof PsiClassType) {
for (GroovyResolveResult resolveResult : PsiUtil.getConstructorCandidates((PsiClassType) type, argTypes, list)) {
final PsiElement method = resolveResult.getElement();
if (method instanceof PsiMethod && ((PsiMethod) method).isConstructor()) {
final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, list, false, true, GrNamedArgument.EMPTY_ARRAY, args, GrClosableBlock.EMPTY_ARRAY);
if (map != null) {
final Pair<PsiParameter, PsiType> pair = map.get(arg);
if (pair != null) {
result.add(SubtypeConstraint.create(pair.second));
}
}
}
}
}
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint in project intellij-community by JetBrains.
the class CreateClassFix method generateConstructor.
private static void generateConstructor(@NotNull PsiElement refElement, @NotNull String name, @NotNull PsiType[] argTypes, @NotNull GrTypeDefinition targetClass, @NotNull Project project) {
WriteAction.run(() -> {
ChooseTypeExpression[] paramTypesExpressions = new ChooseTypeExpression[argTypes.length];
String[] paramTypes = new String[argTypes.length];
String[] paramNames = new String[argTypes.length];
for (int i = 0; i < argTypes.length; i++) {
PsiType argType = argTypes[i];
if (argType == null)
argType = TypesUtil.getJavaLangObject(refElement);
paramTypes[i] = "Object";
paramNames[i] = "o" + i;
TypeConstraint[] constraints = { SupertypeConstraint.create(argType) };
paramTypesExpressions[i] = new ChooseTypeExpression(constraints, refElement.getManager(), targetClass.getResolveScope());
}
GrMethod method = GroovyPsiElementFactory.getInstance(project).createConstructorFromText(name, paramTypes, paramNames, "{\n}");
method = (GrMethod) targetClass.addBefore(method, null);
final PsiElement context = PsiTreeUtil.getParentOfType(refElement, PsiMethod.class, PsiClass.class, PsiFile.class);
IntentionUtils.createTemplateForMethod(argTypes, paramTypesExpressions, method, targetClass, TypeConstraint.EMPTY_ARRAY, true, context);
});
}
use of org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint in project intellij-community by JetBrains.
the class GroovyCreateFieldFromUsageHelper method setupTemplateImpl.
@Override
public Template setupTemplateImpl(PsiField f, Object expectedTypes, PsiClass targetClass, Editor editor, PsiElement context, boolean createConstantField, PsiSubstitutor substitutor) {
GrVariableDeclaration fieldDecl = (GrVariableDeclaration) f.getParent();
GrField field = (GrField) fieldDecl.getVariables()[0];
TemplateBuilderImpl builder = new TemplateBuilderImpl(fieldDecl);
Project project = context.getProject();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (expectedTypes instanceof TypeConstraint[]) {
GrTypeElement typeElement = fieldDecl.getTypeElementGroovy();
assert typeElement != null;
ChooseTypeExpression expr = new ChooseTypeExpression((TypeConstraint[]) expectedTypes, PsiManager.getInstance(project), typeElement.getResolveScope());
builder.replaceElement(typeElement, expr);
} else if (expectedTypes instanceof ExpectedTypeInfo[]) {
new GuessTypeParameters(factory).setupTypeElement(field.getTypeElement(), (ExpectedTypeInfo[]) expectedTypes, substitutor, builder, context, targetClass);
}
if (createConstantField) {
field.setInitializerGroovy(factory.createExpressionFromText("0", null));
builder.replaceElement(field.getInitializerGroovy(), new EmptyExpression());
}
fieldDecl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(fieldDecl);
Template template = builder.buildTemplate();
TextRange range = fieldDecl.getTextRange();
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
if (expectedTypes instanceof ExpectedTypeInfo[]) {
if (((ExpectedTypeInfo[]) expectedTypes).length > 1)
template.setToShortenLongNames(false);
}
return template;
}
use of org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint in project intellij-community by JetBrains.
the class CreateFieldFromConstructorLabelFix method calculateTypeConstrains.
private TypeConstraint[] calculateTypeConstrains() {
final GrExpression expression = myNamedArgument.getExpression();
PsiType type = null;
if (expression != null) {
type = expression.getType();
}
if (type != null) {
return new TypeConstraint[] { SupertypeConstraint.create(type, type) };
} else {
return TypeConstraint.EMPTY_ARRAY;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint in project intellij-community by JetBrains.
the class CreateLocalVariableFromUsageFix method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiFile file = element.getContainingFile();
PsiClassType type = JavaPsiFacade.getInstance(project).getElementFactory().createTypeByFQClassName("Object", GlobalSearchScope.allScope(project));
GrVariableDeclaration decl = GroovyPsiElementFactory.getInstance(project).createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, myRefExpression.getReferenceName());
int offset = myRefExpression.getTextRange().getStartOffset();
GrStatement anchor = findAnchor(file, offset);
TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints(myRefExpression);
if (myRefExpression.equals(anchor)) {
decl = myRefExpression.replaceWithStatement(decl);
} else {
decl = myOwner.addVariableDeclarationBefore(decl, anchor);
}
GrTypeElement typeElement = decl.getTypeElementGroovy();
assert typeElement != null;
ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), typeElement.getResolveScope());
TemplateBuilderImpl builder = new TemplateBuilderImpl(decl);
builder.replaceElement(typeElement, expr);
decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
Template template = builder.buildTemplate();
Editor newEditor = positionCursor(project, myOwner.getContainingFile(), decl);
TextRange range = decl.getTextRange();
newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(newEditor, template);
}
Aggregations