@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; }
private void createTreeModel() { final PsiManager psiManager = PsiManager.getInstance(myProject); final FileIndex fileIndex = myModule != null ? ModuleRootManager.getInstance(myModule).getFileIndex() : ProjectRootManager.getInstance(myProject).getFileIndex(); fileIndex.iterateContent( new ContentIterator() { public boolean processFile(VirtualFile fileOrDir) { if (fileOrDir.isDirectory() && fileIndex.isInSourceContent(fileOrDir)) { final PsiDirectory psiDirectory = psiManager.findDirectory(fileOrDir); LOG.assertTrue(psiDirectory != null); PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory); if (aPackage != null) { addPackage(aPackage); } } return true; } }); TreeUtil.sort( myModel, new Comparator() { public int compare(Object o1, Object o2) { DefaultMutableTreeNode n1 = (DefaultMutableTreeNode) o1; DefaultMutableTreeNode n2 = (DefaultMutableTreeNode) o2; PsiNamedElement element1 = (PsiNamedElement) n1.getUserObject(); PsiNamedElement element2 = (PsiNamedElement) n2.getUserObject(); return element1.getName().compareToIgnoreCase(element2.getName()); } }); }
private PsiElement[] getElementsToDelete() { ArrayList<PsiElement> result = new ArrayList<PsiElement>(); Object[] elements = getSelectedNodeElements(); for (int idx = 0; elements != null && idx < elements.length; idx++) { if (elements[idx] instanceof PsiElement) { final PsiElement element = (PsiElement) elements[idx]; result.add(element); if (element instanceof PsiDirectory) { final VirtualFile virtualFile = ((PsiDirectory) element).getVirtualFile(); final String path = virtualFile.getPath(); if (path.endsWith(JarFileSystem.JAR_SEPARATOR)) { // if is jar-file root final VirtualFile vFile = LocalFileSystem.getInstance() .findFileByPath( path.substring(0, path.length() - JarFileSystem.JAR_SEPARATOR.length())); if (vFile != null) { final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(vFile); if (psiFile != null) { elements[idx] = psiFile; } } } } } } return result.toArray(new PsiElement[result.size()]); }
/** @deprecated to remove in IDEA 16 */ @Nullable public static PsiDirectory getPsiDirectory( @NotNull final FindModel findModel, @NotNull Project project) { VirtualFile directory = getDirectory(findModel); return directory == null ? null : PsiManager.getInstance(project).findDirectory(directory); }
@NotNull protected PsiElement[] invokeDialog(final Project project, final PsiDirectory directory) { final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); descriptor.setRoots(project.getBaseDir()); PsiDirectory currentDir = currentFile.getContainingDirectory(); PsiDirectory includeDir = currentDir.findSubdirectory("Includes"); VirtualFile selectDir = directory.getVirtualFile(); if (includeDir == null) { PsiDirectory parentDir = currentDir.getParentDirectory(); if (parentDir != null) { includeDir = parentDir.findSubdirectory("Includes"); } } if (includeDir != null) selectDir = includeDir.getVirtualFile(); final VirtualFile myFolder = FileChooser.chooseFile(descriptor, project, selectDir); if (myFolder != null) { PsiDirectory myDirectory = PsiManager.getInstance(project).findDirectory(myFolder); final MyInputValidator validator = new MyInputValidator(project, myDirectory); final String fileName = Messages.showInputDialog( project, getDialogPrompt(), getDialogTitle(), Messages.getQuestionIcon(), "", validator); final PsiElement[] elements = validator.getCreatedElements(); if (elements.length > 0) { ApplicationManager.getApplication() .invokeLater( new Runnable() { @Override public void run() { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { try { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); CaretModel caretModel = editor.getCaretModel(); int caretOffset = selectonModel.getSelectionStart(); EditorModificationUtil.deleteSelectedText(editor); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); caretModel.moveToOffset(caretOffset); EditorModificationUtil.insertStringAtCaret( editor, "<% include " + fileName + " %>", true, false); PsiFile createdFile = (PsiFile) elements[0]; codeStyleManager.reformat(createdFile); psiDocumentManager.commitDocument( psiDocumentManager.getDocument(currentFile)); FileEditorManager.getInstance(project) .openFile(currentFile.getVirtualFile(), true); codeStyleManager.adjustLineIndent(currentFile, caretOffset); psiDocumentManager.commitDocument( psiDocumentManager.getDocument(currentFile)); } catch (Exception e) { e.printStackTrace(); // To change body of catch statement use // File | Settings | File Templates. } } }); } }); } return elements; } return PsiElement.EMPTY_ARRAY; }