use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GroovyParameterInfoHandler method updateUI.
@Override
public void updateUI(Object o, @NotNull ParameterInfoUIContext context) {
CodeInsightSettings settings = CodeInsightSettings.getInstance();
if (o == null)
return;
Object element;
if (o instanceof GroovyResolveResult) {
element = ((GroovyResolveResult) o).getElement();
if (element == null || !((PsiElement) element).isValid()) {
context.setUIComponentEnabled(false);
return;
}
} else if (o instanceof GrClosureSignature) {
if (!((GrClosureSignature) o).isValid()) {
context.setUIComponentEnabled(false);
return;
}
element = o;
} else {
return;
}
int highlightStartOffset = -1;
int highlightEndOffset = -1;
final int currentParameter = context.getCurrentParameterIndex();
StringBuilder buffer = new StringBuilder();
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;
if (method instanceof GrReflectedMethod)
method = ((GrReflectedMethod) method).getBaseMethod();
if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
if (!method.isConstructor()) {
PsiType returnType = PsiUtil.getSmartReturnType(method);
if (returnType != null) {
buffer.append(returnType.getPresentableText());
buffer.append(' ');
}
}
buffer.append(method.getName());
buffer.append('(');
}
PsiParameter[] params = method.getParameterList().getParameters();
params = updateConstructorParams(method, params, context.getParameterOwner());
int numParams = params.length;
if (numParams > 0) {
LOG.assertTrue(o instanceof GroovyResolveResult, o.getClass());
final PsiSubstitutor substitutor = ((GroovyResolveResult) o).getSubstitutor();
for (int j = 0; j < numParams; j++) {
PsiParameter param = params[j];
int startOffset = buffer.length();
appendParameterText(param, substitutor, buffer);
int endOffset = buffer.length();
if (j < numParams - 1) {
buffer.append(", ");
}
if (context.isUIComponentEnabled() && (j == currentParameter || (j == numParams - 1 && param.isVarArgs() && currentParameter >= numParams))) {
highlightStartOffset = startOffset;
highlightEndOffset = endOffset;
}
}
} else {
buffer.append("no parameters");
}
if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
buffer.append(")");
}
} else if (element instanceof PsiClass) {
buffer.append("no parameters");
} else if (element instanceof GrClosureSignature) {
GrClosureParameter[] parameters = ((GrClosureSignature) element).getParameters();
if (parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
if (i > 0)
buffer.append(", ");
int startOffset = buffer.length();
final PsiType psiType = parameters[i].getType();
if (psiType == null) {
buffer.append("def");
} else {
buffer.append(psiType.getPresentableText());
}
buffer.append(' ').append(parameters[i].getName() != null ? parameters[i].getName() : "<unknown>");
int endOffset = buffer.length();
if (context.isUIComponentEnabled() && (i == currentParameter || (i == parameters.length - 1 && ((GrClosureSignature) element).isVarargs() && currentParameter >= parameters.length))) {
highlightStartOffset = startOffset;
highlightEndOffset = endOffset;
}
final GrExpression initializer = parameters[i].getDefaultInitializer();
if (initializer != null) {
buffer.append(" = ").append(initializer.getText());
}
}
} else {
buffer.append("no parameters");
}
}
final boolean isDeprecated = o instanceof PsiDocCommentOwner && ((PsiDocCommentOwner) o).isDeprecated();
context.setupUIComponentPresentation(buffer.toString(), highlightStartOffset, highlightEndOffset, !context.isUIComponentEnabled(), isDeprecated, false, context.getDefaultParameterColor());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GroovyParameterInfoHandler method updateParameterInfo.
@Override
public void updateParameterInfo(@NotNull GroovyPsiElement place, @NotNull UpdateParameterInfoContext context) {
final PsiElement parameterOwner = context.getParameterOwner();
if (parameterOwner != place) {
context.removeHint();
return;
}
int offset = context.getEditor().getCaretModel().getOffset();
offset = CharArrayUtil.shiftForward(context.getEditor().getDocument().getText(), offset, " \t\n");
final int currIndex = getCurrentParameterIndex(place, offset);
context.setCurrentParameter(currIndex);
final Object[] objects = context.getObjectsToView();
Outer: for (int i = 0; i < objects.length; i++) {
PsiType[] parameterTypes = null;
PsiType[] argTypes = null;
PsiSubstitutor substitutor = null;
if (objects[i] instanceof GroovyResolveResult) {
final GroovyResolveResult resolveResult = (GroovyResolveResult) objects[i];
PsiNamedElement namedElement = (PsiNamedElement) resolveResult.getElement();
if (namedElement instanceof GrReflectedMethod)
namedElement = ((GrReflectedMethod) namedElement).getBaseMethod();
substitutor = resolveResult.getSubstitutor();
assert namedElement != null;
if (!namedElement.isValid()) {
context.setUIComponentEnabled(i, false);
continue Outer;
}
if (namedElement instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) namedElement;
PsiParameter[] parameters = method.getParameterList().getParameters();
parameters = updateConstructorParams(method, parameters, context.getParameterOwner());
parameterTypes = PsiType.createArray(parameters.length);
for (int j = 0; j < parameters.length; j++) {
parameterTypes[j] = parameters[j].getType();
}
argTypes = PsiUtil.getArgumentTypes(place, false);
}
if (argTypes == null)
continue;
} else if (objects[i] instanceof GrClosureSignature) {
final GrClosureSignature signature = (GrClosureSignature) objects[i];
argTypes = PsiUtil.getArgumentTypes(place, false);
parameterTypes = PsiType.createArray(signature.getParameterCount());
int j = 0;
for (GrClosureParameter parameter : signature.getParameters()) {
parameterTypes[j++] = parameter.getType();
}
} else {
continue Outer;
}
assert argTypes != null;
if (argTypes.length > currIndex) {
if (parameterTypes.length <= currIndex) {
context.setUIComponentEnabled(i, false);
continue;
} else {
for (int j = 0; j < currIndex; j++) {
PsiType argType = argTypes[j];
final PsiType paramType = substitutor != null ? substitutor.substitute(parameterTypes[j]) : parameterTypes[j];
if (!TypesUtil.isAssignableByMethodCallConversion(paramType, argType, place)) {
context.setUIComponentEnabled(i, false);
break Outer;
}
}
}
}
context.setUIComponentEnabled(i, true);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GrClosureSignatureUtil method getSignatureApplicabilities.
private static List<Trinity<GrClosureSignature, ArgInfo<PsiType>[], ApplicabilityResult>> getSignatureApplicabilities(@NotNull GrSignature signature, @NotNull final PsiType[] args, @NotNull final PsiElement context) {
final List<Trinity<GrClosureSignature, ArgInfo<PsiType>[], ApplicabilityResult>> results = new ArrayList<>();
signature.accept(new GrRecursiveSignatureVisitor() {
@Override
public void visitClosureSignature(GrClosureSignature signature) {
ArgInfo<PsiType>[] map = mapArgTypesToParameters(signature, args, context, false);
if (map != null) {
results.add(new Trinity<>(signature, map, isSignatureApplicableInner(map, signature)));
return;
}
// check for the case foo([1, 2, 3]) if foo(int, int, int)
if (args.length == 1 && PsiUtil.isInMethodCallContext(context)) {
final GrClosureParameter[] parameters = signature.getParameters();
if (parameters.length == 1 && parameters[0].getType() instanceof PsiArrayType) {
return;
}
PsiType arg = args[0];
if (arg instanceof GrTupleType) {
PsiType[] _args = ((GrTupleType) arg).getComponentTypes();
map = mapArgTypesToParameters(signature, _args, context, false);
if (map != null) {
results.add(new Trinity<>(signature, map, isSignatureApplicableInner(map, signature)));
}
}
}
}
});
return results;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GrClosureType method create.
public static GrClosureType create(@NotNull PsiMethod method, @NotNull PsiSubstitutor substitutor) {
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor);
final GlobalSearchScope scope = GlobalSearchScope.allScope(method.getProject());
final JavaPsiFacade facade = JavaPsiFacade.getInstance(method.getProject());
return create(signature, scope, facade, LanguageLevel.JDK_1_5, true);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method processExternalUsage.
private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
final PsiElement element = usage.getElement();
GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (callExpression == null) {
final PsiElement parent = element.getParent();
if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
}
}
if (callExpression == null)
return;
//check for x.getFoo()(args)
if (callExpression instanceof GrMethodCall) {
final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
if (invoked instanceof GrReferenceExpression) {
final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
final PsiElement resolved = result.getElement();
if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
PsiElement actualCallExpression = callExpression.getParent();
if (actualCallExpression instanceof GrCall) {
callExpression = (GrCall) actualCallExpression;
}
}
}
}
GrArgumentList argList = callExpression.getArgumentList();
LOG.assertTrue(argList != null);
GrExpression[] oldArgs = argList.getExpressionArguments();
GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
if (signature == null)
signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
} else {
PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
LOG.assertTrue(initializer instanceof GrExpression);
GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
if (newArg == null) {
final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
}
new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
ChangeContextUtil.clearContextInfo(initializer);
//newarg can be replaced by OldReferenceResolve
if (newArg.isValid()) {
JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
}
}
if (actualArgs == null) {
GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
} else {
GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
}
if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
argList.replace(emptyArgList);
}
}
Aggregations