Search in sources :

Example 16 with StringPartInfo

use of org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo 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() : "");
        }
    }
}
Also used : VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Aggregations

StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)16 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)11 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 Editor (com.intellij.openapi.editor.Editor)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiType (com.intellij.psi.PsiType)2 NameSuggestionsField (com.intellij.refactoring.ui.NameSuggestionsField)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 PsiElement (com.intellij.psi.PsiElement)1 ChangedMethodCallInfo (com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo)1 ExternalUsageInfo (com.intellij.refactoring.introduceParameter.ExternalUsageInfo)1 InternalUsageInfo (com.intellij.refactoring.introduceParameter.InternalUsageInfo)1 JavaVisibilityPanel (com.intellij.refactoring.ui.JavaVisibilityPanel)1 UsageInfo (com.intellij.usageView.UsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1