PsiLocalVariable (PsiLocalVariable origVar, PsiExpression initializer) { // Don't normalize the original declaration: it may declare many variables PsiElement declCopy = origVar.getParent().copy(); PsiLocalVariable varCopy = (PsiLocalVariable)ContainerUtil.find( declCopy.getChildren(), e -> e instanceof PsiLocalVariable && Objects.equals(origVar.getName(), ((PsiLocalVariable)e).getName())); if (varCopy != null) { varCopy.setInitializer(initializer); varCopy.normalizeDeclaration(); } return varCopy; }