PsiLocalVariable[] (PsiCodeBlock target, PsiCodeBlock body, PsiLocalVariable[] declaredVars) { PsiElement firstBodyElement = body.getFirstBodyElement(); PsiElement lastBodyElement = body.getLastBodyElement(); if (firstBodyElement == null || lastBodyElement == null) return declaredVars; PsiElement firstAdded = target.addRange(firstBodyElement, lastBodyElement); PsiLocalVariable[] updatedVars = new PsiLocalVariable[declaredVars.length]; int index = 0; for (PsiElement e = firstAdded; index < updatedVars.length && e != null; e = e.getNextSibling()) { if (e instanceof PsiDeclarationStatement) { PsiElement[] elements = ((PsiDeclarationStatement)e).getDeclaredElements(); if (elements.length == 1) { PsiLocalVariable var = tryCast(elements[0], PsiLocalVariable.class); if (var != null) { if (var.getName().equals(declaredVars[index].getName())) { updatedVars[index++] = var; } } } } } assert index == updatedVars.length; return updatedVars; }