String (GrExpression expr, final GroovyPsiElement context, boolean declareFinal) { expr = addBlockIntoParent(expr); final GrVariableDeclarationOwner block = PsiTreeUtil.getParentOfType(expr, GrVariableDeclarationOwner.class); LOG.assertTrue(block != null); final PsiElement anchorStatement = PsiTreeUtil.findPrevParent(block, expr); LOG.assertTrue(anchorStatement instanceof GrStatement); Project project = expr.getProject(); String[] suggestedNames =GroovyNameSuggestionUtil.suggestVariableNames(expr, new NameValidator() { @Override public String validateName(String name, boolean increaseNumber) { return name; } @Override public Project getProject() { return context.getProject(); } }); /* JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expr, null).names;*/ final String prefix = suggestedNames[0]; final String id = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(prefix, context, true); GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expr.getProject()); String[] modifiers; if (declareFinal) { modifiers = finalModifiers; } else { modifiers = ArrayUtilRt.EMPTY_STRING_ARRAY; } GrVariableDeclaration decl = factory.createVariableDeclaration(modifiers, (GrExpression)org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil .skipParentheses(expr, false), expr.getType(), id); final GrStatement statement = ((GrStatementOwner)anchorStatement.getParent()).addStatementBefore(decl, (GrStatement)anchorStatement); JavaCodeStyleManager.getInstance(statement.getProject()).shortenClassReferences(statement); return id; }