Search in sources :

Example 6 with GrParametersOwner

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

the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.

private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
    final PsiElement namedElem = getReferencedElement(owner);
    if (namedElem == null)
        return true;
    final Ref<Boolean> result = new Ref<>(true);
    final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
            final Processor<PsiReference> consumer = psiReference -> {
                references.add(psiReference);
                return true;
            };
            ReferencesSearch.search(namedElem).forEach(consumer);
            boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
                }
            });
            if (isProperty) {
                final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {

                    @Override
                    public GrAccessorMethod[] compute() {
                        return ((GrField) namedElem).getGetters();
                    }
                });
                for (GrAccessorMethod getter : getters) {
                    MethodReferencesSearch.search(getter).forEach(consumer);
                }
            }
            for (final PsiReference reference : references) {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        occurrences.add(element);
                    }
                });
            }
        }

        @Override
        public void onCancel() {
            result.set(false);
        }

        @Override
        public void onThrowable(@NotNull Throwable error) {
            super.onThrowable(error);
            result.set(false);
        }

        @Override
        public void onSuccess() {
            result.set(true);
        }
    };
    ProgressManager.getInstance().run(task);
    return result.get().booleanValue();
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) HashSet(com.intellij.util.containers.HashSet) GrMethodImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef.members.GrMethodImpl) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Task(com.intellij.openapi.progress.Task) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) Ref(com.intellij.openapi.util.Ref) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) GroovyIntentionsBundle(org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) StringUtil(com.intellij.openapi.util.text.StringUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyValidationUtil(org.jetbrains.plugins.groovy.refactoring.GroovyValidationUtil) Collections(java.util.Collections) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Task(com.intellij.openapi.progress.Task) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 7 with GrParametersOwner

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

Aggregations

GrParametersOwner (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner)7 ArrayList (java.util.ArrayList)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 PsiElement (com.intellij.psi.PsiElement)2 MultiMap (com.intellij.util.containers.MultiMap)2 NotNull (org.jetbrains.annotations.NotNull)2 GrNamedElement (org.jetbrains.plugins.groovy.lang.psi.GrNamedElement)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1