use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ExtractUtil method generateBody.
public static void generateBody(ExtractInfoHelper helper, boolean isVoid, StringBuilder buffer, boolean forceReturn) {
VariableInfo[] outputInfos = helper.getOutputVariableInfos();
ParameterInfo[] infos = helper.getParameterInfos();
Set<String> declaredVars = new HashSet<>();
for (ParameterInfo info : infos) {
declaredVars.add(info.getName());
}
for (VariableInfo info : mustAddVariableDeclaration(helper.getStatements(), outputInfos)) {
declaredVars.add(info.getName());
}
List<VariableInfo> genDecl = new ArrayList<>();
final Collection<GrVariable> outside = collectUsedLocalVarsOrParamsDeclaredOutside(helper);
for (final GrVariable variable : outside) {
if (!declaredVars.contains(variable.getName())) {
genDecl.add(new VariableInfo() {
@NotNull
@Override
public String getName() {
return variable.getName();
}
@Override
public PsiType getType() {
return variable.getDeclaredType();
}
});
}
}
final List<GrStatement> statements = generateVarDeclarations(genDecl, helper.getProject(), null);
for (GrStatement statement : statements) {
buffer.append(statement.getText()).append('\n');
}
final StringPartInfo stringPartInfo = helper.getStringPartInfo();
if (!isSingleExpression(helper.getStatements()) && stringPartInfo == null) {
for (PsiElement element : helper.getInnerElements()) {
buffer.append(element.getText());
}
//append return statement
if (!isVoid && outputInfos.length > 0) {
buffer.append('\n');
if (forceReturn) {
buffer.append("return ");
}
if (outputInfos.length > 1)
buffer.append('[');
for (VariableInfo info : outputInfos) {
buffer.append(info.getName()).append(", ");
}
buffer.delete(buffer.length() - 2, buffer.length());
if (outputInfos.length > 1)
buffer.append(']');
}
} else {
GrExpression expr = stringPartInfo != null ? stringPartInfo.createLiteralFromSelected() : (GrExpression) PsiUtil.skipParentheses(helper.getStatements()[0], false);
boolean addReturn = !isVoid && forceReturn && !PsiUtil.isVoidMethodCall(expr);
if (addReturn) {
buffer.append("return ");
final GrExpression methodCall = ApplicationStatementUtil.convertToMethodCallExpression(expr);
buffer.append(methodCall.getText());
} else {
buffer.append(expr != null ? expr.getText() : "");
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.
private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
final GroovyResolveResult[] results = refExpr.multiResolve(false);
for (GroovyResolveResult result : results) {
final PsiElement resolved = result.getElement();
if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
if (parent != null) {
PsiElement current = element;
while (current != null && current != parent) {
if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
return;
}
current = current.getParent();
}
}
if (!place.getManager().areElementsEquivalent(element, resolved)) {
visitor.visitCollidingVariable((PsiVariable) resolved);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ExpressionGenerator method createVarByInitializer.
private String createVarByInitializer(@NotNull GrExpression initializer) {
GrExpression inner = initializer;
while (inner instanceof GrParenthesizedExpression) inner = ((GrParenthesizedExpression) inner).getOperand();
if (inner != null)
initializer = inner;
if (initializer instanceof GrReferenceExpression) {
final GrExpression qualifier = ((GrReferenceExpression) initializer).getQualifier();
if (qualifier == null) {
final PsiElement resolved = ((GrReferenceExpression) initializer).resolve();
if (resolved instanceof GrVariable && !(resolved instanceof GrField)) {
//don't create new var. it is already exists
return ((GrVariable) resolved).getName();
}
}
}
final String name = GenerationUtil.suggestVarName(initializer, context);
final StringBuilder builder = new StringBuilder();
builder.append("final ");
TypeWriter.writeType(builder, initializer.getType(), initializer);
builder.append(' ').append(name).append(" = ");
initializer.accept(new ExpressionGenerator(builder, context));
builder.append(';');
context.myStatements.add(builder.toString());
return name;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GenerationUtil method writeSimpleVarDeclaration.
public static void writeSimpleVarDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
GrVariable[] variables = variableDeclaration.getVariables();
//if (types.size() > 1) {
if (variables.length > 1 && variableDeclaration.getParent() instanceof GrControlStatement) {
expressionContext.setInsertCurlyBrackets();
}
for (GrVariable variable : variables) {
writeVariableSeparately(variable, builder, expressionContext);
builder.append(";\n");
}
builder.delete(builder.length() - 1, builder.length());
//builder.removeFromTheEnd(1);
/*return;
}
ModifierListGenerator.writeModifiers(builder, variableDeclaration.getModifierList());
PsiType type = getVarType(variables[0]);
writeType(builder, type, variableDeclaration);
builder.append(" ");
for (GrVariable variable : variables) {
writeVariableWithoutType(builder, expressionContext, variable);
builder.append(", ");
}
if (variables.length > 0) {
builder.delete(builder.length() - 2, builder.length());
}
builder.append(";");*/
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class LocalVarAnalyzer method visitReferenceExpression.
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
super.visitReferenceExpression(ref);
PsiElement resolved = ref.resolve();
if (!allVars.contains(resolved))
return;
GrVariable var = (GrVariable) resolved;
if (PsiUtil.isAccessedForWriting(ref)) {
rewritten.add(var);
}
if (allVars.get(var) < grade) {
touched.add((PsiVariable) resolved);
}
}
Aggregations