Search in sources :

Example 6 with GrForInClause

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);
}
Also used : GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrCatchClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrForInClause

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;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)

Example 8 with GrForInClause

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);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)

Example 9 with GrForInClause

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);
}
Also used : GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) GrForStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)

Example 10 with GrForInClause

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);
                    }
                });
            }
        }
    });
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) ArrayList(java.util.ArrayList) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) Template(com.intellij.codeInsight.template.Template) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiParameter(com.intellij.psi.PsiParameter) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Aggregations

GrForInClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)5 GrForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause)4 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 PsiElement (com.intellij.psi.PsiElement)3 GrForStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 TextRange (com.intellij.openapi.util.TextRange)2 GrTraditionalForClause (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)2 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)2 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 PsiFile (com.intellij.psi.PsiFile)1 PsiParameter (com.intellij.psi.PsiParameter)1