@Override public boolean canClose(final String inputString) { if (inputString.length() == 0) { return super.canClose(inputString); } final PsiDirectory psiDirectory = getDirectory(); final Project project = psiDirectory.getProject(); final boolean[] result = {false}; DumbService.allowStartingDumbModeInside(DumbModePermission.MAY_START_BACKGROUND, new Runnable() { @Override public void run() { final FileType type = FileTypeChooser.getKnownFileTypeOrAssociate(new FakeVirtualFile(psiDirectory.getVirtualFile(), getFileName(inputString)), project); result[0] = type != null && MyValidator.super.canClose(getFileName(inputString)); } }); return result[0]; }
@Override @NotNull public VirtualFile createChildDirectory(final Object requestor, @NotNull final VirtualFile parent, @NotNull final String dir) throws IOException { final File ioDir = new File(convertToIOFile(parent), dir); final boolean succeed = auxCreateDirectory(parent, dir) || ioDir.mkdirs(); auxNotifyCompleted(new ThrowableConsumer<LocalFileOperationsHandler, IOException>() { @Override public void consume(LocalFileOperationsHandler handler) throws IOException { handler.createDirectory(parent, dir); } }); if (!succeed) { throw new IOException("Failed to create directory: " + ioDir.getPath()); } return new FakeVirtualFile(parent, dir); }
@Override public VirtualFile createChildFile(final Object requestor, @NotNull final VirtualFile parent, @NotNull final String file) throws IOException { final File ioFile = new File(convertToIOFile(parent), file); final boolean succeed = auxCreateFile(parent, file) || FileUtil.createIfDoesntExist(ioFile); auxNotifyCompleted(new ThrowableConsumer<LocalFileOperationsHandler, IOException>() { @Override public void consume(LocalFileOperationsHandler handler) throws IOException { handler.createFile(parent, file); } }); if (!succeed) { throw new IOException("Failed to create child file at " + ioFile.getPath()); } return new FakeVirtualFile(parent, file); }
/** * If fileName is already associated any known file type returns it. * Otherwise asks user to select file type and associates it with fileName extension if any selected. * @return Known file type or null. Never returns {@link com.intellij.openapi.fileTypes.FileTypes#UNKNOWN}. */ @Nullable public static FileType getKnownFileTypeOrAssociate(@NotNull VirtualFile file, @Nullable Project project) { if (project != null && !(file instanceof FakeVirtualFile)) { ((PsiManagerEx)PsiManager.getInstance(project)).getFileManager().findFile(file); // autodetect text file if needed } FileType type = file.getFileType(); if (type == FileTypes.UNKNOWN) { type = getKnownFileTypeOrAssociate(file.getName()); } return type; }
@Override @NotNull public VirtualFile createChildDirectory(Object requestor, @NotNull final VirtualFile parent, @NotNull final String dir) throws IOException { if (!VirtualFile.isValidName(dir)) { throw new IOException(VfsBundle.message("directory.invalid.name.error", dir)); } if (!parent.exists() || !parent.isDirectory()) { throw new IOException(VfsBundle.message("vfs.target.not.directory.error", parent.getPath())); } if (parent.findChild(dir) != null) { throw new IOException(VfsBundle.message("vfs.target.already.exists.error", parent.getPath() + "/" + dir)); } File ioParent = convertToIOFile(parent); if (!ioParent.isDirectory()) { throw new IOException(VfsBundle.message("target.not.directory.error", ioParent.getPath())); } if (!auxCreateDirectory(parent, dir)) { File ioDir = new File(ioParent, dir); if (!(ioDir.mkdirs() || ioDir.isDirectory())) { throw new IOException(VfsBundle.message("new.directory.failed.error", ioDir.getPath())); } } auxNotifyCompleted(new ThrowableConsumer<LocalFileOperationsHandler, IOException>() { @Override public void consume(LocalFileOperationsHandler handler) throws IOException { handler.createDirectory(parent, dir); } }); return new FakeVirtualFile(parent, dir); }
@NotNull @Override public VirtualFile createChildFile(Object requestor, @NotNull final VirtualFile parent, @NotNull final String file) throws IOException { if (!VirtualFile.isValidName(file)) { throw new IOException(VfsBundle.message("file.invalid.name.error", file)); } if (!parent.exists() || !parent.isDirectory()) { throw new IOException(VfsBundle.message("vfs.target.not.directory.error", parent.getPath())); } if (parent.findChild(file) != null) { throw new IOException(VfsBundle.message("vfs.target.already.exists.error", parent.getPath() + "/" + file)); } File ioParent = convertToIOFile(parent); if (!ioParent.isDirectory()) { throw new IOException(VfsBundle.message("target.not.directory.error", ioParent.getPath())); } if (!auxCreateFile(parent, file)) { File ioFile = new File(ioParent, file); if (!FileUtil.createIfDoesntExist(ioFile)) { throw new IOException(VfsBundle.message("new.file.failed.error", ioFile.getPath())); } } auxNotifyCompleted(new ThrowableConsumer<LocalFileOperationsHandler, IOException>() { @Override public void consume(LocalFileOperationsHandler handler) throws IOException { handler.createFile(parent, file); } }); return new FakeVirtualFile(parent, file); }
public void testGetAttributesConvertsToAbsolute() throws Exception { PersistentFS fs = PersistentFS.getInstance(); LocalFileSystem lfs = LocalFileSystem.getInstance(); NewVirtualFile fakeRoot = fs.findRoot("", lfs); assertNotNull(fakeRoot); File userDir = new File(System.getProperty("user.dir")); File[] files = userDir.listFiles(); File fileToQuery; if (files != null && files.length != 0) { fileToQuery = files[0]; } else if (userDir.isDirectory()) { fileToQuery = FileUtil.createTempFile(userDir, getTestName(false), "", true); myFilesToDelete.add(fileToQuery); } else { // can't test return; } FileAttributes attributes = lfs.getAttributes(new FakeVirtualFile(fakeRoot, fileToQuery.getName())); assertNull(attributes); attributes = lfs.getAttributes(new FakeVirtualFile(fakeRoot, "windows")); assertNull(attributes); attributes = lfs.getAttributes(new FakeVirtualFile(fakeRoot, "usr")); assertNull(attributes); attributes = lfs.getAttributes(new FakeVirtualFile(fakeRoot, "Users")); assertNull(attributes); }
@Override public boolean canClose(String inputString) { if (inputString.length() == 0) { return super.canClose(inputString); } final PsiDirectory psiDirectory = getDirectory(); final FileType type = FileTypeChooser.getKnownFileTypeOrAssociate(new FakeVirtualFile(psiDirectory.getVirtualFile(), getFileName(inputString)), psiDirectory.getProject()); return type != null && super.canClose(getFileName(inputString)); }
/** * If fileName is already associated any known file type returns it. * Otherwise asks user to select file type and associates it with fileName extension if any selected. * * @return Known file type or null. Never returns {@link UnknownFileType#INSTANCE}. */ @Nullable @RequiredDispatchThread public static FileType getKnownFileTypeOrAssociate(@Nonnull VirtualFile file, @Nullable Project project) { if (project != null && !(file instanceof FakeVirtualFile)) { PsiManagerEx.getInstanceEx(project).getFileManager().findFile(file); // autodetect text file if needed } FileType type = file.getFileType(); if (type == UnknownFileType.INSTANCE) { type = getKnownFileTypeOrAssociate(file.getName()); } return type; }
@Override @Nonnull public VirtualFile createChildDirectory(Object requestor, @Nonnull final VirtualFile parent, @Nonnull final String dir) throws IOException { if (!isValidName(dir)) { throw new IOException(VfsBundle.message("directory.invalid.name.error", dir)); } if (!parent.exists() || !parent.isDirectory()) { throw new IOException(VfsBundle.message("vfs.target.not.directory.error", parent.getPath())); } if (parent.findChild(dir) != null) { throw new IOException(VfsBundle.message("vfs.target.already.exists.error", parent.getPath() + "/" + dir)); } File ioParent = convertToIOFile(parent); if (!ioParent.isDirectory()) { throw new IOException(VfsBundle.message("target.not.directory.error", ioParent.getPath())); } if (!auxCreateDirectory(parent, dir)) { File ioDir = new File(ioParent, dir); if (!(ioDir.mkdirs() || ioDir.isDirectory())) { throw new IOException(VfsBundle.message("new.directory.failed.error", ioDir.getPath())); } } auxNotifyCompleted(handler -> handler.createDirectory(parent, dir)); return new FakeVirtualFile(parent, dir); }
@Nonnull @Override public VirtualFile createChildFile(Object requestor, @Nonnull final VirtualFile parent, @Nonnull final String file) throws IOException { if (!isValidName(file)) { throw new IOException(VfsBundle.message("file.invalid.name.error", file)); } if (!parent.exists() || !parent.isDirectory()) { throw new IOException(VfsBundle.message("vfs.target.not.directory.error", parent.getPath())); } if (parent.findChild(file) != null) { throw new IOException(VfsBundle.message("vfs.target.already.exists.error", parent.getPath() + "/" + file)); } File ioParent = convertToIOFile(parent); if (!ioParent.isDirectory()) { throw new IOException(VfsBundle.message("target.not.directory.error", ioParent.getPath())); } if (!auxCreateFile(parent, file)) { File ioFile = new File(ioParent, file); if (!FileUtil.createIfDoesntExist(ioFile)) { throw new IOException(VfsBundle.message("new.file.failed.error", ioFile.getPath())); } } auxNotifyCompleted(handler -> handler.createFile(parent, file)); return new FakeVirtualFile(parent, file); }
@NotNull @Override public VirtualFile copyFile(Object requestor, @NotNull final VirtualFile file, @NotNull final VirtualFile newParent, @NotNull final String copyName) throws IOException { if (!VirtualFile.isValidName(copyName)) { throw new IOException(VfsBundle.message("file.invalid.name.error", copyName)); } if (!file.exists()) { throw new IOException(VfsBundle.message("vfs.file.not.exist.error", file.getPath())); } if (!newParent.exists() || !newParent.isDirectory()) { throw new IOException(VfsBundle.message("vfs.target.not.directory.error", newParent.getPath())); } if (newParent.findChild(copyName) != null) { throw new IOException(VfsBundle.message("vfs.target.already.exists.error", newParent.getPath() + "/" + copyName)); } FileAttributes attributes = getAttributes(file); if (attributes == null) { throw new FileNotFoundException(VfsBundle.message("file.not.exist.error", file.getPath())); } if (attributes.isSpecial()) { throw new FileNotFoundException("Not a file: " + file); } File ioParent = convertToIOFile(newParent); if (!ioParent.isDirectory()) { throw new IOException(VfsBundle.message("target.not.directory.error", ioParent.getPath())); } File ioTarget = new File(ioParent, copyName); if (ioTarget.exists()) { throw new IOException(VfsBundle.message("target.already.exists.error", ioTarget.getPath())); } if (!auxCopy(file, newParent, copyName)) { try { File ioFile = convertToIOFile(file); FileUtil.copyFileOrDir(ioFile, ioTarget, attributes.isDirectory()); } catch (IOException e) { FileUtil.delete(ioTarget); throw e; } } auxNotifyCompleted(new ThrowableConsumer<LocalFileOperationsHandler, IOException>() { @Override public void consume(LocalFileOperationsHandler handler) throws IOException { handler.copy(file, newParent, copyName); } }); return new FakeVirtualFile(newParent, copyName); }
private VirtualFile createWebInfParentWebXml() { VirtualFile webInf = new FakeVirtualFile(new RootVirtualFile(), "WEB-INF"); return new FakeVirtualFile(webInf, "appengine-web.xml"); }
private VirtualFile createRootWebXml() { return new FakeVirtualFile(new RootVirtualFile(), "appengine-web.xml"); }
@Override public VirtualFile copyFile(final Object requestor, @NotNull final VirtualFile vFile, @NotNull final VirtualFile newParent, @NotNull final String copyName) throws IOException { if (!PathUtil.isValidFileName(copyName)) { throw new IOException("Invalid file name: " + copyName); } FileAttributes attributes = getAttributes(vFile); if (attributes == null || attributes.isSpecial()) { throw new FileNotFoundException("Not a file: " + vFile); } File physicalFile = convertToIOFile(vFile); File physicalCopy = auxCopy(vFile, newParent, copyName); try { if (physicalCopy == null) { File newPhysicalParent = convertToIOFile(newParent); physicalCopy = new File(newPhysicalParent, copyName); try { if (attributes.isDirectory()) { FileUtil.copyDir(physicalFile, physicalCopy); } else { FileUtil.copy(physicalFile, physicalCopy); } } catch (IOException e) { FileUtil.delete(physicalCopy); throw e; } } } finally { auxNotifyCompleted(new ThrowableConsumer<LocalFileOperationsHandler, IOException>() { @Override public void consume(LocalFileOperationsHandler handler) throws IOException { handler.copy(vFile, newParent, copyName); } }); } return new FakeVirtualFile(newParent, copyName); }
/** * Speculates if file with newName would have known file type */ @Nullable @RequiredDispatchThread public static FileType getKnownFileTypeOrAssociate(@Nonnull VirtualFile parent, @Nonnull String newName, @Nullable Project project) { return getKnownFileTypeOrAssociate(new FakeVirtualFile(parent, newName), project); }
@Nonnull @Override public VirtualFile copyFile(Object requestor, @Nonnull final VirtualFile file, @Nonnull final VirtualFile newParent, @Nonnull final String copyName) throws IOException { if (!isValidName(copyName)) { throw new IOException(VfsBundle.message("file.invalid.name.error", copyName)); } if (!file.exists()) { throw new IOException(VfsBundle.message("vfs.file.not.exist.error", file.getPath())); } if (!newParent.exists() || !newParent.isDirectory()) { throw new IOException(VfsBundle.message("vfs.target.not.directory.error", newParent.getPath())); } if (newParent.findChild(copyName) != null) { throw new IOException(VfsBundle.message("vfs.target.already.exists.error", newParent.getPath() + "/" + copyName)); } FileAttributes attributes = getAttributes(file); if (attributes == null) { throw new FileNotFoundException(VfsBundle.message("file.not.exist.error", file.getPath())); } if (attributes.isSpecial()) { throw new FileNotFoundException("Not a file: " + file); } File ioParent = convertToIOFile(newParent); if (!ioParent.isDirectory()) { throw new IOException(VfsBundle.message("target.not.directory.error", ioParent.getPath())); } File ioTarget = new File(ioParent, copyName); if (ioTarget.exists()) { throw new IOException(VfsBundle.message("target.already.exists.error", ioTarget.getPath())); } if (!auxCopy(file, newParent, copyName)) { try { File ioFile = convertToIOFile(file); FileUtil.copyFileOrDir(ioFile, ioTarget, attributes.isDirectory()); } catch (IOException e) { FileUtil.delete(ioTarget); throw e; } } auxNotifyCompleted(handler -> handler.copy(file, newParent, copyName)); return new FakeVirtualFile(newParent, copyName); }