Search in sources :

Example 86 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class GenericInlineHandler method invoke.

public static boolean invoke(final PsiElement element, @Nullable Editor editor, final InlineHandler languageSpecific) {
    final PsiReference invocationReference = editor != null ? TargetElementUtil.findReference(editor) : null;
    final InlineHandler.Settings settings = languageSpecific.prepareInlineElement(element, editor, invocationReference != null);
    if (settings == null || settings == InlineHandler.Settings.CANNOT_INLINE_SETTINGS) {
        return settings != null;
    }
    final Collection<? extends PsiReference> allReferences;
    if (settings.isOnlyOneReferenceToInline()) {
        allReferences = Collections.singleton(invocationReference);
    } else {
        final Ref<Collection<? extends PsiReference>> usagesRef = new Ref<>();
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> usagesRef.set(ReferencesSearch.search(element).findAll()), "Find Usages", false, element.getProject());
        allReferences = usagesRef.get();
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final Map<Language, InlineHandler.Inliner> inliners = initializeInliners(element, settings, allReferences);
    for (PsiReference reference : allReferences) {
        collectConflicts(reference, element, inliners, conflicts);
    }
    final Project project = element.getProject();
    if (!conflicts.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
        } else {
            final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts);
            if (!conflictsDialog.showAndGet()) {
                return true;
            }
        }
    }
    HashSet<PsiElement> elements = new HashSet<>();
    for (PsiReference reference : allReferences) {
        PsiElement refElement = reference.getElement();
        if (refElement != null) {
            elements.add(refElement);
        }
    }
    if (!settings.isOnlyOneReferenceToInline()) {
        elements.add(element);
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, elements, true)) {
        return true;
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        final String subj = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : "element";
        CommandProcessor.getInstance().executeCommand(project, () -> {
            final PsiReference[] references = sortDepthFirstRightLeftOrder(allReferences);
            final UsageInfo[] usages = new UsageInfo[references.length];
            for (int i = 0; i < references.length; i++) {
                usages[i] = new UsageInfo(references[i]);
            }
            for (UsageInfo usage : usages) {
                inlineReference(usage, element, inliners);
            }
            if (!settings.isOnlyOneReferenceToInline()) {
                languageSpecific.removeDefinition(element, settings);
            }
        }, RefactoringBundle.message("inline.command", StringUtil.notNullize(subj, "<nameless>")), null);
    });
    return true;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiReference(com.intellij.psi.PsiReference) MultiMap(com.intellij.util.containers.MultiMap) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) Language(com.intellij.lang.Language) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) InlineHandler(com.intellij.lang.refactoring.InlineHandler) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 87 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class IntroduceParameterObjectProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usageInfos) {
    final PsiElement aClass = myClassDescriptor.createClass(myMethod, myAccessors);
    if (aClass != null) {
        myClassDescriptor.setExistingClass(aClass);
        super.performRefactoring(usageInfos);
        List<UsageInfo> changeSignatureUsages = new ArrayList<>();
        for (UsageInfo info : usageInfos) {
            if (info instanceof ChangeSignatureUsageWrapper) {
                changeSignatureUsages.add(((ChangeSignatureUsageWrapper) info).getInfo());
            }
        }
        ChangeSignatureProcessorBase.doChangeSignature(myChangeInfo, changeSignatureUsages.toArray(new UsageInfo[changeSignatureUsages.size()]));
    }
}
Also used : ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) OverriderMethodUsageInfo(com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo)

Example 88 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class IntroduceParameterObjectProcessor method findUsages.

@Override
protected void findUsages(@NotNull List<FixableUsageInfo> usages) {
    if (myClassDescriptor.isUseExistingClass()) {
        myClassDescriptor.setExistingClassCompatibleConstructor(myClassDescriptor.findCompatibleConstructorInExistingClass(myMethod));
    }
    List<PsiNamedElement> methodHierarchy = new ArrayList<>();
    methodHierarchy.add(myMethod);
    for (UsageInfo info : ChangeSignatureProcessorBase.findUsages(myChangeInfo)) {
        if (info instanceof OverriderMethodUsageInfo) {
            methodHierarchy.add(((OverriderMethodUsageInfo) info).getOverridingMethod());
        }
        usages.add(new ChangeSignatureUsageWrapper(info));
    }
    final P[] paramsToMerge = myClassDescriptor.getParamsToMerge();
    for (PsiElement element : methodHierarchy) {
        final IntroduceParameterObjectDelegate delegate = IntroduceParameterObjectDelegate.findDelegate(element);
        if (delegate != null) {
            for (int i = 0; i < paramsToMerge.length; i++) {
                ReadWriteAccessDetector.Access access = delegate.collectInternalUsages(usages, (PsiNamedElement) element, myClassDescriptor, paramsToMerge[i], myMergedParameterInfo.getName());
                if (myAccessors[i] == null || access == ReadWriteAccessDetector.Access.Write) {
                    myAccessors[i] = access;
                }
            }
        }
    }
    myDelegate.collectUsagesToGenerateMissedFieldAccessors(usages, myMethod, myClassDescriptor, myAccessors);
    myDelegate.collectAdditionalFixes(usages, myMethod, myClassDescriptor);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector) UsageInfo(com.intellij.usageView.UsageInfo) OverriderMethodUsageInfo(com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo) FixableUsageInfo(com.intellij.refactoring.util.FixableUsageInfo) OverriderMethodUsageInfo(com.intellij.refactoring.changeSignature.OverriderMethodUsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 89 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class MoveFilesOrDirectoriesProcessor method findUsages.

@Override
@NotNull
protected UsageInfo[] findUsages() {
    ArrayList<UsageInfo> result = new ArrayList<>();
    for (int i = 0; i < myElementsToMove.length; i++) {
        PsiElement element = myElementsToMove[i];
        if (mySearchForReferences) {
            for (PsiReference reference : ReferencesSearch.search(element, GlobalSearchScope.projectScope(myProject))) {
                result.add(new MyUsageInfo(reference.getElement(), i, reference));
            }
        }
        findElementUsages(result, element);
    }
    return result.toArray(new UsageInfo[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) UsageInfo(com.intellij.usageView.UsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class MoveFilesOrDirectoriesProcessor method retargetUsages.

protected void retargetUsages(UsageInfo[] usages, Map<PsiElement, PsiElement> oldToNewMap) {
    final List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usageInfo : usages) {
        if (usageInfo instanceof MyUsageInfo) {
            final MyUsageInfo info = (MyUsageInfo) usageInfo;
            final PsiElement element = myElementsToMove[info.myIndex];
            if (info.getReference() instanceof FileReference || info.getReference() instanceof PsiDynaReference) {
                final PsiElement usageElement = info.getElement();
                if (usageElement != null) {
                    final PsiFile usageFile = usageElement.getContainingFile();
                    final PsiFile psiFile = usageFile.getViewProvider().getPsi(usageFile.getViewProvider().getBaseLanguage());
                    if (psiFile != null && psiFile.equals(element)) {
                        // already processed in MoveFilesOrDirectoriesUtil.doMoveFile
                        continue;
                    }
                }
            }
            final PsiElement refElement = info.myReference.getElement();
            if (refElement != null && refElement.isValid()) {
                info.myReference.bindToElement(element);
            }
        } else if (usageInfo instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usageInfo);
        }
    }
    for (PsiFile movedFile : myFoundUsages.keySet()) {
        MoveFileHandler.forElement(movedFile).retargetUsages(myFoundUsages.get(movedFile), oldToNewMap);
    }
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
Also used : PsiDynaReference(com.intellij.openapi.paths.PsiDynaReference) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) UsageInfo(com.intellij.usageView.UsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) PsiElement(com.intellij.psi.PsiElement)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)283 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)41 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 PsiFile (com.intellij.psi.PsiFile)33 MultiMap (com.intellij.util.containers.MultiMap)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 Nullable (org.jetbrains.annotations.Nullable)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)17 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)17 Project (com.intellij.openapi.project.Project)16 HashSet (com.intellij.util.containers.HashSet)15 TextRange (com.intellij.openapi.util.TextRange)12 PsiReference (com.intellij.psi.PsiReference)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11