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

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#rename() . These examples are extracted from open source projects. 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
@Override
public void visit(RenameChange c) throws StopVisitingException {
  if (shouldRevert(c)) {
    VirtualFile f = myGateway.findVirtualFile(c.getPath());
    if (f != null) {
      VirtualFile existing = f.getParent().findChild(c.getOldName());
      try {
        if (existing != null && !Comparing.equal(existing, f)) {
          existing.delete(LocalHistory.VFS_EVENT_REQUESTOR);
        }
        f.rename(LocalHistory.VFS_EVENT_REQUESTOR, c.getOldName());
      }
      catch (IOException e) {
        throw new RuntimeIOException(e);
      }
    }
  }
  checkShouldStop(c);
}
 
Example 2
Source Project: consulo   File: PatchCreatorTest.java    License: 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 Project: consulo   File: ScratchUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void updateFileExtension(@Nonnull Project project, @Nullable VirtualFile file) throws IOException {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  if (CommandProcessor.getInstance().getCurrentCommand() == null) {
    throw new AssertionError("command required");
  }

  if (file == null) return;
  Language language = LanguageUtil.getLanguageForPsi(project, file);
  FileType expected = getFileTypeFromName(file);
  FileType actual = language == null ? null : language.getAssociatedFileType();
  if (expected == actual || actual == null) return;
  String ext = actual.getDefaultExtension();
  if (StringUtil.isEmpty(ext)) return;

  String newName = PathUtil.makeFileName(file.getNameWithoutExtension(), ext);
  VirtualFile parent = file.getParent();
  newName = parent != null && parent.findChild(newName) != null ? PathUtil.makeFileName(file.getName(), ext) : newName;
  file.rename(ScratchUtil.class, newName);
}
 
Example 4
Source Project: consulo   File: DifferenceReverterTest.java    License: 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 5
Source Project: consulo   File: DifferenceReverterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testContentChangeWhenDirectoryExists() throws Exception {
  VirtualFile f = myRoot.createChildData(this, "foo.txt");
  f.setBinaryContent(new byte[]{1}, -1, 1000);

  getVcs().beginChangeSet();
  f.rename(this, "bar.txt");
  f.setBinaryContent(new byte[]{2}, -1, 2000);
  getVcs().endChangeSet(null);

  myRoot.createChildDirectory(this, "foo.txt");

  revertChange(1, 0, 1);

  assertNull(myRoot.findChild("bar.txt"));
  f = myRoot.findChild("foo.txt");
  assertNotNull(f);
  assertFalse(f.isDirectory());
  assertEquals(1, f.contentsToByteArray()[0]);
  assertEquals(1000, f.getTimeStamp());
}
 
Example 6
Source Project: consulo   File: RevisionsAndDiffsTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testRevisionsForFileCreatedInPlaceOfRenamedOne() throws IOException {
  VirtualFile f = createFile("file1.txt", "content1");
  f.rename("file1", "file2.txt");
  VirtualFile ff = createFile("file1.txt", "content2");

  List<Revision> rr = getRevisionsFor(ff);
  assertEquals(2, rr.size());

  Entry e = rr.get(0).findEntry();
  assertEquals("file1.txt", e.getName());
  assertContent("content2", e);

  rr = getRevisionsFor(f);
  assertEquals(3, rr.size());

  e = rr.get(0).findEntry();
  assertEquals("file2.txt", e.getName());
  assertContent("content1", e);

  e = rr.get(1).findEntry();
  assertEquals("file1.txt", e.getName());
  assertContent("content1", e);
}
 
Example 7
Source Project: consulo   File: FileHistoryDialogTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testTitles() throws IOException {
  long leftTime = new Date(2001 - 1900, 1, 3, 12, 0).getTime();
  long rightTime = new Date(2002 - 1900, 2, 4, 14, 0).getTime();

  VirtualFile f = myRoot.createChildData(null, "old.txt");
  f.setBinaryContent("old".getBytes(), -1, leftTime);

  f.rename(null, "new.txt");
  f.setBinaryContent("new".getBytes(), -1, rightTime);

  f.setBinaryContent(new byte[0]); // to create current content to skip.

  FileHistoryDialogModel m = createFileModelAndSelectRevisions(f, 0, 2);
  assertEquals(FileUtil.toSystemDependentName(f.getPath()), m.getDifferenceModel().getTitle());

  assertEquals(DateFormatUtil.formatPrettyDateTime(leftTime) + " - old.txt",
               m.getDifferenceModel().getLeftTitle(new NullRevisionsProgress()));
  assertEquals(DateFormatUtil.formatPrettyDateTime(rightTime) + " - new.txt",
               m.getDifferenceModel().getRightTitle(new NullRevisionsProgress()));
}
 
Example 8
/**
 * Called when user invokes intention.
 */
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    VirtualFile vFile = file.getVirtualFile();
    Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    if (document == null) return;

    FileDocumentManager.getInstance().saveDocument(document);
    try {
        vFile.rename(file.getManager(), myTargetName);
    } catch (IOException e) {
        MessagesEx.error(project, e.getMessage()).showLater();
    }
}
 
Example 9
private static void renameFile(VirtualFile file, String newName) throws IOException {
  AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(FileDocumentManagerImplTest.class);
  try {
    file.rename(null, newName);
  }
  finally {
    token.finish();
  }
}
 
Example 10
Source Project: consulo   File: DifferenceReverter.java    License: Apache License 2.0 5 votes vote down vote up
private void revertRename(Entry l, VirtualFile file) throws IOException {
  String oldName = l.getName();
  if (!oldName.equals(file.getName())) {
    VirtualFile existing = file.getParent().findChild(oldName);
    if (existing != null) {
      existing.delete(this);
    }
    file.rename(this, oldName);
  }
}
 
Example 11
Source Project: consulo   File: PatchCreatorTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testRename() throws Exception {
  VirtualFile f = myRoot.createChildData(null, "f.txt");
  f.setBinaryContent(new byte[]{1});

  f.rename(null, "ff.txt");

  createPatchBetweenRevisions(1, 0);
  f.rename(null, "f.txt");
  applyPatch();

  VirtualFile patched = myRoot.findChild("ff.txt");
  assertNull(myRoot.findChild("f.txt"));
  assertNotNull(patched);
  assertEquals(1, patched.contentsToByteArray()[0]);
}
 
Example 12
Source Project: consulo   File: VisitingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testVisitingAllChanges() throws Exception {
  createFile("f.txt");
  getVcs().beginChangeSet();
  VirtualFile dir = createFile("dir");
  getVcs().endChangeSet(null);
  getVcs().beginChangeSet();
  dir.rename(this, "newDir");

  assertVisitorLog("begin rename end begin create end begin create end finished ");
}
 
Example 13
Source Project: consulo   File: DifferenceReverterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testRename() throws Exception {
  VirtualFile f = myRoot.createChildData(this, "foo.txt");
  f.setBinaryContent(new byte[]{123}, -1, 4000);
  f.rename(this, "bar.txt");

  revertLastChange();

  assertNull(myRoot.findChild("bar.txt"));
  f = myRoot.findChild("foo.txt");
  assertNotNull(f);
  assertEquals(123, f.contentsToByteArray()[0]);
  assertEquals(4000, f.getTimeStamp());
}
 
Example 14
Source Project: consulo   File: DifferenceReverterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testRevertingRenameFromOldRevisionsWhenDirDoesNotExists() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(this, "dir");
  VirtualFile f = dir.createChildData(this, "foo.txt");

  f.rename(this, "bar.txt");

  dir.delete(this);

  revertChange(1);

  dir = myRoot.findChild("dir");
  assertNotNull(dir);
  assertNotNull(dir.findChild("foo.txt"));
  assertNull(dir.findChild("bar.txt"));
}
 
Example 15
Source Project: consulo   File: RevisionsAndDiffsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testGettingEntryFromRevisionInRenamedDir() throws IOException {
  VirtualFile dir = createDirectory("dir");
  VirtualFile f = createFile("dir/file.txt");
  dir.rename("dir", "newDir");
  setContent(f, "xxx");

  List<Revision> rr = getRevisionsFor(f);
  assertEquals(4, rr.size());

  assertEquals(myRoot.getPath() + "/newDir/file.txt", rr.get(0).findEntry().getPath());
  assertEquals(myRoot.getPath() + "/newDir/file.txt", rr.get(1).findEntry().getPath());
  assertEquals(myRoot.getPath() + "/dir/file.txt", rr.get(2).findEntry().getPath());
}
 
Example 16
Source Project: consulo   File: FileHistoryDialogTest.java    License: 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 17
public void testNames() throws IOException {
  VirtualFile f = createDirectory("foo");
  f.rename(this, "bar");

  List<Revision> revs = getRevisionsFor(f);

  Difference d = new Difference(false, revs.get(0).findEntry(), revs.get(1).findEntry());
  DirectoryChangeModel m = createModelOn(d);

  assertEquals("bar", m.getEntryName(0));
  assertEquals("foo", m.getEntryName(1));
}
 
Example 18
Source Project: consulo   File: RenameFileFix.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) {
  VirtualFile vFile = file.getVirtualFile();
  Document document = PsiDocumentManager.getInstance(project).getDocument(file);
  FileDocumentManager.getInstance().saveDocument(document);
  try {
    vFile.rename(file.getManager(), myNewFileName);
  }
  catch(IOException e){
    MessagesEx.error(project, e.getMessage()).showLater();
  }
}
 
Example 19
public void testRenameFile() throws Exception {
  final VirtualFile file = createFile("file.txt");
  XLineBreakpoint<?> b = putBreakpoint(file);
  file.rename(this, "file2.txt");
  assertTrue(b.getFileUrl().endsWith("file2.txt"));
  assertSame(b, getBreakpointManager().findBreakpointAtLine(XDebuggerTestCase.MY_LINE_BREAKPOINT_TYPE, file, 0));
}
 
Example 20
Source Project: consulo   File: ApplyFilePatchBase.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static VirtualFile findPatchTarget(final ApplyPatchContext context, final String beforeName, final String afterName,
                                          final boolean isNewFile) throws IOException {
  VirtualFile file = null;
  if (beforeName != null) {
    file = findFileToPatchByName(context, beforeName, isNewFile);
  }
  if (file == null) {
    file = findFileToPatchByName(context, afterName, isNewFile);
  }
  else if (context.isAllowRename() && afterName != null && !beforeName.equals(afterName)) {
    String[] beforeNameComponents = beforeName.split("/");
    String[] afterNameComponents = afterName.split("/");
    if (!beforeNameComponents [beforeNameComponents.length-1].equals(afterNameComponents [afterNameComponents.length-1])) {
      context.registerBeforeRename(file);
      file.rename(FilePatch.class, afterNameComponents [afterNameComponents.length-1]);
    }
    boolean needMove = (beforeNameComponents.length != afterNameComponents.length);
    if (!needMove) {
      needMove = checkPackageRename(context, beforeNameComponents, afterNameComponents);
    }
    if (needMove) {
      VirtualFile moveTarget = findFileToPatchByComponents(context, afterNameComponents, afterNameComponents.length-1);
      if (moveTarget == null) {
        return null;
      }
      context.registerBeforeRename(file);
      file.move(FilePatch.class, moveTarget);
    }
  }
  return file;
}