use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method replaceAllOccurrencesWithExpression.
private static void replaceAllOccurrencesWithExpression(GrMethod method, GrCallExpression call, GrExpression oldExpression, GrParameter parameter) {
Collection<PsiReference> refs = ReferencesSearch.search(parameter, new LocalSearchScope(method), false).findAll();
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(call.getProject());
GrExpression expression = elementFactory.createExpressionFromText(oldExpression.getText());
if (GroovyRefactoringUtil.hasSideEffect(expression) && refs.size() > 1 || !hasUnresolvableWriteAccess(refs, oldExpression)) {
final String oldName = parameter.getName();
final String newName = InlineMethodConflictSolver.suggestNewName(oldName, method, call);
expression = elementFactory.createExpressionFromText(newName);
final GrOpenBlock body = method.getBlock();
final GrStatement[] statements = body.getStatements();
GrStatement anchor = null;
if (statements.length > 0) {
anchor = statements[0];
}
body.addStatementBefore(elementFactory.createStatementFromText(createVariableDefinitionText(parameter, oldExpression, newName)), anchor);
}
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (element instanceof GrReferenceExpression) {
((GrReferenceExpression) element).replaceWithExpression(expression, true);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class ExtractUtil method createResultStatement.
@NotNull
private static GrStatement[] createResultStatement(ExtractInfoHelper helper) {
VariableInfo[] outputVars = helper.getOutputVariableInfos();
PsiType type = helper.getOutputType();
GrStatement[] statements = helper.getStatements();
GrMethodCallExpression callExpression = createMethodCall(helper);
if ((outputVars.length == 0 || PsiType.VOID.equals(type)) && !helper.hasReturnValue())
return new GrStatement[] { callExpression };
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
if (helper.hasReturnValue()) {
return new GrStatement[] { factory.createStatementFromText("return " + callExpression.getText()) };
}
LOG.assertTrue(outputVars.length > 0);
final List<VariableInfo> mustAdd = mustAddVariableDeclaration(statements, outputVars);
if (mustAdd.isEmpty()) {
return new GrStatement[] { createAssignment(outputVars, callExpression, helper.getProject()) };
} else if (mustAdd.size() == outputVars.length && outputVars.length == 1) {
return new GrVariableDeclaration[] { factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, callExpression, outputVars[0].getType(), outputVars[0].getName()) };
} else if (varsAreEqual(mustAdd, outputVars)) {
return createTupleDeclaration(outputVars, callExpression, helper.getProject());
} else {
final List<GrStatement> result = generateVarDeclarations(mustAdd, helper.getProject(), null);
result.add(createAssignment(outputVars, callExpression, helper.getProject()));
return result.toArray(new GrStatement[result.size()]);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class ExtractUtil method createTupleDeclaration.
private static GrStatement[] createTupleDeclaration(final VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
StringBuilder tuple = new StringBuilder();
tuple.append("def (");
for (VariableInfo info : infos) {
final PsiType type = info.getType();
if (type != null) {
final PsiType unboxed = TypesUtil.unboxPrimitiveTypeWrapper(type);
tuple.append(unboxed.getCanonicalText());
tuple.append(' ');
}
tuple.append(info.getName());
tuple.append(",");
}
StringUtil.trimEnd(tuple, ",");
tuple.append(")=");
tuple.append(callExpression.getText());
return new GrStatement[] { factory.createStatementFromText(tuple) };
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class ExtractUtil method createMethod.
public static GrMethod createMethod(ExtractMethodInfoHelper helper) {
StringBuilder buffer = new StringBuilder();
//Add signature
PsiType type = helper.getOutputType();
final PsiPrimitiveType outUnboxed = PsiPrimitiveType.getUnboxedType(type);
if (outUnboxed != null)
type = outUnboxed;
String modifier = getModifierString(helper);
String typeText = getTypeString(helper, false, modifier);
buffer.append(modifier);
buffer.append(typeText);
appendName(buffer, helper.getName());
buffer.append("(");
for (String param : getParameterString(helper, true)) {
buffer.append(param);
}
buffer.append(") { \n");
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
generateBody(helper, PsiType.VOID.equals(type), buffer, helper.isForceReturn());
buffer.append("\n}");
String methodText = buffer.toString();
return factory.createMethodFromText(methodText, helper.getContext());
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyGenerationInfo method insert.
@Override
public void insert(@NotNull PsiClass aClass, @Nullable PsiElement anchor, boolean before) throws IncorrectOperationException {
final T proto = getPsiMember();
if (proto instanceof GrMethod) {
GroovyChangeContextUtil.encodeContextInfo(((GrMethod) proto).getParameterList());
}
super.insert(aClass, anchor, before);
final T member = getPsiMember();
if (member == null)
return;
LOG.assertTrue(member instanceof GroovyPsiElement, member);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(member.getProject());
final PsiElement prev = member.getPrevSibling();
if (prev != null && GroovyTokenTypes.mNLS == prev.getNode().getElementType()) {
prev.replace(factory.createLineTerminator(1));
} else if (prev instanceof PsiMember) {
member.getParent().getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", member.getNode());
}
final PsiElement next = member.getNextSibling();
if (next != null && GroovyTokenTypes.mNLS == next.getNode().getElementType()) {
next.replace(factory.createLineTerminator(1));
} else if (next instanceof PsiMember) {
member.getParent().getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", next.getNode());
}
if (member instanceof GrMethod) {
GroovyChangeContextUtil.decodeContextInfo(((GrMethod) member).getParameterList(), null, null);
}
JavaCodeStyleManager.getInstance(member.getProject()).shortenClassReferences(member);
adjustDocCommentIfExists(member);
}
Aggregations