use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class ForToEachIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrForStatement parentStatement = (GrForStatement) element;
final GrForInClause clause = (GrForInClause) parentStatement.getClause();
final GrVariable var = clause.getDeclaredVariable();
final GrStatement body = parentStatement.getBody();
final String bodyText;
if (body instanceof GrBlockStatement) {
final String text = body.getText();
bodyText = text.substring(1, text.length() - 1);
} else {
bodyText = body.getText();
}
GrExpression collection = clause.getIteratedExpression();
assert collection != null;
@NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
parentStatement.replaceWithStatement(eachExpression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
PsiElement parent = element.getParent();
PsiElement pparent;
if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
pparent = parent.getParent();
} else if (isModifierListOfVarDecl(element, parent)) {
pparent = parent;
} else {
return false;
}
if (pparent instanceof GrVariableDeclaration) {
if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
return false;
GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
for (GrVariable variable : variables) {
if (isVarDeclaredWithInitializer(variable))
return true;
}
} else if (pparent instanceof GrForInClause) {
final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
} else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
} else {
final GrVariable variable = (GrVariable) parent;
return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
}
return false;
}
private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
}
private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
}
private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class GroovyAnnotator method visitVariable.
@Override
public void visitVariable(@NotNull GrVariable variable) {
checkName(variable);
PsiElement parent = variable.getParent();
if (parent instanceof GrForInClause) {
PsiElement delimiter = ((GrForInClause) parent).getDelimiter();
if (delimiter.getNode().getElementType() == GroovyTokenTypes.mCOLON) {
GrTypeElement typeElement = variable.getTypeElementGroovy();
GrModifierList modifierList = variable.getModifierList();
if (typeElement == null && StringUtil.isEmptyOrSpaces(modifierList.getText())) {
Annotation annotation = myHolder.createErrorAnnotation(variable.getNameIdentifierGroovy(), GroovyBundle.message("java.style.for.each.statement.requires.a.type.declaration"));
annotation.registerFix(new ReplaceDelimiterFix());
}
}
}
PsiNamedElement duplicate = ResolveUtil.findDuplicate(variable);
if (duplicate instanceof GrVariable && (variable instanceof GrField || ResolveUtil.isScriptField(variable) || !(duplicate instanceof GrField))) {
final String key = duplicate instanceof GrField ? "field.already.defined" : "variable.already.defined";
myHolder.createErrorAnnotation(variable.getNameIdentifierGroovy(), GroovyBundle.message(key, variable.getName()));
}
PsiType type = variable.getDeclaredType();
if (type instanceof PsiEllipsisType && !isLastParameter(variable)) {
TextRange range = getEllipsisRange(variable);
if (range == null) {
range = getTypeRange(variable);
}
if (range != null) {
myHolder.createErrorAnnotation(range, GroovyBundle.message("ellipsis.type.is.not.allowed.here"));
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class ReplaceDelimiterFix method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiFile file = element.getContainingFile();
PsiElement at = file.findElementAt(editor.getCaretModel().getOffset());
GrForStatement forStatement = PsiTreeUtil.getParentOfType(at, GrForStatement.class);
if (forStatement == null)
return;
GrForClause clause = forStatement.getClause();
if (clause instanceof GrForInClause) {
GrForStatement stubFor = (GrForStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("for (x in y){}");
PsiElement newDelimiter = ((GrForInClause) stubFor.getClause()).getDelimiter();
PsiElement delimiter = ((GrForInClause) clause).getDelimiter();
delimiter.replace(newDelimiter);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method visitVariable.
@Override
public void visitVariable(@NotNull GrVariable variable) {
super.visitVariable(variable);
final PsiType varType = variable.getType();
final PsiElement parent = variable.getParent();
if (variable instanceof GrParameter && ((GrParameter) variable).getDeclarationScope() instanceof GrMethod || parent instanceof GrForInClause) {
return;
} else if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
//check tuple assignment: def (int x, int y) = foo()
final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
final GrExpression initializer = tuple.getTupleInitializer();
if (initializer == null)
return;
if (!(initializer instanceof GrListOrMap)) {
PsiType type = initializer.getType();
if (type == null)
return;
PsiType valueType = extractIterableTypeParameter(type, false);
processAssignment(varType, valueType, tuple, variable.getNameIdentifierGroovy());
return;
}
}
GrExpression initializer = variable.getInitializerGroovy();
if (initializer == null)
return;
processAssignment(varType, initializer, variable.getNameIdentifierGroovy(), "cannot.assign", variable, ApplicableTo.ASSIGNMENT);
}
Aggregations