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

The following examples show how to use com.intellij.openapi.vfs.LocalFileSystem#findFileByIoFile() . 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: 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 4
Source File: FilePathImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Charset getCharset(Project project) {
  // try to find existing virtual file
  VirtualFile existing = myVirtualFile != null && myVirtualFile.isValid() ? myVirtualFile : null;
  if (existing == null) {
    LocalFileSystem lfs = LocalFileSystem.getInstance();
    for (File f = myFile; f != null; f = f.getParentFile()) {
      existing = lfs.findFileByIoFile(f);
      if (existing != null && existing.isValid()) {
        break;
      }
    }
  }
  if (existing != null) {
    Charset rc = existing.getCharset();
    if (rc != null) {
      return rc;
    }
  }
  EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null;
  if (e == null) {
    e = EncodingManager.getInstance();
  }
  return e.getDefaultCharset();
}
 
Example 5
Source File: FilePathImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static FilePathImpl createForDeletedFile(final File selectedFile, final boolean isDirectory) {
  LocalFileSystem lfs = LocalFileSystem.getInstance();

  File parentFile = selectedFile.getParentFile();
  if (parentFile == null) {
    return new FilePathImpl(selectedFile, isDirectory);
  }

  VirtualFile virtualFileParent = lfs.findFileByIoFile(parentFile);
  if (virtualFileParent != null) {
    return new FilePathImpl(virtualFileParent, selectedFile.getName(), isDirectory, true);
  }
  else {
    return new FilePathImpl(selectedFile, isDirectory);
  }
}
 
Example 6
Source File: FilePathImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static FilePath createOn(String s) {
  File ioFile = new File(s);
  final LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
  VirtualFile virtualFile = localFileSystem.findFileByIoFile(ioFile);
  if (virtualFile != null) {
    return new FilePathImpl(virtualFile);
  }
  else {
    VirtualFile virtualFileParent = localFileSystem.findFileByIoFile(ioFile.getParentFile());
    if (virtualFileParent != null) {
      return new FilePathImpl(virtualFileParent, ioFile.getName(), false);
    }
    else {
      return null;
    }
  }
}
 
Example 7
Source File: ProjectImporterCheckoutListener.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processCheckedOutDirectory(Project project, File directory) {
  final File[] files = directory.listFiles();
  if (files != null) {
    final LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
    for (File file : files) {
      if (file.isDirectory()) continue;
      final VirtualFile virtualFile = localFileSystem.findFileByIoFile(file);
      if (virtualFile != null) {
        final ProjectOpenProcessor openProcessor = ProjectOpenProcessors.getInstance().findProcessor(file);
        if (openProcessor != null) {
          int rc = Messages.showYesNoDialog(project, VcsBundle .message("checkout.open.project.prompt", files[0].getPath()),
                                            VcsBundle.message("checkout.title"), Messages.getQuestionIcon());
          if (rc == Messages.YES) {
            ProjectUtil.openAsync(virtualFile.getPath(), project, false, UIAccess.current());
          }
          return true;
        }
      }
    }
  }
  return false;
}
 
Example 8
Source File: FileExperimentLoader.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void doInitialize() {
  LocalFileSystem fileSystem = LocalFileSystem.getInstance();
  fileSystem.addRootToWatch(file.getPath(), /* watchRecursively= */ false);
  // We need to look up the file in the VFS or else we don't receive events about it. This works
  // even if the returned VirtualFile is null (because the experiments file doesn't exist yet).
  fileSystem.findFileByIoFile(file);
  ApplicationManager.getApplication()
      .getMessageBus()
      .connect()
      .subscribe(VirtualFileManager.VFS_CHANGES, new RefreshExperimentsListener());
}
 
Example 9
Source File: FilePathImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static FilePathImpl create(File selectedFile, boolean isDirectory) {
  if (selectedFile == null) {
    return null;
  }

  LocalFileSystem lfs = LocalFileSystem.getInstance();

  VirtualFile virtualFile = lfs.findFileByIoFile(selectedFile);
  if (virtualFile != null) {
    return new FilePathImpl(virtualFile);
  }

  return createForDeletedFile(selectedFile, isDirectory);
}
 
Example 10
Source File: RefreshVFsSynchronously.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static VirtualFile findFirstValidVirtualParent(@javax.annotation.Nullable File file) {
  LocalFileSystem lfs = LocalFileSystem.getInstance();
  VirtualFile vf = null;
  while (file != null && (vf == null || !vf.isValid())) {
    vf = lfs.findFileByIoFile(file);
    file = file.getParentFile();
  }
  return vf == null || !vf.isValid() ? null : vf;
}
 
Example 11
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);
  }
}
 
Example 12
Source File: Unity3dPackageOrderEntry.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
public VirtualFile[] getFiles(@Nonnull OrderRootType rootType)
{
	List<VirtualFile> files = new ArrayList<>();

	if(rootType == BinariesOrderRootType.getInstance() || rootType == SourcesOrderRootType.getInstance())
	{
		LocalFileSystem localFileSystem = LocalFileSystem.getInstance();

		// moved to project files
		String projectPackageCache = getProjectPackageCache();
		VirtualFile localFile = localFileSystem.findFileByIoFile(new File(projectPackageCache, myNameWithVersion));
		addDotNetModulesInsideLibrary(files, localFile);

		if(files.isEmpty())
		{
			Unity3dPackageWatcher watcher = Unity3dPackageWatcher.getInstance();
			for(String path : watcher.getPackageDirPaths())
			{
				VirtualFile file = localFileSystem.findFileByIoFile(new File(path, myNameWithVersion));
				addDotNetModulesInsideLibrary(files, file);
			}
		}
	}

	if(files.isEmpty())
	{
		Sdk sdk = getSdk();
		if(sdk != null)
		{
			VirtualFile homeDirectory = sdk.getHomeDirectory();
			if(homeDirectory != null)
			{
				VirtualFile builtInDirectory = homeDirectory.findFileByRelativePath(getBuiltInPackagesRelativePath());
				if(builtInDirectory != null)
				{
					VirtualFile packageDirectory = builtInDirectory.findChild(getPackageName());
					ContainerUtil.addIfNotNull(files, packageDirectory);
				}
			}
		}
	}
	return VfsUtilCore.toVirtualFileArray(files);
}