use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method processSingleVar.
private static void processSingleVar(Project project, GrVariableDeclaration declaration, GrVariable variable) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrExpression assignment = factory.createExpressionFromText(variable.getName() + " = " + initializer.getText());
initializer.delete();
declaration = GroovyRefactoringUtil.addBlockIntoParent(declaration);
declaration.getParent().addAfter(assignment, declaration);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class IndexingMethodConversionPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression)) {
return false;
}
if (ErrorUtil.containsError(element)) {
return false;
}
final GrMethodCallExpression callExpression = (GrMethodCallExpression) element;
final GrArgumentList argList = callExpression.getArgumentList();
final GrExpression[] arguments = argList.getExpressionArguments();
final GrExpression invokedExpression = callExpression.getInvokedExpression();
if (!(invokedExpression instanceof GrReferenceExpression)) {
return false;
}
final GrReferenceExpression referenceExpression = (GrReferenceExpression) invokedExpression;
final GrExpression qualifier = referenceExpression.getQualifierExpression();
if (qualifier == null) {
return false;
}
final IElementType referenceType = referenceExpression.getDotTokenType();
if (!GroovyTokenTypes.mDOT.equals(referenceType)) {
return false;
}
final String methodName = referenceExpression.getReferenceName();
if ("getAt".equals(methodName)) {
return arguments.length == 1;
}
if ("get".equals(methodName)) {
final PsiType qualifierType = qualifier.getType();
if (!isMap(qualifierType)) {
return false;
}
return arguments.length == 1;
} else if ("setAt".equals(methodName)) {
return arguments.length == 2;
} else if ("put".equals(methodName)) {
final PsiType qualifierType = qualifier.getType();
if (!isMap(qualifierType)) {
return false;
}
return arguments.length == 2;
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class InitialInfo method inferOutputType.
@Nullable
private PsiType inferOutputType(VariableInfo[] outputInfos, GrStatement[] statements, ArrayList<GrStatement> returnStatements, boolean hasReturnValue, StringPartInfo stringPartInfo) {
if (stringPartInfo != null) {
return stringPartInfo.getLiteral().getType();
}
PsiType outputType = PsiType.VOID;
if (outputInfos.length > 0) {
if (outputInfos.length == 1) {
outputType = outputInfos[0].getType();
} else {
outputType = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(CommonClassNames.JAVA_UTIL_LIST, getContext());
}
} else if (ExtractUtil.isSingleExpression(statements)) {
outputType = ((GrExpression) statements[0]).getType();
} else if (hasReturnValue) {
assert !returnStatements.isEmpty();
List<PsiType> types = new ArrayList<>(returnStatements.size());
for (GrStatement statement : returnStatements) {
if (statement instanceof GrReturnStatement) {
GrExpression returnValue = ((GrReturnStatement) statement).getReturnValue();
if (returnValue != null) {
types.add(returnValue.getType());
}
} else if (statement instanceof GrExpression) {
types.add(((GrExpression) statement).getType());
}
}
outputType = TypesUtil.getLeastUpperBoundNullable(types, getContext().getManager());
}
return outputType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method renameParameterOccurrences.
private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
GrOpenBlock block = method.getBlock();
if (block == null)
return;
GrStatement[] statements = block.getStatements();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
for (ParameterInfo info : helper.getParameterInfos()) {
final String oldName = info.getOriginalName();
final String newName = info.getName();
final ArrayList<GrExpression> result = new ArrayList<>();
if (!oldName.equals(newName)) {
for (final GrStatement statement : statements) {
statement.accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(final PsiElement element) {
super.visitElement(element);
if (element instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) element;
if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
result.add(expr);
}
}
}
});
for (GrExpression expr : result) {
expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable DataContext dataContext) {
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
final SelectionModel model = editor.getSelectionModel();
if (model.hasSelection()) {
invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd());
} else {
final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, editor.getCaretModel().getOffset(), true);
final Pass<GrExpression> callback = new Callback(project, editor, file);
if (expressions.size() == 1) {
callback.pass(expressions.get(0));
} else if (expressions.isEmpty()) {
model.selectLineAtCaret();
invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd());
} else {
IntroduceTargetChooser.showChooser(editor, expressions, callback, GrIntroduceHandlerBase.GR_EXPRESSION_RENDERER);
}
}
}
Aggregations