Search in sources :

Example 6 with PsiElement2UsageTargetAdapter

use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.

the class BaseRefactoringProcessor method previewRefactoring.

protected void previewRefactoring(@NotNull UsageInfo[] usages) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!PREVIEW_IN_TESTS)
            throw new RuntimeException("Unexpected preview in tests: " + StringUtil.join(usages, info -> info.toString(), ", "));
        ensureElementsWritable(usages, createUsageViewDescriptor(usages));
        execute(usages);
        return;
    }
    final UsageViewDescriptor viewDescriptor = createUsageViewDescriptor(usages);
    final PsiElement[] elements = viewDescriptor.getElements();
    final PsiElement2UsageTargetAdapter[] targets = PsiElement2UsageTargetAdapter.convert(elements);
    Factory<UsageSearcher> factory = new Factory<UsageSearcher>() {

        @Override
        public UsageSearcher create() {
            return new UsageInfoSearcherAdapter() {

                @Override
                public void generate(@NotNull final Processor<Usage> processor) {
                    ApplicationManager.getApplication().runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            for (int i = 0; i < elements.length; i++) {
                                elements[i] = targets[i].getElement();
                            }
                            refreshElements(elements);
                        }
                    });
                    processUsages(processor, myProject);
                }

                @NotNull
                @Override
                protected UsageInfo[] findUsages() {
                    return BaseRefactoringProcessor.this.findUsages();
                }
            };
        }
    };
    showUsageView(viewDescriptor, factory, usages);
}
Also used : Language(com.intellij.lang.Language) LocalHistoryAction(com.intellij.history.LocalHistoryAction) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) RefactoringListenerManager(com.intellij.refactoring.listeners.RefactoringListenerManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Messages(com.intellij.openapi.ui.Messages) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) DumbService(com.intellij.openapi.project.DumbService) RefactoringTransaction(com.intellij.refactoring.listeners.impl.RefactoringTransaction) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonProjectFileWritingAccessProvider(com.intellij.openapi.fileEditor.impl.NonProjectFileWritingAccessProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable) ApplicationImpl(com.intellij.openapi.application.impl.ApplicationImpl) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) LocalHistory(com.intellij.history.LocalHistory) NotNull(org.jetbrains.annotations.NotNull) Factory(com.intellij.openapi.util.Factory) Ref(com.intellij.openapi.util.Ref) java.util(java.util) RefactoringListenerManagerImpl(com.intellij.refactoring.listeners.impl.RefactoringListenerManagerImpl) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) StatusBarUtil(com.intellij.openapi.wm.impl.status.StatusBarUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DataManager(com.intellij.ide.DataManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ThrowableRunnable(com.intellij.util.ThrowableRunnable) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.usages(com.intellij.usages) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) GuiUtils(com.intellij.ui.GuiUtils) UsageViewUtil(com.intellij.usageView.UsageViewUtil) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) TestOnly(org.jetbrains.annotations.TestOnly) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UndoManager(com.intellij.openapi.command.undo.UndoManager) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) TransactionGuard(com.intellij.openapi.application.TransactionGuard) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) Processor(com.intellij.util.Processor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Factory(com.intellij.openapi.util.Factory) NotNull(org.jetbrains.annotations.NotNull) ThrowableRunnable(com.intellij.util.ThrowableRunnable) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 7 with PsiElement2UsageTargetAdapter

use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.

the class FindUtil method showInUsageView.

@Nullable
public static UsageView showInUsageView(@Nullable PsiElement sourceElement, @NotNull PsiElement[] targets, @NotNull String title, @NotNull final Project project) {
    if (targets.length == 0)
        return null;
    final UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setCodeUsagesString(title);
    presentation.setTabName(title);
    presentation.setTabText(title);
    UsageTarget[] usageTargets = sourceElement == null ? UsageTarget.EMPTY_ARRAY : new UsageTarget[] { new PsiElement2UsageTargetAdapter(sourceElement) };
    PsiElement[] primary = sourceElement == null ? PsiElement.EMPTY_ARRAY : new PsiElement[] { sourceElement };
    UsageView view = UsageViewManager.getInstance(project).showUsages(usageTargets, Usage.EMPTY_ARRAY, presentation);
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(project);
    List<SmartPsiElementPointer> pointers = ContainerUtil.map(targets, smartPointerManager::createSmartPsiElementPointer);
    // usage view will load document/AST so still referencing all these PSI elements might lead to out of memory
    //noinspection UnusedAssignment
    targets = PsiElement.EMPTY_ARRAY;
    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Updating Usage View ...") {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            for (final SmartPsiElementPointer pointer : pointers) {
                if (((UsageViewImpl) view).isDisposed())
                    break;
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement target = pointer.getElement();
                    if (target != null) {
                        view.appendUsage(UsageInfoToUsageConverter.convert(primary, new UsageInfo(target)));
                    }
                });
            }
            UIUtil.invokeLaterIfNeeded(((UsageViewImpl) view)::expandAll);
        }
    });
    return view;
}
Also used : Task(com.intellij.openapi.progress.Task) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) UsageViewImpl(com.intellij.usages.impl.UsageViewImpl) UsageInfo(com.intellij.usageView.UsageInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with PsiElement2UsageTargetAdapter

use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.

the class InheritanceToDelegationProcessor method showObjectUpcastedUsageView.

private void showObjectUpcastedUsageView(final ObjectUpcastedUsageInfo[] usages) {
    UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setTargetsNodeText(RefactoringBundle.message("replacing.inheritance.with.delegation"));
    presentation.setCodeUsagesString(RefactoringBundle.message("instances.casted.to.java.lang.object"));
    final String upcastedString = RefactoringBundle.message("instances.upcasted.to.object");
    presentation.setUsagesString(upcastedString);
    presentation.setTabText(upcastedString);
    UsageViewManager manager = UsageViewManager.getInstance(myProject);
    manager.showUsages(new UsageTarget[] { new PsiElement2UsageTargetAdapter(myClass) }, UsageInfoToUsageConverter.convert(new PsiElement[] { myClass }, usages), presentation);
    WindowManager.getInstance().getStatusBar(myProject).setInfo(RefactoringBundle.message("instances.upcasted.to.java.lang.object.found"));
}
Also used : UsageViewPresentation(com.intellij.usages.UsageViewPresentation) UsageViewManager(com.intellij.usages.UsageViewManager) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)

Example 9 with PsiElement2UsageTargetAdapter

use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.

the class ResourceBundleStructureViewComponent method getData.

@Override
public Object getData(final String dataId) {
    if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        return new ResourceBundleAsVirtualFile(myResourceBundle);
    } else if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
        return getFileEditor();
    } else if (ResourceBundle.ARRAY_DATA_KEY.is(dataId)) {
        return new ResourceBundle[] { myResourceBundle };
    } else if (IProperty.ARRAY_KEY.is(dataId)) {
        final Collection<ResourceBundleEditorViewElement> selectedElements = ((ResourceBundleEditor) getFileEditor()).getSelectedElements();
        if (selectedElements.isEmpty()) {
            return null;
        } else if (selectedElements.size() == 1) {
            return ContainerUtil.getFirstItem(selectedElements).getProperties();
        } else {
            return ContainerUtil.toArray(ContainerUtil.flatten(ContainerUtil.mapNotNull(selectedElements, (NullableFunction<ResourceBundleEditorViewElement, List<IProperty>>) element -> {
                final IProperty[] properties = element.getProperties();
                return properties == null ? null : ContainerUtil.newArrayList(properties);
            })), IProperty[]::new);
        }
    } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
        final List<PsiElement> elements = new ArrayList<>();
        Collections.addAll(elements, getSelectedPsiFiles());
        final IProperty[] properties = (IProperty[]) getData(IProperty.ARRAY_KEY.getName());
        if (properties != null) {
            for (IProperty property : properties) {
                final PsiElement element = property.getPsiElement();
                if (element.isValid()) {
                    elements.add(element);
                }
            }
        }
        return elements.toArray(new PsiElement[elements.size()]);
    } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        if (getSelectedPsiFiles().length != 0) {
            return new ResourceBundleDeleteProvider();
        }
        final IProperty[] properties = IProperty.ARRAY_KEY.getData(this);
        if (properties != null && properties.length != 0) {
            return new PropertiesDeleteProvider(((ResourceBundleEditor) getFileEditor()).getPropertiesInsertDeleteManager(), properties);
        }
    } else if (UsageView.USAGE_TARGETS_KEY.is(dataId)) {
        final PsiElement[] chosenElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
        if (chosenElements != null) {
            final UsageTarget[] usageTargets = new UsageTarget[chosenElements.length];
            for (int i = 0; i < chosenElements.length; i++) {
                usageTargets[i] = new PsiElement2UsageTargetAdapter(chosenElements[i]);
            }
            return usageTargets;
        }
    } else if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) {
        return new CopyProvider() {

            @Override
            public void performCopy(@NotNull final DataContext dataContext) {
                final PsiElement[] selectedPsiElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
                if (selectedPsiElements != null) {
                    final List<String> names = new ArrayList<>(selectedPsiElements.length);
                    for (final PsiElement element : selectedPsiElements) {
                        if (element instanceof PsiNamedElement) {
                            names.add(((PsiNamedElement) element).getName());
                        }
                    }
                    CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
                }
            }

            @Override
            public boolean isCopyEnabled(@NotNull final DataContext dataContext) {
                return true;
            }

            @Override
            public boolean isCopyVisible(@NotNull final DataContext dataContext) {
                return true;
            }
        };
    }
    return super.getData(dataId);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PopupHandler(com.intellij.ui.PopupHandler) NonNls(org.jetbrains.annotations.NonNls) ResourceBundleEditorRenderer(com.intellij.lang.properties.editor.inspections.ResourceBundleEditorRenderer) ContainerUtil(com.intellij.util.containers.ContainerUtil) DeleteProvider(com.intellij.ide.DeleteProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) StringSelection(java.awt.datatransfer.StringSelection) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PsiNamedElement(com.intellij.psi.PsiNamedElement) SafeDeleteHandler(com.intellij.refactoring.safeDelete.SafeDeleteHandler) Logger(com.intellij.openapi.diagnostic.Logger) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) UsageTarget(com.intellij.usages.UsageTarget) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) UsageView(com.intellij.usages.UsageView) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) CopyProvider(com.intellij.ide.CopyProvider) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) ContextHelpAction(com.intellij.ide.actions.ContextHelpAction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) CopyPasteManager(com.intellij.openapi.ide.CopyPasteManager) CopyProvider(com.intellij.ide.CopyProvider) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) StringSelection(java.awt.datatransfer.StringSelection) IProperty(com.intellij.lang.properties.IProperty) ArrayList(java.util.ArrayList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Collection(java.util.Collection) ResourceBundle(com.intellij.lang.properties.ResourceBundle)

Aggregations

PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)9 PsiElement (com.intellij.psi.PsiElement)6 PsiFile (com.intellij.psi.PsiFile)4 UsageInfo (com.intellij.usageView.UsageInfo)4 UsageTarget (com.intellij.usages.UsageTarget)3 Logger (com.intellij.openapi.diagnostic.Logger)2 Project (com.intellij.openapi.project.Project)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1 LocalHistory (com.intellij.history.LocalHistory)1 LocalHistoryAction (com.intellij.history.LocalHistoryAction)1 CopyProvider (com.intellij.ide.CopyProvider)1 DataManager (com.intellij.ide.DataManager)1 DeleteProvider (com.intellij.ide.DeleteProvider)1 ContextHelpAction (com.intellij.ide.actions.ContextHelpAction)1 Language (com.intellij.lang.Language)1 IProperty (com.intellij.lang.properties.IProperty)1