@Nullable private static PsiElement getElementToCopy( @Nullable final Editor editor, final DataContext dataContext) { PsiElement element = null; if (editor != null) { PsiReference reference = TargetElementUtilBase.findReference(editor); if (reference != null) { element = reference.getElement(); } } if (element == null) { element = LangDataKeys.PSI_ELEMENT.getData(dataContext); } if (element == null && editor == null) { VirtualFile virtualFile = PlatformDataKeys.VIRTUAL_FILE.getData(dataContext); Project project = PlatformDataKeys.PROJECT.getData(dataContext); if (virtualFile != null && project != null) { element = PsiManager.getInstance(project).findFile(virtualFile); } } if (element instanceof PsiFile && !((PsiFile) element).getViewProvider().isPhysical()) { return null; } for (QualifiedNameProvider provider : Extensions.getExtensions(QualifiedNameProvider.EP_NAME)) { PsiElement adjustedElement = provider.adjustElementToCopy(element); if (adjustedElement != null) return adjustedElement; } return element; }
public void invoke( @NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); PsiElement element = findTargetMember(file, editor); if (element == null) { element = LangDataKeys.PSI_ELEMENT.getData(dataContext); } invokeOnElement(project, editor, element); }
@Override public Object getData(@NonNls String dataId) { if (LangDataKeys.PSI_ELEMENT.is(dataId)) { final int[] selected = getSelectedRows(); if (selected.length == 1) { return getPsiElementForHint(getValueAt(selected[0], 0)); } } return null; }
public Object getData(String dataId) { PackageDependenciesNode node = getSelectedNode(); if (PlatformDataKeys.NAVIGATABLE.is(dataId)) { return node; } if (LangDataKeys.PSI_ELEMENT.is(dataId) && node != null) { final PsiElement element = node.getPsiElement(); return element != null && element.isValid() ? element : null; } return null; }
@Nullable @NonNls public Object getData(@NonNls String dataId) { if (LangDataKeys.PSI_ELEMENT.is(dataId)) { final PackageDependenciesNode selectedNode = myRightTree.getSelectedNode(); if (selectedNode != null) { final PsiElement element = selectedNode.getPsiElement(); return element != null && element.isValid() ? element : null; } } if (PlatformDataKeys.HELP_ID.is(dataId)) { return "dependency.viewer.tool.window"; } return null; }
@Nullable private static PsiElement[] getPsiElements(DataContext dataContext) { PsiElement[] elements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext); if (elements == null) { final Object data = LangDataKeys.PSI_ELEMENT.getData(dataContext); if (data != null) { elements = new PsiElement[] {(PsiElement) data}; } else { final Object data1 = LangDataKeys.PSI_FILE.getData(dataContext); if (data1 != null) { elements = new PsiElement[] {(PsiFile) data1}; } } } return elements; }
@Nullable private PsiElement getSymbolPsiElement(DataContext dataContext) { final PsiElement firstPsiElement = LangDataKeys.PSI_ELEMENT.getData(dataContext); return SymbolUtils.getSymbolElement(firstPsiElement); // TODO: fix rename for JSON string values // final Caret caret = LangDataKeys.CARET.getData(dataContext); // final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(dataContext); // // if(caret == null || psiFile == null) return null; // // final int caretPosition = caret.getOffset(); // final PsiElement psiElement = psiFile.findElementAt(caretPosition); // // return SymbolUtils.getSymbolElement(psiElement); }
public Object getData(String dataId) { if (LangDataKeys.PSI_ELEMENT.is(dataId)) { TreePath path = getSelectedUniquePath(); if (path == null) return null; DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); Object userObject = node.getUserObject(); if (!(userObject instanceof AbstractTreeNode)) return null; AbstractTreeNode descriptor = (AbstractTreeNode) userObject; Object element = descriptor.getValue(); if (element instanceof StructureViewTreeElement) { element = ((StructureViewTreeElement) element).getValue(); } if (!(element instanceof PsiElement)) return null; if (!((PsiElement) element).isValid()) return null; return element; } if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) { return convertToPsiElementsArray(getSelectedElements()); } if (PlatformDataKeys.FILE_EDITOR.is(dataId)) { return myFileEditor; } if (PlatformDataKeys.CUT_PROVIDER.is(dataId)) { return myCopyPasteDelegator.getCutProvider(); } if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) { return myCopyPasteDelegator.getCopyProvider(); } if (PlatformDataKeys.PASTE_PROVIDER.is(dataId)) { return myCopyPasteDelegator.getPasteProvider(); } if (PlatformDataKeys.NAVIGATABLE.is(dataId)) { Object[] selectedElements = getSelectedTreeElements(); if (selectedElements == null || selectedElements.length == 0) return null; if (selectedElements[0] instanceof Navigatable) { return selectedElements[0]; } } if (PlatformDataKeys.HELP_ID.is(dataId)) { return getHelpID(); } return super.getData(dataId); }
@Nullable private static PsiMethod getMethodImpl(final DataContext dataContext) { final Project project = PlatformDataKeys.PROJECT.getData(dataContext); if (project == null) return null; PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext); final PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false); if (method != null) { return method; } final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext); if (editor == null) { return null; } final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (psiFile == null) { return null; } PsiDocumentManager.getInstance(project).commitAllDocuments(); final int offset = editor.getCaretModel().getOffset(); if (offset < 1) { return null; } element = psiFile.findElementAt(offset); if (!(element instanceof PsiWhiteSpace)) { return null; } element = psiFile.findElementAt(offset - 1); if (!(element instanceof PsiJavaToken) || ((PsiJavaToken) element).getTokenType() != JavaTokenType.SEMICOLON) { return null; } return PsiTreeUtil.getParentOfType(element, PsiMethod.class, false); }
@Override public void invoke( @NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); PsiElement element = findTargetMember(file, editor); if (element == null) { element = LangDataKeys.PSI_ELEMENT.getData(dataContext); } if (element != null) { JetChangeSignatureDialog dialog = createDialog( element, file.findElementAt(editor.getCaretModel().getOffset()), project, editor); if (dialog != null) { dialog.show(); } } }
@Override protected void gotoActionPerformed(final AnActionEvent e) { final Project project = e.getData(PlatformDataKeys.PROJECT); if (project == null) return; PsiDocumentManager.getInstance(project).commitAllDocuments(); final PsiElement psiElement = LangDataKeys.PSI_ELEMENT.getData(e.getDataContext()); final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(e.getDataContext()); final VirtualFile virtualFile = PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext()); FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.goto.inspection"); final GotoInspectionModel model = new GotoInspectionModel(project); showNavigationPopup( e, model, new GotoActionCallback<Object>() { @Override protected ChooseByNameFilter<Object> createFilter(@NotNull ChooseByNamePopup popup) { popup.setSearchInAnyPlace(true); return super.createFilter(popup); } @Override public void elementChosen(ChooseByNamePopup popup, final Object element) { ApplicationManager.getApplication() .invokeLater( new Runnable() { @Override public void run() { runInspection( project, (InspectionToolWrapper) element, virtualFile, psiElement, psiFile); } }); } }); }
public Object getData(DataProvider dataProvider) { return LangDataKeys.PSI_ELEMENT.getData(dataProvider); }
public Object getData(String dataId) { if (PlatformDataKeys.PROJECT.is(dataId)) { return myProject; } if (PlatformDataKeys.NAVIGATABLE.is(dataId)) { final FavoritesTreeNodeDescriptor[] selectedNodeDescriptors = getSelectedNodeDescriptors(); return selectedNodeDescriptors.length == 1 ? selectedNodeDescriptors[0].getElement() : null; } if (PlatformDataKeys.NAVIGATABLE_ARRAY.is(dataId)) { final List<Navigatable> selectedElements = getSelectedElements(Navigatable.class); return selectedElements.toArray(new Navigatable[selectedElements.size()]); } if (PlatformDataKeys.CUT_PROVIDER.is(dataId)) { return myCopyPasteDelegator.getCutProvider(); } if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) { return myCopyPasteDelegator.getCopyProvider(); } if (PlatformDataKeys.PASTE_PROVIDER.is(dataId)) { return myCopyPasteDelegator.getPasteProvider(); } if (PlatformDataKeys.HELP_ID.is(dataId)) { return myHelpId; } if (LangDataKeys.PSI_ELEMENT.is(dataId)) { PsiElement[] elements = getSelectedPsiElements(); if (elements.length != 1) { return null; } return elements[0] != null && elements[0].isValid() ? elements[0] : null; } if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) { final PsiElement[] elements = getSelectedPsiElements(); ArrayList<PsiElement> result = new ArrayList<PsiElement>(); for (PsiElement element : elements) { if (element.isValid()) { result.add(element); } } return result.isEmpty() ? null : result.toArray(new PsiElement[result.size()]); } if (LangDataKeys.IDE_VIEW.is(dataId)) { return myIdeView; } if (LangDataKeys.TARGET_PSI_ELEMENT.is(dataId)) { return null; } if (LangDataKeys.MODULE_CONTEXT.is(dataId)) { Module[] selected = getSelectedModules(); return selected != null && selected.length == 1 ? selected[0] : null; } if (LangDataKeys.MODULE_CONTEXT_ARRAY.is(dataId)) { return getSelectedModules(); } if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) { final Object[] elements = getSelectedNodeElements(); return elements != null && elements.length >= 1 && elements[0] instanceof Module ? myDeleteModuleProvider : myDeletePSIElementProvider; } if (ModuleGroup.ARRAY_DATA_KEY.is(dataId)) { final List<ModuleGroup> selectedElements = getSelectedElements(ModuleGroup.class); return selectedElements.isEmpty() ? null : selectedElements.toArray(new ModuleGroup[selectedElements.size()]); } if (LibraryGroupElement.ARRAY_DATA_KEY.is(dataId)) { final List<LibraryGroupElement> selectedElements = getSelectedElements(LibraryGroupElement.class); return selectedElements.isEmpty() ? null : selectedElements.toArray(new LibraryGroupElement[selectedElements.size()]); } if (NamedLibraryElement.ARRAY_DATA_KEY.is(dataId)) { final List<NamedLibraryElement> selectedElements = getSelectedElements(NamedLibraryElement.class); return selectedElements.isEmpty() ? null : selectedElements.toArray(new NamedLibraryElement[selectedElements.size()]); } if (CONTEXT_FAVORITES_ROOTS_DATA_KEY.is(dataId)) { List<FavoritesTreeNodeDescriptor> result = new ArrayList<FavoritesTreeNodeDescriptor>(); FavoritesTreeNodeDescriptor[] selectedNodeDescriptors = getSelectedNodeDescriptors(); for (FavoritesTreeNodeDescriptor selectedNodeDescriptor : selectedNodeDescriptors) { FavoritesTreeNodeDescriptor root = selectedNodeDescriptor.getFavoritesRoot(); if (root != null && !(root.getElement().getValue() instanceof String)) { result.add(root); } } return result.toArray(new FavoritesTreeNodeDescriptor[result.size()]); } if (FAVORITES_LIST_NAME_DATA_KEY.is(dataId)) { return myListName; } FavoritesTreeNodeDescriptor[] descriptors = getSelectedNodeDescriptors(); if (descriptors.length > 0) { List<AbstractTreeNode> nodes = new ArrayList<AbstractTreeNode>(); for (FavoritesTreeNodeDescriptor descriptor : descriptors) { nodes.add(descriptor.getElement()); } return myFavoritesTreeStructure.getDataFromProviders(nodes, dataId); } return null; }