Java Code Examples for com.intellij.openapi.vfs.VirtualFile#createChildData()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#createChildData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DifferenceReverterTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testDirDeletion() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(this, "dir");
  VirtualFile subdir = dir.createChildDirectory(this, "subdir");
  VirtualFile f = subdir.createChildData(this, "foo.txt");
  f.setBinaryContent(new byte[]{123}, -1, 4000);

  dir.delete(this);

  revertLastChange();

  dir = myRoot.findChild("dir");
  subdir = dir.findChild("subdir");
  f = subdir.findChild("foo.txt");
  assertNotNull(f);
  assertEquals(123, f.contentsToByteArray()[0]);
  assertEquals(4000, f.getTimeStamp());
}
 
Example 2
Source File: PatchCreatorTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testDirectoryRename() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(null, "dir1");
  dir.createChildData(null, "f.txt");

  dir.rename(null, "dir2");

  createPatchBetweenRevisions(1, 0);

  dir.rename(null, "dir1");

  applyPatch();

  VirtualFile afterDir1 = myRoot.findChild("dir1");
  VirtualFile afterDir2 = myRoot.findChild("dir2");
  assertNotNull(afterDir1);
  assertNotNull(afterDir2);

  assertNull(afterDir1.findChild("f.txt"));
  assertNotNull(afterDir2.findChild("f.txt"));
}
 
Example 3
Source File: PatchCreatorTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testDirectoryDeletionWithFiles() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(null, "dir");
  dir.createChildData(null, "f1.txt");
  dir.createChildData(null, "f2.txt");

  dir.delete(null);
  createPatchBetweenRevisions(1, 0, false);

  dir = myRoot.createChildDirectory(null, "dir");
  dir.createChildData(null, "f1.txt");
  dir.createChildData(null, "f2.txt");

  applyPatch();

  assertNotNull(myRoot.findChild("dir"));
  assertNull(myRoot.findChild("dir").findChild("f1.txt"));
  assertNull(myRoot.findChild("dir").findChild("f2.txt"));
}
 
Example 4
Source File: PsiFileUtils.java    From crud-intellij-plugin with Apache License 2.0 6 votes vote down vote up
private static void createModel(Project project, VirtualFile packageDir, Model model) {
	try {
		VirtualFile virtualFile = packageDir.createChildData(project, model.getSimpleName() + ".java");
		StringWriter sw = new StringWriter();
		String templateName;
		if (model.getOrmType() == SelectionContext.MYBATIS) {
			templateName = "model_mybatis.ftl";
		} else {
			templateName = "model_jpa.ftl";
		}
		Template template = freemarker.getTemplate(templateName);
		template.process(model, sw);
		virtualFile.setBinaryContent(sw.toString().getBytes(CrudUtils.DEFAULT_CHARSET));
		CrudUtils.addWaitOptimizeFile(virtualFile);
	} catch (Exception e) {
		e.printStackTrace();
	}

}
 
Example 5
Source File: PsiFileUtils.java    From crud-intellij-plugin with Apache License 2.0 6 votes vote down vote up
private static void createDao(Project project, VirtualFile packageDir, Dao dao) {
	try {
		VirtualFile virtualFile = packageDir.createChildData(project, dao.getSimpleName() + ".java");
		StringWriter sw = new StringWriter();
		String templateName;
		if (dao.getOrmType() == SelectionContext.MYBATIS) {
			templateName = "dao_mybatis.ftl";
		} else {
			templateName = "dao_jpa.ftl";
		}
		Template template = freemarker.getTemplate(templateName);
		template.process(dao, sw);
		virtualFile.setBinaryContent(sw.toString().getBytes(CrudUtils.DEFAULT_CHARSET));

		CrudUtils.addWaitOptimizeFile(virtualFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 6
Source File: DifferenceReverterTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testMovement() throws Exception {
  VirtualFile dir1 = myRoot.createChildDirectory(this, "dir1");
  VirtualFile dir2 = myRoot.createChildDirectory(this, "dir2");

  VirtualFile f = dir1.createChildData(this, "foo.txt");
  f.setBinaryContent(new byte[]{123}, -1, 4000);

  f.move(this, dir2);

  revertLastChange();

  assertNull(dir2.findChild("foo.txt"));
  f = dir1.findChild("foo.txt");
  assertNotNull(f);
  assertEquals(123, f.contentsToByteArray()[0]);
  assertEquals(4000, f.getTimeStamp());
}
 
Example 7
Source File: DifferenceReverterTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testRevertingMoveFromOldRevisionsWhenDirDoesNotExists() throws Exception {
  VirtualFile dir1 = myRoot.createChildDirectory(this, "dir1");
  VirtualFile dir2 = myRoot.createChildDirectory(this, "dir2");
  VirtualFile f = dir1.createChildData(this, "foo.txt");

  f.move(this, dir2);

  dir1.delete(this);
  dir2.delete(this);

  revertChange(2);

  dir1 = myRoot.findChild("dir1");
  assertNotNull(dir1);
  assertNull(myRoot.findChild("dir2"));
  assertNotNull(dir1.findChild("foo.txt"));
}
 
Example 8
Source File: DifferenceReverterTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testParentAndChildRename() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(this, "dir");
  VirtualFile f = dir.createChildData(this, "foo.txt");
  f.setBinaryContent(new byte[]{123}, -1, 4000);

  getVcs().beginChangeSet();
  dir.rename(this, "dir2");
  f.rename(this, "bar.txt");
  getVcs().endChangeSet(null);

  revertLastChange();

  assertNull(myRoot.findChild("dir2"));
  dir = myRoot.findChild("dir");

  assertNull(dir.findChild("bar.txt"));
  f = dir.findChild("foo.txt");
  assertNotNull(f);
  assertEquals(123, f.contentsToByteArray()[0]);
  assertEquals(4000, f.getTimeStamp());
}
 
Example 9
Source File: TemplateUtils.java    From CppTools with Apache License 2.0 5 votes vote down vote up
public static void createOrResetFileContent(VirtualFile sourcePathDir, String fileName, StringBufferInputStream inputStream) throws IOException {
  VirtualFile child = sourcePathDir.findChild(fileName);
  if (child == null) child = sourcePathDir.createChildData(CppModuleBuilder.class, fileName);
  OutputStream outputStream = child.getOutputStream(CppModuleBuilder.class);

  FileUtil.copy(inputStream, outputStream);
  outputStream.flush();
  outputStream.close();
}
 
Example 10
Source File: PatchCreatorTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testDirectoryCreationWithFiles() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(null, "dir");
  dir.createChildData(null, "f.txt");

  createPatchBetweenRevisions(2, 0, false);
  clearRoot();

  applyPatch();

  assertNotNull(myRoot.findChild("dir"));
  assertNotNull(myRoot.findChild("dir").findChild("f.txt"));
}
 
Example 11
Source File: TempFiles.java    From consulo with Apache License 2.0 5 votes vote down vote up
public VirtualFile createVFile(VirtualFile parentDir, String name, String text) {
  try {
    final VirtualFile virtualFile = parentDir.createChildData(this, name);
    VfsUtil.saveText(virtualFile, text + "\n");
    return virtualFile;
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 12
Source File: IntellijFile.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates this file in the local filesystem with the given content.
 *
 * @param input an input stream to write into the file
 * @throws FileAlreadyExistsException if a resource with the same name already exists
 * @throws FileNotFoundException if the parent directory of this file does not exist
 */
private void createInternal(@Nullable InputStream input) throws IOException {
  IResource parent = getParent();

  VirtualFile parentFile = referencePoint.findVirtualFile(parent.getReferencePointRelativePath());

  if (parentFile == null || !parentFile.exists()) {
    throw new FileNotFoundException(
        "Could not create "
            + this
            + " as its parent folder "
            + parent
            + " does not exist or is not valid");
  }

  VirtualFile virtualFile = parentFile.findChild(getName());

  if (virtualFile != null && virtualFile.exists()) {
    throw new FileAlreadyExistsException(
        "Could not create "
            + this
            + " as a resource with the same name already exists: "
            + virtualFile);
  }

  parentFile.createChildData(this, getName());

  if (input != null) {
    setContents(input);
  }
}
 
Example 13
Source File: FileHistoryDialogTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testRevertion() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(null, "oldDir");
  VirtualFile f = dir.createChildData(null, "old.txt");
  f.rename(null, "new.txt");
  dir.rename(null, "newDir");

  FileHistoryDialogModel m = createFileModelAndSelectRevisions(f, 1, 1);
  m.createReverter().revert();

  assertEquals("old.txt", f.getName());
  assertEquals(f.getParent(), dir);
  assertEquals("newDir", dir.getName());
}
 
Example 14
Source File: PsiFileUtils.java    From crud-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private static void createServiceImplJpa(Project project, VirtualFile packageDir, Service service) {
	try {
		VirtualFile virtualFile = packageDir.createChildData(project, service.getSimpleName() + "Impl.java");
		StringWriter sw = new StringWriter();
		Template template = freemarker.getTemplate("service_impl.ftl");
		template.process(service, sw);
		virtualFile.setBinaryContent(sw.toString().getBytes(CrudUtils.DEFAULT_CHARSET));

		CrudUtils.addWaitOptimizeFile(virtualFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 15
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public PsiFile copyFileFrom(@Nonnull String newName, @Nonnull PsiFile originalFile) throws IncorrectOperationException {
  checkCreateFile(newName);

  final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(originalFile);
  if (document != null) {
    FileDocumentManager.getInstance().saveDocument(document);
  }

  final VirtualFile parent = getVirtualFile();
  try {
    final VirtualFile vFile = originalFile.getVirtualFile();
    if (vFile == null) throw new IncorrectOperationException("Cannot copy nonphysical file");
    VirtualFile copyVFile;
    if (parent.getFileSystem() == vFile.getFileSystem()) {
      copyVFile = vFile.copy(this, parent, newName);
    }
    else if (vFile instanceof LightVirtualFile) {
      copyVFile = parent.createChildData(this, newName);
      copyVFile.setBinaryContent(originalFile.getText().getBytes(copyVFile.getCharset()));
    }
    else {
      copyVFile = VfsUtilCore.copyFile(this, vFile, parent, newName);
    }
    LOG.assertTrue(copyVFile != null, "File was not copied: " + vFile);
    DumbService.getInstance(getProject()).completeJustSubmittedTasks();
    final PsiFile copyPsi = myManager.findFile(copyVFile);
    if (copyPsi == null) {
      LOG.error("Could not find file '" + copyVFile + "' after copying '" + vFile + "'");
    }
    updateAddedFile(copyPsi);
    return copyPsi;
  }
  catch (IOException e) {
    throw new IncorrectOperationException(e);
  }
}
 
Example 16
Source File: PsiFileUtils.java    From crud-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private static void createMapper(Project project, VirtualFile packageDir, Dao dao) {
	try {
		VirtualFile virtualFile = packageDir.createChildData(project, dao.getModel().getSimpleName() + "Mapper.xml");
		StringWriter sw = new StringWriter();
		Template template = freemarker.getTemplate("mapper.ftl");
		template.process(dao, sw);
		virtualFile.setBinaryContent(sw.toString().getBytes(CrudUtils.DEFAULT_CHARSET));
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 17
Source File: PsiFileUtils.java    From crud-intellij-plugin with Apache License 2.0 5 votes vote down vote up
public static void createApplicationYml(Project project, VirtualFile root, Selection selection) {
	try {
		VirtualFile virtualFile = root.createChildData(project, "application.yml");
		StringWriter sw = new StringWriter();
		Template template = freemarker.getTemplate("application.yml.ftl");
		template.process(selection, sw);
		virtualFile.setBinaryContent(sw.toString().getBytes(CrudUtils.DEFAULT_CHARSET));
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 18
Source File: PsiFileUtils.java    From crud-intellij-plugin with Apache License 2.0 5 votes vote down vote up
public static void createApplicationJava(Project project, VirtualFile root, Selection selection) {
	try {
		VirtualFile virtualFile = root.createChildData(project, "Application.java");
		StringWriter sw = new StringWriter();
		Template template = freemarker.getTemplate("Application.java.ftl");
		template.process(selection, sw);
		virtualFile.setBinaryContent(sw.toString().getBytes(CrudUtils.DEFAULT_CHARSET));
		CrudUtils.addWaitOptimizeFile(virtualFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 19
Source File: ScratchFileServiceImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public VirtualFile findFile(@Nonnull RootType rootType, @Nonnull String pathName, @Nonnull Option option) throws IOException {
  ApplicationManager.getApplication().assertReadAccessAllowed();

  String fullPath = getRootPath(rootType) + "/" + pathName;
  if (option != Option.create_new_always) {
    VirtualFile file = LocalFileSystem.getInstance().findFileByPath(fullPath);
    if (file != null && !file.isDirectory()) return file;
    if (option == Option.existing_only) return null;
  }
  String ext = PathUtil.getFileExtension(pathName);
  String fileNameExt = PathUtil.getFileName(pathName);
  String fileName = StringUtil.trimEnd(fileNameExt, ext == null ? "" : "." + ext);
  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(getClass());
  try {
    VirtualFile dir = VfsUtil.createDirectories(PathUtil.getParentPath(fullPath));
    if (option == Option.create_new_always) {
      return VfsUtil.createChildSequent(LocalFileSystem.getInstance(), dir, fileName, StringUtil.notNullize(ext));
    }
    else {
      return dir.createChildData(LocalFileSystem.getInstance(), fileNameExt);
    }
  }
  finally {
    token.finish();
  }
}
 
Example 20
Source File: ResolvingTest.java    From idea-gitignore with MIT License 4 votes vote down vote up
public void testGlob() throws IOException {
    VirtualFile dir = myFixture.getTempDirFixture().findOrCreateDir("glob");
    dir.createChildData(this, "fileName1.txt");
    dir.createChildData(this, "fileName2.txt");
    doTest("glob/*<caret>.txt", "glob/fileName1.txt", "glob/fileName2.txt");
}