Search in sources :

Example 6 with PsiElementUsage

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

the class MethodGroupingRule method groupUsage.

@Override
public UsageGroup groupUsage(@NotNull Usage usage) {
    if (!(usage instanceof PsiElementUsage))
        return null;
    PsiElement psiElement = ((PsiElementUsage) usage).getElement();
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null)
        return null;
    InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject());
    PsiFile topLevelFile = manager.getTopLevelFile(containingFile);
    if (topLevelFile instanceof PsiJavaFile) {
        PsiElement containingMethod = topLevelFile == containingFile ? psiElement : manager.getInjectionHost(containingFile);
        if (usage instanceof UsageInfo2UsageAdapter && topLevelFile == containingFile) {
            int offset = ((UsageInfo2UsageAdapter) usage).getUsageInfo().getNavigationOffset();
            containingMethod = containingFile.findElementAt(offset);
        }
        do {
            containingMethod = PsiTreeUtil.getParentOfType(containingMethod, PsiMethod.class, true);
            if (containingMethod == null)
                break;
            final PsiClass containingClass = ((PsiMethod) containingMethod).getContainingClass();
            if (containingClass == null || containingClass.getQualifiedName() != null)
                break;
        } while (true);
        if (containingMethod != null) {
            return new MethodUsageGroup((PsiMethod) containingMethod);
        }
    }
    return null;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Example 7 with PsiElementUsage

use of com.intellij.usages.rules.PsiElementUsage in project intellij-plugins by JetBrains.

the class ImportFilteringRule method isVisible.

public boolean isVisible(@NotNull Usage usage, @NotNull UsageTarget[] targets) {
    if (usage instanceof PsiElementUsage) {
        final PsiElement psiElement = ((PsiElementUsage) usage).getElement();
        final PsiFile containingFile = psiElement.getContainingFile();
        if (containingFile != null && containingFile.getLanguage().is(JavaScriptSupportLoader.ECMA_SCRIPT_L4)) {
            return PsiTreeUtil.getParentOfType(psiElement, JSImportStatement.class, true) == null;
        }
    }
    return true;
}
Also used : JSImportStatement(com.intellij.lang.javascript.psi.ecmal4.JSImportStatement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Example 8 with PsiElementUsage

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

the class PredefinedSearchScopeProviderImpl method getPredefinedScopes.

@NotNull
@Override
public List<SearchScope> getPredefinedScopes(@NotNull final Project project, @Nullable final DataContext dataContext, boolean suggestSearchInLibs, boolean prevSearchFiles, boolean currentSelection, boolean usageView, boolean showEmptyScopes) {
    Collection<SearchScope> result = ContainerUtil.newLinkedHashSet();
    result.add(GlobalSearchScope.everythingScope(project));
    result.add(GlobalSearchScope.projectScope(project));
    if (suggestSearchInLibs) {
        result.add(GlobalSearchScope.allScope(project));
    }
    if (ModuleUtil.isSupportedRootType(project, JavaSourceRootType.TEST_SOURCE)) {
        result.add(GlobalSearchScopesCore.projectProductionScope(project));
        result.add(GlobalSearchScopesCore.projectTestScope(project));
    }
    result.add(ScratchFileServiceImpl.buildScratchesSearchScope());
    final GlobalSearchScope openFilesScope = GlobalSearchScopes.openFilesScope(project);
    if (openFilesScope != GlobalSearchScope.EMPTY_SCOPE) {
        result.add(openFilesScope);
    } else if (showEmptyScopes) {
        result.add(new LocalSearchScope(PsiElement.EMPTY_ARRAY, IdeBundle.message("scope.open.files")));
    }
    final Editor selectedTextEditor = ApplicationManager.getApplication().isDispatchThread() ? FileEditorManager.getInstance(project).getSelectedTextEditor() : null;
    PsiFile psiFile = selectedTextEditor == null ? null : PsiDocumentManager.getInstance(project).getPsiFile(selectedTextEditor.getDocument());
    PsiFile currentFile = psiFile;
    if (dataContext != null) {
        PsiElement dataContextElement = CommonDataKeys.PSI_FILE.getData(dataContext);
        if (dataContextElement == null) {
            dataContextElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
        }
        if (dataContextElement == null && psiFile != null) {
            dataContextElement = psiFile;
        }
        if (dataContextElement != null) {
            if (!PlatformUtils.isCidr()) {
                // TODO: have an API to disable module scopes.
                Module module = ModuleUtilCore.findModuleForPsiElement(dataContextElement);
                if (module == null) {
                    module = LangDataKeys.MODULE.getData(dataContext);
                }
                if (module != null && !(ModuleType.get(module) instanceof InternalModuleType)) {
                    result.add(module.getModuleScope());
                }
            }
            if (currentFile == null) {
                currentFile = dataContextElement.getContainingFile();
            }
        }
    }
    if (currentFile != null || showEmptyScopes) {
        PsiElement[] scope = currentFile != null ? new PsiElement[] { currentFile } : PsiElement.EMPTY_ARRAY;
        result.add(new LocalSearchScope(scope, IdeBundle.message("scope.current.file")));
    }
    if (currentSelection && selectedTextEditor != null && psiFile != null) {
        SelectionModel selectionModel = selectedTextEditor.getSelectionModel();
        if (selectionModel.hasSelection()) {
            int start = selectionModel.getSelectionStart();
            final PsiElement startElement = psiFile.findElementAt(start);
            if (startElement != null) {
                int end = selectionModel.getSelectionEnd();
                final PsiElement endElement = psiFile.findElementAt(end);
                if (endElement != null) {
                    final PsiElement parent = PsiTreeUtil.findCommonParent(startElement, endElement);
                    if (parent != null) {
                        final List<PsiElement> elements = new ArrayList<>();
                        final PsiElement[] children = parent.getChildren();
                        TextRange selection = new TextRange(start, end);
                        for (PsiElement child : children) {
                            if (!(child instanceof PsiWhiteSpace) && child.getContainingFile() != null && selection.contains(child.getTextOffset())) {
                                elements.add(child);
                            }
                        }
                        if (!elements.isEmpty()) {
                            SearchScope local = new LocalSearchScope(PsiUtilCore.toPsiElementArray(elements), IdeBundle.message("scope.selection"));
                            result.add(local);
                        }
                    }
                }
            }
        }
    }
    if (usageView) {
        addHierarchyScope(project, result);
        UsageView selectedUsageView = UsageViewManager.getInstance(project).getSelectedUsageView();
        if (selectedUsageView != null && !selectedUsageView.isSearchInProgress()) {
            final Set<Usage> usages = ContainerUtil.newTroveSet(selectedUsageView.getUsages());
            usages.removeAll(selectedUsageView.getExcludedUsages());
            if (prevSearchFiles) {
                final Set<VirtualFile> files = collectFiles(usages, true);
                if (!files.isEmpty()) {
                    GlobalSearchScope prev = new GlobalSearchScope(project) {

                        private Set<VirtualFile> myFiles;

                        @NotNull
                        @Override
                        public String getDisplayName() {
                            return IdeBundle.message("scope.files.in.previous.search.result");
                        }

                        @Override
                        public synchronized boolean contains(@NotNull VirtualFile file) {
                            if (myFiles == null) {
                                myFiles = collectFiles(usages, false);
                            }
                            return myFiles.contains(file);
                        }

                        @Override
                        public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
                            return 0;
                        }

                        @Override
                        public boolean isSearchInModuleContent(@NotNull Module aModule) {
                            return true;
                        }

                        @Override
                        public boolean isSearchInLibraries() {
                            return true;
                        }
                    };
                    result.add(prev);
                }
            } else {
                final List<PsiElement> results = new ArrayList<>(usages.size());
                for (Usage usage : usages) {
                    if (usage instanceof PsiElementUsage) {
                        final PsiElement element = ((PsiElementUsage) usage).getElement();
                        if (element != null && element.isValid() && element.getContainingFile() != null) {
                            results.add(element);
                        }
                    }
                }
                if (!results.isEmpty()) {
                    result.add(new LocalSearchScope(PsiUtilCore.toPsiElementArray(results), IdeBundle.message("scope.previous.search.results")));
                }
            }
        }
    }
    final FavoritesManager favoritesManager = FavoritesManager.getInstance(project);
    if (favoritesManager != null) {
        for (final String favorite : favoritesManager.getAvailableFavoritesListNames()) {
            final Collection<TreeItem<Pair<AbstractUrl, String>>> rootUrls = favoritesManager.getFavoritesListRootUrls(favorite);
            // ignore unused root
            if (rootUrls.isEmpty())
                continue;
            result.add(new GlobalSearchScope(project) {

                @NotNull
                @Override
                public String getDisplayName() {
                    return "Favorite \'" + favorite + "\'";
                }

                @Override
                public boolean contains(@NotNull final VirtualFile file) {
                    return ReadAction.compute(() -> favoritesManager.contains(favorite, file));
                }

                @Override
                public int compare(@NotNull final VirtualFile file1, @NotNull final VirtualFile file2) {
                    return 0;
                }

                @Override
                public boolean isSearchInModuleContent(@NotNull final Module aModule) {
                    return true;
                }

                @Override
                public boolean isSearchInLibraries() {
                    return true;
                }
            });
        }
    }
    ContainerUtil.addIfNotNull(result, getSelectedFilesScope(project, dataContext));
    return ContainerUtil.newArrayList(result);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TreeItem(com.intellij.util.TreeItem) NotNull(org.jetbrains.annotations.NotNull) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) UsageView(com.intellij.usages.UsageView) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) FavoritesManager(com.intellij.ide.favoritesTreeView.FavoritesManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Usage(com.intellij.usages.Usage) AbstractUrl(com.intellij.ide.projectView.impl.AbstractUrl) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PsiElementUsage

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

the class UsageScopeGroupingRule method groupUsage.

@Override
public UsageGroup groupUsage(@NotNull Usage usage) {
    if (!(usage instanceof PsiElementUsage)) {
        return null;
    }
    PsiElementUsage elementUsage = (PsiElementUsage) usage;
    PsiElement element = elementUsage.getElement();
    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(element);
    if (virtualFile == null) {
        return null;
    }
    Project project = element.getProject();
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    boolean isInLib = fileIndex.isInLibraryClasses(virtualFile) || fileIndex.isInLibrarySource(virtualFile);
    if (isInLib)
        return LIBRARY;
    return TestSourcesFilter.isTestSources(virtualFile, project) ? TEST : PRODUCTION;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiElement(com.intellij.psi.PsiElement) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Example 10 with PsiElementUsage

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

the class UsageTypeGroupingRule method groupUsage.

@Override
public UsageGroup groupUsage(@NotNull Usage usage, @NotNull UsageTarget[] targets) {
    if (usage instanceof PsiElementUsage) {
        PsiElementUsage elementUsage = (PsiElementUsage) usage;
        PsiElement element = elementUsage.getElement();
        UsageType usageType = getUsageType(element, targets);
        if (usageType == null && element instanceof PsiFile && elementUsage instanceof UsageInfo2UsageAdapter) {
            usageType = ((UsageInfo2UsageAdapter) elementUsage).getUsageType();
        }
        if (usageType != null)
            return new UsageTypeGroup(usageType);
        if (usage instanceof ReadWriteAccessUsage) {
            ReadWriteAccessUsage u = (ReadWriteAccessUsage) usage;
            if (u.isAccessedForWriting())
                return new UsageTypeGroup(UsageType.WRITE);
            if (u.isAccessedForReading())
                return new UsageTypeGroup(UsageType.READ);
        }
        return new UsageTypeGroup(UsageType.UNCLASSIFIED);
    }
    return null;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Aggregations

PsiElementUsage (com.intellij.usages.rules.PsiElementUsage)11 PsiElement (com.intellij.psi.PsiElement)8 PsiFile (com.intellij.psi.PsiFile)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 NotNull (org.jetbrains.annotations.NotNull)3 UsageInfo (com.intellij.usageView.UsageInfo)2 Usage (com.intellij.usages.Usage)2 CustomUsageSearcher (com.intellij.find.findUsages.CustomUsageSearcher)1 FindUsagesOptions (com.intellij.find.findUsages.FindUsagesOptions)1 FavoritesManager (com.intellij.ide.favoritesTreeView.FavoritesManager)1 AbstractUrl (com.intellij.ide.projectView.impl.AbstractUrl)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 JSImportStatement (com.intellij.lang.javascript.psi.ecmal4.JSImportStatement)1 Editor (com.intellij.openapi.editor.Editor)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 Project (com.intellij.openapi.project.Project)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)1