Search in sources :

Example 6 with UsageTarget

use of com.intellij.usages.UsageTarget in project android by JetBrains.

the class AndroidFindUsagesTest method findUsages.

private static Collection<UsageInfo> findUsages(String fileName, final JavaCodeInsightTestFixture fixture, String newFilePath) throws Throwable {
    VirtualFile file = fixture.copyFileToProject(BASE_PATH + fileName, newFilePath);
    fixture.configureFromExistingVirtualFile(file);
    final UsageTarget[] targets = UsageTargetUtil.findUsageTargets(new DataProvider() {

        @Override
        public Object getData(@NonNls String dataId) {
            return ((EditorEx) fixture.getEditor()).getDataContext().getData(dataId);
        }
    });
    assert targets != null && targets.length > 0 && targets[0] instanceof PsiElementUsageTarget;
    return fixture.findUsages(((PsiElementUsageTarget) targets[0]).getElement());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DataProvider(com.intellij.openapi.actionSystem.DataProvider) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiElementUsageTarget(com.intellij.usages.PsiElementUsageTarget) UsageTarget(com.intellij.usages.UsageTarget) PsiElementUsageTarget(com.intellij.usages.PsiElementUsageTarget)

Example 7 with UsageTarget

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

the class HighlightUsagesHandler method invoke.

public static void invoke(@NotNull final Project project, @NotNull final Editor editor, final PsiFile file) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (file == null && !selectionModel.hasSelection()) {
        selectionModel.selectWordAtCaret(false);
    }
    if (file == null || selectionModel.hasSelection()) {
        doRangeHighlighting(editor, project);
        return;
    }
    final HighlightUsagesHandlerBase handler = createCustomHandler(editor, file);
    if (handler != null) {
        final String featureId = handler.getFeatureId();
        if (featureId != null) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(featureId);
        }
        handler.highlightUsages();
        return;
    }
    DumbService.getInstance(project).withAlternativeResolveEnabled(() -> {
        UsageTarget[] usageTargets = getUsageTargets(editor, file);
        if (usageTargets == null) {
            handleNoUsageTargets(file, editor, selectionModel, project);
            return;
        }
        boolean clearHighlights = isClearHighlights(editor);
        for (UsageTarget target : usageTargets) {
            target.highlightUsages(file, editor, clearHighlights);
        }
    });
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) UsageTarget(com.intellij.usages.UsageTarget)

Example 8 with UsageTarget

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

the class FindUsagesInFileAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return;
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    UsageTarget[] usageTargets = UsageView.USAGE_TARGETS_KEY.getData(dataContext);
    if (usageTargets != null) {
        FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(dataContext);
        if (fileEditor != null) {
            usageTargets[0].findUsagesInEditor(fileEditor);
        }
    } else if (editor == null) {
        Messages.showMessageDialog(project, FindBundle.message("find.no.usages.at.cursor.error"), CommonBundle.getErrorTitle(), Messages.getErrorIcon());
    } else {
        HintManager.getInstance().showErrorHint(editor, FindBundle.message("find.no.usages.at.cursor.error"));
    }
}
Also used : Project(com.intellij.openapi.project.Project) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) UsageTarget(com.intellij.usages.UsageTarget)

Example 9 with UsageTarget

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

the class VirtualFileArrayRule method getData.

@Override
public Object getData(final DataProvider dataProvider) {
    // Try to detect multiselection.
    Set<VirtualFile> result = null;
    FileSystemTree fileSystemTree = FileSystemTree.DATA_KEY.getData(dataProvider);
    if (fileSystemTree != null) {
        result = addFiles(result, fileSystemTree.getSelectedFiles());
    }
    Project project = PlatformDataKeys.PROJECT_CONTEXT.getData(dataProvider);
    if (project != null && !project.isDisposed()) {
        result = addFiles(result, ProjectRootManager.getInstance(project).getContentRoots());
    }
    Module[] selectedModules = LangDataKeys.MODULE_CONTEXT_ARRAY.getData(dataProvider);
    if (selectedModules != null && selectedModules.length > 0) {
        for (Module selectedModule : selectedModules) {
            result = addFiles(result, ModuleRootManager.getInstance(selectedModule).getContentRoots());
        }
    }
    Module selectedModule = LangDataKeys.MODULE_CONTEXT.getData(dataProvider);
    if (selectedModule != null && !selectedModule.isDisposed()) {
        result = addFiles(result, ModuleRootManager.getInstance(selectedModule).getContentRoots());
    }
    PsiElement[] psiElements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataProvider);
    if (psiElements != null) {
        for (PsiElement element : psiElements) {
            result = addFilesFromPsiElement(result, element);
        }
    }
    result = addFile(result, CommonDataKeys.VIRTUAL_FILE.getData(dataProvider));
    PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(dataProvider);
    if (psiFile != null) {
        result = addFile(result, psiFile.getVirtualFile());
    }
    if (result != null) {
        return VfsUtilCore.toVirtualFileArray(result);
    }
    PsiElement elem = CommonDataKeys.PSI_ELEMENT.getData(dataProvider);
    if (elem != null) {
        result = addFilesFromPsiElement(result, elem);
    }
    Usage[] usages = UsageView.USAGES_KEY.getData(dataProvider);
    UsageTarget[] usageTargets = UsageView.USAGE_TARGETS_KEY.getData(dataProvider);
    if (usages != null || usageTargets != null) {
        for (VirtualFile file : UsageDataUtil.provideVirtualFileArray(usages, usageTargets)) {
            result = addFile(result, file);
        }
    }
    if (result == null) {
        final Object[] objects = (Object[]) dataProvider.getData(PlatformDataKeys.SELECTED_ITEMS.getName());
        if (objects != null) {
            final VirtualFile[] files = new VirtualFile[objects.length];
            for (int i = 0, objectsLength = objects.length; i < objectsLength; i++) {
                Object object = objects[i];
                if (!(object instanceof VirtualFile))
                    return null;
                files[i] = (VirtualFile) object;
            }
            return files;
        }
        return null;
    } else {
        return VfsUtilCore.toVirtualFileArray(result);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Usage(com.intellij.usages.Usage) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) FileSystemTree(com.intellij.openapi.fileChooser.FileSystemTree) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget)

Example 10 with UsageTarget

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

the class ThrowsUsageTargetProvider method getTargets.

@Override
@Nullable
public UsageTarget[] getTargets(Editor editor, final PsiFile file) {
    if (editor == null || file == null)
        return null;
    PsiElement element = file.findElementAt(TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset()));
    if (element == null)
        return null;
    if (element instanceof PsiKeyword && PsiKeyword.THROWS.equals(element.getText())) {
        return new UsageTarget[] { new PsiElement2UsageTargetAdapter(element) };
    }
    final PsiElement parent = element.getParent();
    if (parent instanceof PsiThrowStatement) {
        return new UsageTarget[] { new PsiElement2UsageTargetAdapter(parent) };
    }
    return null;
}
Also used : PsiThrowStatement(com.intellij.psi.PsiThrowStatement) PsiKeyword(com.intellij.psi.PsiKeyword) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget) Nullable(org.jetbrains.annotations.Nullable)

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