TreeElement (@NotNull ASTNode child, @NotNull TreeElement newChild, @NotNull PsiManager manager) { boolean needParenth = ElementType.EXPRESSION_BIT_SET.contains(child.getElementType()) && ElementType.EXPRESSION_BIT_SET.contains(newChild.getElementType()) && ReplaceExpressionUtil.isNeedParenthesis(child, newChild); if (!needParenth) return newChild; CompositeElement parenthExpr = ASTFactory.composite(JavaElementType.PARENTH_EXPRESSION); TreeElement dummyExpr = (TreeElement)newChild.clone(); CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild); new DummyHolder(manager, parenthExpr, null, charTableByTree); parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "(")); parenthExpr.rawAddChildren(dummyExpr); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")")); try { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr)); parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted); } catch (IncorrectOperationException e) { LOG.error(e); // should not happen } newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild)); dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild); // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr)); return parenthExpr; }