Search in sources :

Example 11 with UsageTarget

use of com.intellij.usages.UsageTarget in project intellij-community by JetBrains.

the class UsageViewManagerTest method testScopeCreatedForFindInModuleContent.

public void testScopeCreatedForFindInModuleContent() {
    FindModel findModel = new FindModel();
    findModel.setModuleName(getModule().getName());
    findModel.setProjectScope(false);
    UsageTarget target = new FindInProjectUtil.StringUsageTarget(getProject(), findModel);
    UsageViewManagerImpl manager = (UsageViewManagerImpl) UsageViewManager.getInstance(getProject());
    SearchScope scope = manager.getMaxSearchScopeToWarnOfFallingOutOf(new UsageTarget[] { target });
    assertEquals(scope, getModule().getModuleContentScope());
}
Also used : FindModel(com.intellij.find.FindModel) SearchScope(com.intellij.psi.search.SearchScope) UsageTarget(com.intellij.usages.UsageTarget)

Example 12 with UsageTarget

use of com.intellij.usages.UsageTarget in project intellij-community by JetBrains.

the class FindUsagesAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project == null) {
        return;
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    UsageTarget[] usageTargets = e.getData(UsageView.USAGE_TARGETS_KEY);
    if (usageTargets == null) {
        final Editor editor = e.getData(CommonDataKeys.EDITOR);
        chooseAmbiguousTargetAndPerform(project, editor, element -> {
            startFindUsages(element);
            return false;
        });
    } else {
        UsageTarget target = usageTargets[0];
        if (target instanceof PsiElementUsageTarget) {
            PsiElement element = ((PsiElementUsageTarget) target).getElement();
            if (element != null) {
                startFindUsages(element);
            }
        } else {
            target.findUsages();
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiElementUsageTarget(com.intellij.usages.PsiElementUsageTarget) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget) PsiElementUsageTarget(com.intellij.usages.PsiElementUsageTarget)

Example 13 with UsageTarget

use of com.intellij.usages.UsageTarget in project intellij-community by JetBrains.

the class FindUsagesInFileAction method isEnabled.

private static boolean isEnabled(DataContext dataContext) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null || EditorGutter.KEY.getData(dataContext) != null || Boolean.TRUE.equals(dataContext.getData(CommonDataKeys.EDITOR_VIRTUAL_SPACE))) {
        return false;
    }
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    if (editor == null) {
        UsageTarget[] target = UsageView.USAGE_TARGETS_KEY.getData(dataContext);
        return target != null && target.length > 0;
    } else {
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) {
            return false;
        }
        Language language = PsiUtilBase.getLanguageInEditor(editor, project);
        if (language == null) {
            language = file.getLanguage();
        }
        return !(LanguageFindUsages.INSTANCE.forLanguage(language) instanceof EmptyFindUsagesProvider);
    }
}
Also used : Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) EmptyFindUsagesProvider(com.intellij.lang.findUsages.EmptyFindUsagesProvider) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) UsageTarget(com.intellij.usages.UsageTarget)

Example 14 with UsageTarget

use of com.intellij.usages.UsageTarget 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

UsageTarget (com.intellij.usages.UsageTarget)14 PsiElement (com.intellij.psi.PsiElement)7 Project (com.intellij.openapi.project.Project)5 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)3 Editor (com.intellij.openapi.editor.Editor)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 PsiElementUsageTarget (com.intellij.usages.PsiElementUsageTarget)3 FindModel (com.intellij.find.FindModel)2 DataProvider (com.intellij.openapi.actionSystem.DataProvider)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 PsiReference (com.intellij.psi.PsiReference)2 SearchScope (com.intellij.psi.search.SearchScope)2 ArrayList (java.util.ArrayList)2 CopyProvider (com.intellij.ide.CopyProvider)1 DeleteProvider (com.intellij.ide.DeleteProvider)1 ContextHelpAction (com.intellij.ide.actions.ContextHelpAction)1 Language (com.intellij.lang.Language)1 EmptyFindUsagesProvider (com.intellij.lang.findUsages.EmptyFindUsagesProvider)1 IProperty (com.intellij.lang.properties.IProperty)1