use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression in project intellij-community by JetBrains.
the class TypeCastSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@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);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression in project intellij-community by JetBrains.
the class GroovyTypeCastSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
if (e instanceof GrTypeCastExpression) {
GrTypeCastExpression castExpression = ((GrTypeCastExpression) e);
GrTypeElement type = castExpression.getCastTypeElement();
TextRange range = type.getTextRange();
if (range.contains(cursorOffset)) {
PsiElement leftParen = castExpression.getLeftParen();
PsiElement rightParen = castExpression.getRightParen();
if (leftParen.getTextOffset() < rightParen.getTextOffset()) {
range = new TextRange(leftParen.getTextRange().getStartOffset(), rightParen.getTextRange().getEndOffset());
result.add(range);
}
}
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression in project intellij-community by JetBrains.
the class GrConvertTypeCastToSafeCastIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrTypeCastExpression))
return;
GrExpression operand = ((GrTypeCastExpression) element).getOperand();
GrTypeElement type = ((GrTypeCastExpression) element).getCastTypeElement();
if (type == null)
return;
if (operand == null)
return;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrExpression safeCast = factory.createExpressionFromText(operand.getText() + " as " + type.getText());
((GrTypeCastExpression) element).replaceWithExpression(safeCast, true);
}
Aggregations