use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class ControlFlowBuilder method buildFlowForClosure.
private void buildFlowForClosure(final GrClosableBlock closure) {
for (GrParameter parameter : closure.getAllParameters()) {
if (myPolicy.isVariableInitialized(parameter)) {
addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
}
}
addNode(new ReadWriteVariableInstruction("owner", closure.getLBrace(), ReadWriteVariableInstruction.WRITE));
PsiElement child = closure.getFirstChild();
while (child != null) {
if (child instanceof GroovyPsiElement) {
((GroovyPsiElement) child).accept(this);
}
child = child.getNextSibling();
}
final GrStatement[] statements = closure.getStatements();
if (statements.length > 0) {
handlePossibleReturn(statements[statements.length - 1]);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyClassNameInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, JavaPsiClassReferenceElement item) {
PsiFile file = context.getFile();
Editor editor = context.getEditor();
int endOffset = editor.getCaretModel().getOffset();
if (PsiTreeUtil.findElementOfClassAtOffset(file, endOffset - 1, GrImportStatement.class, false) != null || !(file instanceof GroovyFileBase)) {
AllClassesGetter.INSERT_FQN.handleInsert(context, item);
return;
}
PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
PsiElement position = file.findElementAt(endOffset - 1);
boolean parens = shouldInsertParentheses(position);
final PsiClass psiClass = item.getObject();
if (isInVariable(position) || GroovyCompletionUtil.isInPossibleClosureParameter(position)) {
Project project = context.getProject();
String qname = psiClass.getQualifiedName();
String shortName = psiClass.getName();
if (qname == null)
return;
PsiClass aClass = JavaPsiFacade.getInstance(project).getResolveHelper().resolveReferencedClass(shortName, position);
if (aClass == null) {
((GroovyFileBase) file).addImportForClass(psiClass);
return;
} else if (aClass == CompletionUtil.getOriginalOrSelf(psiClass)) {
return;
}
}
AllClassesGetter.TRY_SHORTENING.handleInsert(context, item);
if (parens && context.getCompletionChar() != '[') {
int identifierEnd = context.getTailOffset();
GroovyPsiElement place = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GroovyPsiElement.class, false);
JavaCompletionUtil.insertParentheses(context, item, false, place != null && GroovyCompletionUtil.hasConstructorParameters(psiClass, place));
if (context.getCompletionChar() == '<' || psiClass.hasTypeParameters() && context.getCompletionChar() != '(') {
context.setAddCompletionChar(false);
JavaCompletionUtil.promptTypeArgs(context, identifierEnd);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyParameterInfoHandler method findAnchorElement.
@Nullable
private static GroovyPsiElement findAnchorElement(int offset, PsiFile file) {
PsiElement element = file.findElementAt(offset);
if (element == null)
return null;
GroovyPsiElement argList = PsiTreeUtil.getParentOfType(element, GrArgumentList.class);
if (argList != null)
return argList;
final GrCall call = PsiTreeUtil.getParentOfType(element, GrCall.class);
if (call != null) {
argList = call.getArgumentList();
if (argList != null && argList.getTextRange().contains(element.getTextRange().getStartOffset()))
return argList;
} else {
offset = CharArrayUtil.shiftBackward(file.getText(), offset, "\n\t ");
if (offset <= 0)
return null;
element = file.findElementAt(offset);
if (element != null && element.getParent() instanceof GrReferenceExpression)
return (GroovyPsiElement) element.getParent();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement 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.GroovyPsiElement in project intellij-community by JetBrains.
the class GroovyParameterInfoHandler method showParameterInfo.
@Override
@SuppressWarnings("unchecked")
public void showParameterInfo(@NotNull GroovyPsiElement place, @NotNull CreateParameterInfoContext context) {
GroovyResolveResult[] variants = ResolveUtil.getCallVariants(place);
final List elementToShow = new ArrayList();
final PsiElement parent = place.getParent();
if (parent instanceof GrMethodCall) {
final GrExpression invoked = ((GrMethodCall) parent).getInvokedExpression();
if (isPropertyOrVariableInvoked(invoked)) {
final PsiType type = invoked.getType();
if (type instanceof GrClosureType) {
addSignatureVariant(elementToShow, (GrClosureType) type);
} else if (type != null) {
addMethodAndClosureVariants(elementToShow, ResolveUtil.getMethodCandidates(type, "call", invoked, PsiUtil.getArgumentTypes(place, true)));
}
} else {
addMethodAndClosureVariants(elementToShow, variants);
}
} else {
elementToShow.addAll(Arrays.asList(variants));
}
filterOutReflectedMethods(elementToShow);
context.setItemsToShow(ArrayUtil.toObjectArray(elementToShow));
context.showHint(place, place.getTextRange().getStartOffset(), this);
}
Aggregations