Search in sources :

Example 1 with GrParameterList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.

the class GrSetStrongTypeIntention method getClosureParameterType.

@Nullable
private static PsiType getClosureParameterType(@NotNull PsiParameter parameter) {
    final PsiElement scope = parameter.getDeclarationScope();
    final PsiType type;
    if (scope instanceof GrClosableBlock) {
        type = ClosureParameterEnhancer.inferType((GrClosableBlock) scope, ((GrParameterList) parameter.getParent()).getParameterIndex(parameter));
    } else {
        type = null;
    }
    return type;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GrParameterList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList 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();
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrParameterList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method generateDelegateClosure.

private GrClosableBlock generateDelegateClosure(GrClosableBlock originalClosure, GrVariable anchor, String newName) {
    GrClosableBlock result;
    if (originalClosure.hasParametersSection()) {
        result = myFactory.createClosureFromText("{->}", anchor);
        final GrParameterList parameterList = (GrParameterList) originalClosure.getParameterList().copy();
        result.getParameterList().replace(parameterList);
    } else {
        result = myFactory.createClosureFromText("{}", anchor);
    }
    StringBuilder call = new StringBuilder();
    call.append(newName).append('(');
    final GrParameter[] parameters = result.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        if (!mySettings.parametersToRemove().contains(i)) {
            call.append(parameters[i].getName()).append(", ");
        }
    }
    call.append(myParameterInitializer.getText());
    call.append(")");
    final GrStatement statement = myFactory.createStatementFromText(call.toString());
    result.addStatementBefore(statement, null);
    return result;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 4 with GrParameterList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.

the class GroovyIntroduceParameterMethodUsagesProcessor method addParameter.

@NotNull
public static GrParameter addParameter(@NotNull GrParametersOwner parametersOwner, @Nullable MethodJavaDocHelper javaDocHelper, @NotNull PsiType forcedType, @NotNull String parameterName, boolean isFinal, @NotNull Project project) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final String typeText = forcedType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) || forcedType == PsiType.NULL || PsiType.VOID.equals(forcedType) ? null : forcedType.getCanonicalText();
    GrParameter parameter = factory.createParameter(parameterName, typeText, parametersOwner);
    parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, isFinal);
    final PsiParameter anchorParameter = getAnchorParameter(parametersOwner);
    final GrParameterList parameterList = parametersOwner.getParameterList();
    parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(parameter);
    if (javaDocHelper != null) {
        final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
        javaDocHelper.addParameterAfter(parameterName, tagForAnchorParameter);
    }
    return parameter;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with GrParameterList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList in project intellij-community by JetBrains.

the class ConvertClosureArgToItIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrClosableBlock closure = (GrClosableBlock) element;
    final GrParameterList parameterList = closure.getParameterList();
    final GrParameter parameter = parameterList.getParameters()[0];
    final Set<GrReferenceExpression> referencesToChange = new HashSet<>();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            if (!referenceExpression.getText().equals(parameter.getName())) {
                return;
            }
            final PsiElement referent = referenceExpression.resolve();
            if (parameter.equals(referent)) {
                referencesToChange.add(referenceExpression);
            }
        }
    };
    closure.accept(visitor);
    parameter.delete();
    for (GrReferenceExpression referenceExpression : referencesToChange) {
        PsiImplUtil.replaceExpression("it", referenceExpression);
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)22 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)14 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)7 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 PsiType (com.intellij.psi.PsiType)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 Nullable (org.jetbrains.annotations.Nullable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)2