use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class GrParameterImpl method getTypeGroovy.
@Override
@Nullable
public PsiType getTypeGroovy() {
final PsiType declaredType = getDeclaredType();
if (declaredType != null)
return declaredType;
if (isVarArgs()) {
PsiClassType type = TypesUtil.getJavaLangObject(this);
return new PsiEllipsisType(type);
}
PsiElement parent = getParent();
if (parent instanceof GrForInClause) {
GrExpression iteratedExpression = ((GrForInClause) parent).getIteratedExpression();
if (iteratedExpression == null)
return null;
PsiType result = ClosureParameterEnhancer.findTypeForIteration(iteratedExpression, this);
if (result != null) {
return result;
}
} else if (parent instanceof GrTraditionalForClause) {
return super.getTypeGroovy();
} else if (parent instanceof GrCatchClause) {
return TypesUtil.createTypeByFQClassName(CommonClassNames.JAVA_LANG_EXCEPTION, this);
}
return GrVariableEnhancer.getEnhancedType(this);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class GrForStatementImpl method processDeclarations.
@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
if (!ResolveUtil.shouldProcessProperties(processor.getHint(ElementClassHint.KEY)))
return true;
GrForClause forClause = getClause();
final GrVariable varScope = PsiTreeUtil.getParentOfType(place, GrVariable.class);
if (forClause == null)
return true;
if (lastParent == null || lastParent instanceof GrForInClause)
return true;
GrVariable var = forClause.getDeclaredVariable();
if (var == null || var.equals(varScope))
return true;
if (!ResolveUtil.processElement(processor, var, state))
return false;
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class WritesCounterDFAInstance method fun.
@Override
public void fun(@NotNull TObjectIntHashMap<GrVariable> map, @NotNull Instruction instruction) {
if (!(instruction instanceof ReadWriteVariableInstruction))
return;
final ReadWriteVariableInstruction rwInstruction = (ReadWriteVariableInstruction) instruction;
if (!rwInstruction.isWrite())
return;
final GrVariable variable = getVariable(instruction.getElement());
if (variable == null)
return;
int currentVal = map.get(variable);
if (currentVal == 2)
return;
if (currentVal == 0 || currentVal == 1 && !(variable.getParent() instanceof GrForInClause))
currentVal++;
map.put(variable, currentVal);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class ForToEachPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrForStatement)) {
return false;
}
final GrForStatement statement = (GrForStatement) element;
final GrForClause clause = statement.getClause();
if (!(clause instanceof GrForInClause) || ((GrForInClause) clause).getIteratedExpression() == null) {
return false;
}
return !ErrorUtil.containsError(element);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
PsiElement elementToBuildTemplate;
GrVariable[] variables;
if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
elementToBuildTemplate = parent.getParent();
} else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent).getVariables();
elementToBuildTemplate = parent;
} else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariable) {
variables = new GrVariable[] { ((GrVariable) parent) };
elementToBuildTemplate = parent;
} else {
return;
}
ArrayList<TypeConstraint> types = new ArrayList<>();
if (parent.getParent() instanceof GrForInClause) {
types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
} else {
for (GrVariable variable : variables) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
PsiType type = initializer.getType();
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
if (variable instanceof GrParameter) {
final PsiParameter parameter = (PsiParameter) variable;
final PsiType type = getClosureParameterType(parameter);
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
}
}
final String originalText = elementToBuildTemplate.getText();
final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
final PsiElement replaceElement = typeInfo.elementToReplace;
TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
builder.replaceElement(replaceElement, chooseTypeExpression);
final Document document = editor.getDocument();
final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
rangeMarker.setGreedyToRight(true);
rangeMarker.setGreedyToLeft(true);
final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
final Template template = builder.buildTemplate();
TextRange range = afterPostprocess.getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
if (rangeMarker.isValid()) {
document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
}
});
}
}
});
}
Aggregations