TextRange (@NotNull GrExpression expression, @Nullable PsiElement context) { final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject()); final GrUnaryExpression template = (GrUnaryExpression)factory.createExpressionFromText("!(a)", context); assert template.getOperand() != null; GroovyExpressionSurrounder.replaceToOldExpression(((GrParenthesizedExpression)template.getOperand()).getOperand(), expression); final GrExpression result = expression.replaceWithExpression(template, true); final int endOffset = result.getTextRange().getEndOffset(); return new TextRange(endOffset, endOffset); }