TextRange (@NotNull GrExpression expression, PsiElement context) { GrParenthesizedExpression parenthesized = (GrParenthesizedExpression) GroovyPsiElementFactory.getInstance(expression.getProject()).createExpressionFromText("((Type)a)", context); GrTypeCastExpression typeCast = (GrTypeCastExpression) parenthesized.getOperand(); replaceToOldExpression(typeCast.getOperand(), expression); GrTypeElement typeElement = typeCast.getCastTypeElement(); int endOffset = typeElement.getTextRange().getStartOffset() + expression.getTextRange().getStartOffset(); parenthesized = (GrParenthesizedExpression) expression.replaceWithExpression(parenthesized, false); final GrTypeCastExpression newTypeCast = (GrTypeCastExpression)parenthesized.getOperand(); final GrTypeElement newTypeElement = newTypeCast.getCastTypeElement(); newTypeElement.delete(); return new TextRange(endOffset, endOffset); }