private void doCreate(final PsiDirectory baseDirectory, PsiElement myContext) { final PsiManager manager = baseDirectory.getManager(); PsiDirectory directory = baseDirectory; String lastName; for (StringTokenizer st = new StringTokenizer(myRedPart, "."); ; ) { lastName = st.nextToken(); if (st.hasMoreTokens()) { try { final PsiDirectory subdirectory = directory.findSubdirectory(lastName); directory = subdirectory != null ? subdirectory : directory.createSubdirectory(lastName); } catch (IncorrectOperationException e) { CreateFromUsageUtils.scheduleFileOrPackageCreationFailedMessageBox( e, lastName, directory, true); return; } } else { break; } } if (myClassKind != null) { PsiClass createdClass; if (myTemplateName != null) { createdClass = CreateClassUtil.createClassFromCustomTemplate( directory, null, lastName, myTemplateName); } else { createdClass = CreateFromUsageUtils.createClass( myClassKind == ClassKind.INTERFACE ? CreateClassKind.INTERFACE : CreateClassKind.CLASS, directory, lastName, manager, myContext, null, mySuperClass); } if (createdClass != null) { createdClass.navigate(true); } } else { try { directory.createSubdirectory(lastName); } catch (IncorrectOperationException e) { CreateFromUsageUtils.scheduleFileOrPackageCreationFailedMessageBox( e, lastName, directory, true); } } }
@Override public void apply( @NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) { PsiFile file = startElement.getContainingFile(); if (file instanceof XmlFile) { ResourceFolderType folderType = ResourceHelper.getFolderType(file); if (folderType != null) { if (folderType != ResourceFolderType.VALUES) { forkResourceFile((XmlFile) file, myFolder, true); } else { XmlTag tag = getValueTag(PsiTreeUtil.getParentOfType(startElement, XmlTag.class, false)); if (tag != null) { AndroidFacet facet = AndroidFacet.getInstance(startElement); if (facet != null) { PsiDirectory dir = null; if (myFolder != null) { PsiDirectory resFolder = findRes(file); if (resFolder != null) { dir = resFolder.findSubdirectory(myFolder); if (dir == null) { dir = resFolder.createSubdirectory(myFolder); } } } forkResourceValue(startElement.getProject(), tag, file, facet, dir); } } } } } }
@Nullable public static PsiDirectory selectFolderDir( final Project project, VirtualFile res, ResourceFolderType folderType) { final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(res); if (directory == null) { return null; } if (ApplicationManager.getApplication().isUnitTestMode() && ourTargetFolderName != null) { PsiDirectory subDirectory = directory.findSubdirectory(ourTargetFolderName); if (subDirectory != null) { return subDirectory; } return directory.createSubdirectory(ourTargetFolderName); } final CreateResourceDirectoryDialog dialog = new CreateResourceDirectoryDialog(project, folderType, directory, null) { @Override protected InputValidator createValidator() { return new ResourceDirectorySelector(project, directory); } }; dialog.setTitle("Select Resource Directory"); dialog.show(); final InputValidator validator = dialog.getValidator(); if (validator != null) { PsiElement[] createdElements = ((ResourceDirectorySelector) validator).getCreatedElements(); if (createdElements != null && createdElements.length > 0) { return (PsiDirectory) createdElements[0]; } } return null; }
public MkDirs(@NotNull String newName, @NotNull PsiDirectory directory) { if (SystemInfo.isWindows) { newName = newName.replace('\\', '/'); } if (newName.contains("/")) { final List<String> subDirs = StringUtil.split(newName, "/"); newName = subDirs.remove(subDirs.size() - 1); boolean firstToken = true; for (String dir : subDirs) { if (firstToken && "~".equals(dir)) { final VirtualFile userHomeDir = VfsUtil.getUserHomeDir(); if (userHomeDir == null) throw new IncorrectOperationException("User home directory not found"); final PsiDirectory directory1 = directory.getManager().findDirectory(userHomeDir); if (directory1 == null) throw new IncorrectOperationException("User home directory not found"); directory = directory1; } else if ("..".equals(dir)) { final PsiDirectory parentDirectory = directory.getParentDirectory(); if (parentDirectory == null) throw new IncorrectOperationException("Not a valid directory"); directory = parentDirectory; } else if (!".".equals(dir)) { final PsiDirectory sub = directory.findSubdirectory(dir); directory = sub == null ? directory.createSubdirectory(dir) : sub; } firstToken = false; } } this.newName = newName; this.directory = directory; }
@Override public PsiElement[] create(String newName) throws Exception { PsiDirectory subdirectory = myDirectory.findSubdirectory(newName); if (subdirectory == null) { subdirectory = myDirectory.createSubdirectory(newName); } return new PsiElement[] {subdirectory}; }
@NotNull public static PsiDirectory findOrCreateSubdirectory( @NotNull PsiDirectory directory, @NotNull String directoryName) { PsiDirectory subDirectory = directory.findSubdirectory(directoryName); if (subDirectory == null) { subDirectory = directory.createSubdirectory(directoryName); } return subDirectory; }
@Override protected PsiFile createFileFromTemplate( @NotNull String name, @NotNull FileTemplate template, @NotNull PsiDirectory dir) { // Strip extension so we don't end up with a file saved as "Foo.d.d" and content of `module // Foo.d where` if (name.endsWith(".d")) { name = name.substring(0, name.lastIndexOf('.')); } List<String> pathParts = StringUtil.split(name, "."); // Create any intermediate subdirectories. PsiDirectory subDir = dir; for (int i = 0; i < pathParts.size() - 1; ++i) { subDir = subDir.createSubdirectory(pathParts.get(i)); } String moduleName = pathParts.get(pathParts.size() - 1); return createFileFromTemplate(moduleName, template, subDir, getDefaultTemplateProperty()); }
/** * @param elementToCopy PsiFile or PsiDirectory * @param newName can be not null only if elements.length == 1 * @param choice a horrible way to pass/keep user preference * @return first copied PsiFile (recursively); null if no PsiFiles copied */ @Nullable public static PsiFile copyToDirectory( @NotNull PsiFileSystemItem elementToCopy, @Nullable String newName, @NotNull PsiDirectory targetDirectory, @Nullable int[] choice) throws IncorrectOperationException, IOException { if (elementToCopy instanceof PsiFile) { PsiFile file = (PsiFile) elementToCopy; String name = newName == null ? file.getName() : newName; if (checkFileExist(targetDirectory, choice, file, name, "Copy")) return null; return targetDirectory.copyFileFrom(name, file); } else if (elementToCopy instanceof PsiDirectory) { PsiDirectory directory = (PsiDirectory) elementToCopy; if (directory.equals(targetDirectory)) { return null; } if (newName == null) newName = directory.getName(); final PsiDirectory existing = targetDirectory.findSubdirectory(newName); final PsiDirectory subdirectory = existing == null ? targetDirectory.createSubdirectory(newName) : existing; EncodingRegistry.doActionAndRestoreEncoding( directory.getVirtualFile(), new ThrowableComputable<VirtualFile, IOException>() { @Override public VirtualFile compute() { return subdirectory.getVirtualFile(); } }); PsiFile firstFile = null; PsiElement[] children = directory.getChildren(); for (PsiElement child : children) { PsiFileSystemItem item = (PsiFileSystemItem) child; PsiFile f = copyToDirectory(item, item.getName(), subdirectory, choice); if (firstFile == null) { firstFile = f; } } return firstFile; } else { throw new IllegalArgumentException("unexpected elementToCopy: " + elementToCopy); } }
@Override protected PsiElement doCreate(PsiDirectory dir, String parameterName, String typeName) throws IncorrectOperationException { GoTemplatesFactory.Template template = GoTemplatesFactory.Template.GoFile; String fileName = fileNameFromTypeName(typeName, parameterName); String packageName = packageNameFromTypeName(typeName, parameterName); if (typeName.equals("multiple")) { if (dir.findSubdirectory(parameterName) == null) { dir = dir.createSubdirectory(parameterName); } else { dir = dir.findSubdirectory(parameterName); } fileName = fileName.replaceFirst(parameterName + "/", ""); } return GoTemplatesFactory.createFromTemplate(dir, packageName, fileName, template); }