Java Code Examples for com.intellij.openapi.vfs.LocalFileSystem#refreshAndFindFileByIoFile()

The following examples show how to use com.intellij.openapi.vfs.LocalFileSystem#refreshAndFindFileByIoFile() . 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: VfsUtils.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to resolve the given file path to a {@link VirtualFile}.
 *
 * <p>WARNING: Refreshing files on the EDT may freeze the IDE.
 *
 * @param refreshIfNeeded whether to refresh the file in the VFS, if it is not already cached.
 *     Will only refresh if called on the EDT.
 */
@Nullable
public static VirtualFile resolveVirtualFile(File file, boolean refreshIfNeeded) {
  LocalFileSystem fileSystem = VirtualFileSystemProvider.getInstance().getSystem();
  VirtualFile vf = fileSystem.findFileByPathIfCached(file.getPath());
  if (vf != null) {
    return vf;
  }
  vf = fileSystem.findFileByIoFile(file);
  if (vf != null && vf.isValid()) {
    return vf;
  }
  boolean shouldRefresh =
      refreshIfNeeded && ApplicationManager.getApplication().isDispatchThread();
  return shouldRefresh ? fileSystem.refreshAndFindFileByIoFile(file) : null;
}
 
Example 2
Source File: SyncProjectDialog.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@Nullable
public VirtualFile getDirectory() {
    if (directoryField == null) {
        return null;
    }
    String text = directoryField.getText();
    if (text == null || text.trim().isEmpty()) {
        return null;
    }
    final File parent = new File(text);
    LocalFileSystem lfs = LocalFileSystem.getInstance();
    VirtualFile file = lfs.findFileByIoFile(parent);
    if (file == null) {
        file = lfs.refreshAndFindFileByIoFile(parent);
    }
    return file;
}
 
Example 3
Source File: PersistentFSTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testFindRootShouldNotBeFooledByRelativePath() throws IOException {
  File tmp = createTempDirectory();
  File x = new File(tmp, "x.jar");
  x.createNewFile();
  LocalFileSystem lfs = LocalFileSystem.getInstance();
  VirtualFile vx = lfs.refreshAndFindFileByIoFile(x);
  assertNotNull(vx);
  ArchiveFileSystem jfs = (ArchiveFileSystem)StandardFileSystems.jar();
  VirtualFile root = ArchiveVfsUtil.getArchiveRootForLocalFile(vx);

  PersistentFS fs = PersistentFS.getInstance();

  String path = vx.getPath() + "/../" + vx.getName() + ArchiveFileSystem.ARCHIVE_SEPARATOR;
  NewVirtualFile root1 = fs.findRoot(path, (NewVirtualFileSystem)jfs);

  assertSame(root1, root);
}
 
Example 4
Source File: FileContent.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static FileContent createFromTempFile(Project project, String name, String ext, @Nonnull byte[] content) throws IOException {
  File tempFile = FileUtil.createTempFile(name, "." + ext);
  if (content.length != 0) {
    FileUtil.writeToFile(tempFile, content);
  }
  tempFile.deleteOnExit();
  final LocalFileSystem lfs = LocalFileSystem.getInstance();
  VirtualFile file = lfs.findFileByIoFile(tempFile);
  if (file == null) {
    file = lfs.refreshAndFindFileByIoFile(tempFile);
  }
  if (file != null) {
    return new FileContent(project, file);
  }
  throw new IOException("Can not create temp file for revision content");
}
 
Example 5
Source File: FileDropHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void openFiles(final Project project, final List<File> fileList, EditorWindow editorWindow) {
  if (editorWindow == null && myEditor != null) {
    editorWindow = findEditorWindow(project);
  }
  final LocalFileSystem fileSystem = LocalFileSystem.getInstance();
  for (File file : fileList) {
    final VirtualFile vFile = fileSystem.refreshAndFindFileByIoFile(file);
    final FileEditorManagerEx fileEditorManager = (FileEditorManagerEx) FileEditorManager.getInstance(project);
    if (vFile != null) {
      NonProjectFileWritingAccessProvider.allowWriting(vFile);

      if (editorWindow != null) {
        fileEditorManager.openFileWithProviders(vFile, true, editorWindow);
      }
      else {
        new OpenFileDescriptor(project, vFile).navigate(true);
      }
    }
  }
}
 
Example 6
Source File: CompilerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void refreshIODirectories(@Nonnull final Collection<File> files) {
  final LocalFileSystem lfs = LocalFileSystem.getInstance();
  final List<VirtualFile> filesToRefresh = new ArrayList<VirtualFile>();
  for (File file : files) {
    final VirtualFile virtualFile = lfs.refreshAndFindFileByIoFile(file);
    if (virtualFile != null) {
      filesToRefresh.add(virtualFile);
    }
  }
  if (!filesToRefresh.isEmpty()) {
    RefreshQueue.getInstance().refresh(false, true, null, filesToRefresh);
  }
}
 
Example 7
Source File: PatchApplier.java    From consulo with Apache License 2.0 5 votes vote down vote up
@CalledInAwt
public static void refreshPassedFilesAndMoveToChangelist(@Nonnull final Project project,
                                                         final Collection<FilePath> directlyAffected,
                                                         final Collection<VirtualFile> indirectlyAffected,
                                                         final Consumer<Collection<FilePath>> targetChangelistMover) {
  final LocalFileSystem lfs = LocalFileSystem.getInstance();
  for (FilePath filePath : directlyAffected) {
    lfs.refreshAndFindFileByIoFile(filePath.getIOFile());
  }
  if (project.isDisposed()) return;

  final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
  if (! directlyAffected.isEmpty() && targetChangelistMover != null) {
    changeListManager.invokeAfterUpdate(new Runnable() {
                                          @Override
                                          public void run() {
                                            targetChangelistMover.consume(directlyAffected);
                                          }
                                        }, InvokeAfterUpdateMode.SYNCHRONOUS_CANCELLABLE,
                                        VcsBundle.message("change.lists.manager.move.changes.to.list"),
                                        new Consumer<VcsDirtyScopeManager>() {
                                          @Override
                                          public void consume(final VcsDirtyScopeManager vcsDirtyScopeManager) {
                                            markDirty(vcsDirtyScopeManager, directlyAffected, indirectlyAffected);
                                          }
                                        }, null);
  } else {
    markDirty(VcsDirtyScopeManager.getInstance(project), directlyAffected, indirectlyAffected);
  }
}
 
Example 8
Source File: FileGroupingProjectNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
public FileGroupingProjectNode(Project project, File file, ViewSettings viewSettings) {
  super(project, file, viewSettings);
  final LocalFileSystem lfs = LocalFileSystem.getInstance();
  myVirtualFile = lfs.findFileByIoFile(file);
  if (myVirtualFile == null) {
    myVirtualFile = lfs.refreshAndFindFileByIoFile(file);
  }
}