@Nullable private static PsiElement getSelectedPsiElement( final DataContext dataContext, final Project project) { PsiElement element = null; final Editor editor = CommonDataKeys.EDITOR.getData(dataContext); if (editor != null) { final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (psiFile != null) { final int offset = editor.getCaretModel().getOffset(); element = psiFile.findElementAt(offset); if (element == null && offset > 0 && offset == psiFile.getTextLength()) { element = psiFile.findElementAt(offset - 1); } } } if (element == null) { final PsiElement[] elements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext); element = elements != null && elements.length > 0 ? elements[0] : null; } if (element == null) { final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); if (files != null && files.length > 0) { element = PsiManager.getInstance(project).findFile(files[0]); } } return element; }
@Nullable public Object getData(String dataId) { if (CommonDataKeys.PROJECT.is(dataId)) { return editor != null ? editor.getProject() : null; } else if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) { return editor != null ? editor.getFile() : null; } else if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) { return editor != null ? new VirtualFile[] {editor.getFile()} : new VirtualFile[] {}; } else if (CommonDataKeys.PSI_FILE.is(dataId)) { return getData(CommonDataKeys.PSI_ELEMENT.getName()); } else if (CommonDataKeys.PSI_ELEMENT.is(dataId)) { VirtualFile file = editor != null ? editor.getFile() : null; return file != null && file.isValid() ? PsiManager.getInstance(editor.getProject()).findFile(file) : null; } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) { return editor != null ? new PsiElement[] {(PsiElement) getData(CommonDataKeys.PSI_ELEMENT.getName())} : new PsiElement[] {}; } else if (PlatformDataKeys.COPY_PROVIDER.is(dataId) && copyPasteSupport != null) { return this; } else if (PlatformDataKeys.CUT_PROVIDER.is(dataId) && copyPasteSupport != null) { return copyPasteSupport.getCutProvider(); } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) { return deleteProvider; } else if (ImageComponentDecorator.DATA_KEY.is(dataId)) { return editor != null ? editor : this; } return null; }
static VirtualFile[] getFiles(DataContext dataContext) { ArrayList<VirtualFile> filesList = new ArrayList<VirtualFile>(); VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); for (int i = 0; files != null && i < files.length; i++) { VirtualFile file = files[i]; if (file.isInLocalFileSystem()) { filesList.add(file); } } return VfsUtil.toVirtualFileArray(filesList); }
public Object getData(String dataId) { if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) { return myFileSystemTree.getSelectedFiles(); } else if (PATH_FIELD.is(dataId)) { return new PathField() { public void toggleVisible() { toggleShowTextField(); } }; } else if (FileSystemTree.DATA_KEY.is(dataId)) { return myFileSystemTree; } return myChooserDescriptor.getUserData(dataId); }
@Nullable private static SearchScope getSelectedFilesScope( final Project project, @Nullable DataContext dataContext) { final VirtualFile[] filesOrDirs = dataContext == null ? null : CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); if (filesOrDirs != null) { final List<VirtualFile> selectedFiles = ContainerUtil.filter(filesOrDirs, file -> !file.isDirectory()); if (!selectedFiles.isEmpty()) { return GlobalSearchScope.filesScope(project, selectedFiles, "Selected Files"); } } return null; }
public static Collection<ErlangFile> findFileTestElements( Project project, DataContext dataContext) { VirtualFile[] selectedFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); if (selectedFiles == null) return Collections.emptyList(); List<ErlangFile> testFiles = new ArrayList<ErlangFile>(selectedFiles.length); PsiManager psiManager = PsiManager.getInstance(project); for (VirtualFile file : selectedFiles) { if (!file.isDirectory()) { ContainerUtil.addIfNotNull(testFiles, getFileTestElement(psiManager.findFile(file))); continue; } PsiDirectory directory = psiManager.findDirectory(file); PsiFile[] children = directory == null ? new PsiFile[0] : directory.getFiles(); for (PsiFile psiFile : children) { ContainerUtil.addIfNotNull(testFiles, getFileTestElement(psiFile)); } } return testFiles; }
public static List<SearchScope> getPredefinedScopes( @NotNull final Project project, @Nullable final DataContext dataContext, boolean suggestSearchInLibs, boolean prevSearchFiles, boolean currentSelection, boolean usageView) { ArrayList<SearchScope> result = new ArrayList<SearchScope>(); result.add(GlobalSearchScope.projectScope(project)); if (suggestSearchInLibs) { result.add(GlobalSearchScope.allScope(project)); } if (!PlatformUtils.isCidr() && ModuleUtil.isSupportedRootType( project, JavaSourceRootType.TEST_SOURCE)) { // TODO: fix these scopes in AppCode result.add(GlobalSearchScopes.projectProductionScope(project)); result.add(GlobalSearchScopes.projectTestScope(project)); } result.add(GlobalSearchScopes.openFilesScope(project)); if (dataContext != null) { PsiElement dataContextElement = CommonDataKeys.PSI_FILE.getData(dataContext); if (dataContextElement == null) { dataContextElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext); } 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) { result.add(module.getModuleScope()); } } if (dataContextElement.getContainingFile() != null) { result.add( new LocalSearchScope(dataContextElement, IdeBundle.message("scope.current.file"))); } } } if (currentSelection) { FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); final Editor selectedTextEditor = fileEditorManager.getSelectedTextEditor(); if (selectedTextEditor != null) { final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(selectedTextEditor.getDocument()); if (psiFile != null) { if (selectedTextEditor.getSelectionModel().hasSelection()) { final PsiElement startElement = psiFile.findElementAt(selectedTextEditor.getSelectionModel().getSelectionStart()); if (startElement != null) { final PsiElement endElement = psiFile.findElementAt(selectedTextEditor.getSelectionModel().getSelectionEnd()); if (endElement != null) { final PsiElement parent = PsiTreeUtil.findCommonParent(startElement, endElement); if (parent != null) { final List<PsiElement> elements = new ArrayList<PsiElement>(); final PsiElement[] children = parent.getChildren(); for (PsiElement child : children) { if (!(child instanceof PsiWhiteSpace) && child.getContainingFile() != null) { elements.add(child); } } if (!elements.isEmpty()) { SearchScope local = new LocalSearchScope( PsiUtilCore.toPsiElementArray(elements), IdeBundle.message("scope.selection")); result.add(local); } } } } } } } } if (usageView) { UsageView selectedUsageView = UsageViewManager.getInstance(project).getSelectedUsageView(); if (selectedUsageView != null && !selectedUsageView.isSearchInProgress()) { final Set<Usage> usages = selectedUsageView.getUsages(); final List<PsiElement> results = new ArrayList<PsiElement>(usages.size()); if (prevSearchFiles) { final Set<VirtualFile> files = new HashSet<VirtualFile>(); for (Usage usage : usages) { if (usage instanceof PsiElementUsage) { PsiElement psiElement = ((PsiElementUsage) usage).getElement(); if (psiElement != null && psiElement.isValid()) { PsiFile psiFile = psiElement.getContainingFile(); if (psiFile != null) { VirtualFile file = psiFile.getVirtualFile(); if (file != null) files.add(file); } } } } if (!files.isEmpty()) { GlobalSearchScope prev = new GlobalSearchScope(project) { @Override public String getDisplayName() { return IdeBundle.message("scope.files.in.previous.search.result"); } @Override public boolean contains(@NotNull VirtualFile file) { return files.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 { 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); if (rootUrls.isEmpty()) continue; // ignore unused root result.add( new GlobalSearchScope(project) { @Override public String getDisplayName() { return "Favorite \'" + favorite + "\'"; } @Override public boolean contains(@NotNull final VirtualFile file) { return 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; } }); } } if (dataContext != null) { final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); if (files != null) { final List<VirtualFile> openFiles = Arrays.asList(files); result.add( new DelegatingGlobalSearchScope(GlobalSearchScope.filesScope(project, openFiles)) { @Override public String getDisplayName() { return "Selected Files"; } }); } } return result; }