use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class ChangeToOperatorInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCall) {
final String methodName = getMethodName(methodCall);
if (methodName == null)
return;
Transformation transformation = TRANSFORMATIONS.get(methodName);
if (transformation == null)
return;
PsiElement highlightElement = getHighlightElement(methodCall);
if (highlightElement == null)
return;
if (transformation.couldApply(methodCall, getOptions())) {
registerError(highlightElement, message("replace.with.operator.message", methodName), new LocalQuickFix[] { getFix(transformation, methodName) }, GENERIC_ERROR_OR_WARNING);
}
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class GroovyNameSuggestionUtil method generateNameByExpr.
private static void generateNameByExpr(GrExpression expr, Set<String> possibleNames, NameValidator validator, boolean forStaticVariable) {
if (expr instanceof GrReferenceExpression && ((GrReferenceExpression) expr).getReferenceName() != null) {
if (PsiUtil.isThisReference(expr)) {
possibleNames.add(validator.validateName("thisInstance", true));
}
if (PsiUtil.isSuperReference(expr)) {
possibleNames.add(validator.validateName("superInstance", true));
}
GrReferenceExpression refExpr = (GrReferenceExpression) expr;
String name = refExpr.getReferenceName();
if (name != null && name.toUpperCase().equals(name)) {
possibleNames.add(validator.validateName(name.toLowerCase(), true));
} else {
generateCamelNames(possibleNames, validator, name);
}
if (expr.getText().equals(name)) {
possibleNames.remove(name);
}
}
if (expr instanceof GrMethodCallExpression) {
generateNameByExpr(((GrMethodCallExpression) expr).getInvokedExpression(), possibleNames, validator, forStaticVariable);
}
if (expr instanceof GrLiteral) {
final Object value = ((GrLiteral) expr).getValue();
if (value instanceof String) {
generateNameByString(possibleNames, (String) value, validator, forStaticVariable, expr.getProject());
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method verifySafeCopyExpressionSubElement.
private static int verifySafeCopyExpressionSubElement(PsiElement element) {
int result = RefactoringUtil.EXPR_COPY_SAFE;
if (element == null)
return result;
if (element instanceof GrNamedElement) {
return RefactoringUtil.EXPR_COPY_SAFE;
}
if (element instanceof GrMethodCallExpression) {
result = RefactoringUtil.EXPR_COPY_UNSAFE;
}
if (element instanceof GrNewExpression) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
if (element instanceof GrAssignmentExpression) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
if (element instanceof GrClosableBlock) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
if (isPlusPlusOrMinusMinus(element)) {
return RefactoringUtil.EXPR_COPY_PROHIBITED;
}
PsiElement[] children = element.getChildren();
for (PsiElement child : children) {
int childResult = verifySafeCopyExpressionSubElement(child);
result = Math.max(result, childResult);
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class EachToForIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
final GrClosableBlock block = expression.getClosureArguments()[0];
final GrParameterList parameterList = block.getParameterList();
final GrParameter[] parameters = parameterList.getParameters();
String var;
if (parameters.length == 1) {
var = parameters[0].getText();
var = StringUtil.replace(var, GrModifier.DEF, "");
} else {
var = "it";
}
final GrExpression invokedExpression = expression.getInvokedExpression();
GrExpression qualifier = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(element.getProject());
if (qualifier == null) {
qualifier = elementFactory.createExpressionFromText("this");
}
StringBuilder builder = new StringBuilder();
builder.append("for (").append(var).append(" in ").append(qualifier.getText()).append(") {\n");
String text = block.getText();
final PsiElement blockArrow = block.getArrow();
int index;
if (blockArrow != null) {
index = blockArrow.getStartOffsetInParent() + blockArrow.getTextLength();
} else {
index = 1;
}
while (index < text.length() && Character.isWhitespace(text.charAt(index))) index++;
text = text.substring(index, text.length() - 1);
builder.append(text);
builder.append("}");
final GrStatement statement = elementFactory.createStatementFromText(builder.toString());
GrForStatement forStatement = (GrForStatement) expression.replaceWithStatement(statement);
final GrForClause clause = forStatement.getClause();
GrVariable variable = clause.getDeclaredVariable();
forStatement = updateReturnStatements(forStatement);
if (variable == null)
return;
if (ApplicationManager.getApplication().isUnitTestMode())
return;
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document doc = documentManager.getDocument(element.getContainingFile());
if (doc == null)
return;
documentManager.doPostponedOperationsAndUnblockDocument(doc);
editor.getCaretModel().moveToOffset(variable.getTextOffset());
new VariableInplaceRenamer(variable, editor).performInplaceRename();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.
the class ExplicitClosureCallPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression)) {
return false;
}
final GrMethodCallExpression call = (GrMethodCallExpression) element;
final GrExpression invokedExpression = call.getInvokedExpression();
if (invokedExpression == null) {
return false;
}
if (!(invokedExpression instanceof GrReferenceExpression)) {
return false;
}
final GrReferenceExpression referenceExpression = (GrReferenceExpression) invokedExpression;
final String name = referenceExpression.getReferenceName();
if (!"call".equals(name)) {
return false;
}
final GrExpression qualifier = referenceExpression.getQualifierExpression();
if (qualifier == null) {
return false;
}
final PsiType qualifierType = qualifier.getType();
if (qualifierType == null) {
return false;
}
if (!qualifierType.equalsToText(GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
return false;
}
return !ErrorUtil.containsError(element);
}
Aggregations