Search in sources :

Example 81 with GrParameter

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

the class PsiImplUtil method isMainMethod.

public static boolean isMainMethod(GrMethod method) {
    if (!method.getName().equals(MAIN_METHOD))
        return false;
    else if (!method.hasModifierProperty(PsiModifier.STATIC))
        return false;
    final GrParameter[] parameters = method.getParameters();
    if (parameters.length == 0)
        return false;
    if (parameters.length == 1 && parameters[0].getTypeElementGroovy() == null)
        return true;
    int args_count = 0;
    int optional_count = 0;
    for (GrParameter p : parameters) {
        final GrTypeElement declaredType = p.getTypeElementGroovy();
        if ((declaredType == null || declaredType.getType().equalsToText(CommonClassNames.JAVA_LANG_STRING + "[]")) && p.getInitializerGroovy() == null) {
            args_count++;
        }
        if (p.getInitializerGroovy() != null)
            optional_count++;
    }
    return optional_count == parameters.length - 1 && args_count == 1;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 82 with GrParameter

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

the class ConvertParameterToMapEntryIntention method checkForMapParameters.

private static boolean checkForMapParameters(GrParametersOwner owner) {
    final GrParameter[] parameters = owner.getParameters();
    if (parameters.length != 1)
        return true;
    final GrParameter parameter = parameters[0];
    final PsiType type = parameter.getTypeGroovy();
    if (!(type instanceof PsiClassType))
        return true;
    final PsiClass psiClass = ((PsiClassType) type).resolve();
    return psiClass == null || !CommonClassNames.JAVA_UTIL_MAP.equals(psiClass.getQualifiedName());
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 83 with GrParameter

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

the class ConvertParameterToMapEntryIntention method processIntention.

@Override
protected void processIntention(@NotNull final PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
    // Method or closure to be refactored
    final GrParametersOwner owner = PsiTreeUtil.getParentOfType(element, GrParametersOwner.class);
    final Collection<PsiElement> occurrences = new ArrayList<>();
    // Find all referenced expressions
    final boolean success = collectOwnerOccurrences(project, owner, occurrences);
    if (!success)
        return;
    // Checking for Groovy files only
    final boolean isClosure = owner instanceof GrClosableBlock;
    if (!checkOwnerOccurrences(project, occurrences, isClosure))
        return;
    // To add or not to add new parameter for map entries
    final GrParameter firstParam = getFirstParameter(owner);
    switch(analyzeForNamedArguments(owner, occurrences)) {
        case ERROR:
            {
                final GrNamedElement namedElement = getReferencedElement(owner);
                LOG.assertTrue(namedElement != null);
                final String msg = GroovyIntentionsBundle.message("wrong.first.parameter.type", isClosure ? CLOSURE_CAPTION_CAP : METHOD_CAPTION_CAP, namedElement.getName(), firstParam.getName());
                showErrorMessage(msg, project);
                return;
            }
        case MUST_BE_MAP:
            {
                if (firstParam == getAppropriateParameter(element)) {
                    final String msg = GroovyIntentionsBundle.message("convert.cannot.itself");
                    showErrorMessage(msg, project);
                    return;
                }
                performRefactoring(element, owner, occurrences, false, null, false);
                break;
            }
        case IS_NOT_MAP:
            {
                if (!ApplicationManager.getApplication().isUnitTestMode()) {
                    final String[] possibleNames = generateValidNames(MY_POSSIBLE_NAMES, firstParam);
                    final GroovyMapParameterDialog dialog = new GroovyMapParameterDialog(project, possibleNames, true) {

                        @Override
                        protected void doOKAction() {
                            String name = getEnteredName();
                            MultiMap<PsiElement, String> conflicts = new MultiMap<>();
                            assert name != null;
                            GroovyValidationUtil.validateNewParameterName(firstParam, conflicts, name);
                            if (isClosure) {
                                findClosureConflictUsages(conflicts, occurrences);
                            }
                            if (reportConflicts(conflicts, project)) {
                                performRefactoring(element, owner, occurrences, createNewFirst(), name, specifyTypeExplicitly());
                            }
                            super.doOKAction();
                        }
                    };
                    dialog.show();
                } else {
                    //todo add statictics manager
                    performRefactoring(element, owner, occurrences, true, (new GroovyValidationUtil.ParameterNameSuggester("attrs", firstParam)).generateName(), true);
                }
                break;
            }
    }
}
Also used : GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) MultiMap(com.intellij.util.containers.MultiMap) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement)

Example 84 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter 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)

Example 85 with GrParameter

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

the class MyPredicate method checkForMethodParameter.

@Nullable
public static GrParameter checkForMethodParameter(GrExpression map) {
    final GrParameter parameter = getParameterByArgument(map);
    if (parameter == null)
        return null;
    final PsiElement parent = parameter.getParent().getParent();
    if (!(parent instanceof PsiMethod))
        return null;
    final PsiMethod method = (PsiMethod) parent;
    if (ApplicationManager.getApplication().isUnitTestMode() || Messages.showYesNoDialog(map.getProject(), GroovyIntentionsBundle.message("do.you.want.to.change.type.of.parameter.in.method", parameter.getName(), method.getName()), GroovyIntentionsBundle.message("convert.map.to.class.intention.name"), Messages.getQuestionIcon()) == Messages.YES) {
        return parameter;
    }
    return null;
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6