Represent relative paths using a dedicated, separator-agnostic type (#38744)
Closes https://github.com/zed-industries/zed/issues/38690 Closes #37353 ### Background On Windows, paths are normally separated by `\`, unlike mac and linux where they are separated by `/`. When editing code in a project that uses a different path style than your local system (e.g. remoting from Windows to Linux, using WSL, and collaboration between windows and unix users), the correct separator for a path may differ from the "native" separator. Previously, to work around this, Zed converted paths' separators in numerous places. This was applied to both absolute and relative paths, leading to incorrect conversions in some cases. ### Solution Many code paths in Zed use paths that are *relative* to either a worktree root or a git repository. This PR introduces a dedicated type for these paths called `RelPath`, which stores the path in the same way regardless of host platform, and offers `Path`-like manipulation APIs. RelPath supports *displaying* the path using either separator, so that we can display paths in a style that is determined at runtime based on the current project. The representation of absolute paths is left untouched, for now. Absolute paths are different from relative paths because (except in contexts where we know that the path refers to the local filesystem) they should generally be treated as opaque strings. Currently we use a mix of types for these paths (std::path::Path, String, SanitizedPath). Release Notes: - N/A --------- Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Co-authored-by: Peter Tripp <petertripp@gmail.com> Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com> Co-authored-by: Lukas Wirth <me@lukaswirth.dev>
This commit is contained in:
co-authored by
Cole Miller
Piotr Osiewicz
Peter Tripp
Smit Barmase
Lukas Wirth
parent
3c626f3758
commit
03f9cf4414
@@ -17,6 +17,7 @@ use parking_lot::Mutex;
|
||||
use rope::Rope;
|
||||
use smol::future::FutureExt as _;
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
use util::{paths::PathStyle, rel_path::RelPath};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FakeGitRepository {
|
||||
@@ -82,7 +83,7 @@ impl GitRepository for FakeGitRepository {
|
||||
self.with_state_async(false, move |state| {
|
||||
state
|
||||
.index_contents
|
||||
.get(path.as_ref())
|
||||
.get(&path)
|
||||
.context("not present in index")
|
||||
.cloned()
|
||||
})
|
||||
@@ -97,7 +98,7 @@ impl GitRepository for FakeGitRepository {
|
||||
self.with_state_async(false, move |state| {
|
||||
state
|
||||
.head_contents
|
||||
.get(path.as_ref())
|
||||
.get(&path)
|
||||
.context("not present in HEAD")
|
||||
.cloned()
|
||||
})
|
||||
@@ -225,6 +226,7 @@ impl GitRepository for FakeGitRepository {
|
||||
.read_file_sync(path)
|
||||
.ok()
|
||||
.map(|content| String::from_utf8(content).unwrap())?;
|
||||
let repo_path = RelPath::from_std_path(repo_path, PathStyle::local()).ok()?;
|
||||
Some((repo_path.into(), (content, is_ignored)))
|
||||
})
|
||||
.collect();
|
||||
@@ -386,7 +388,11 @@ impl GitRepository for FakeGitRepository {
|
||||
let contents = paths
|
||||
.into_iter()
|
||||
.map(|path| {
|
||||
let abs_path = self.dot_git_path.parent().unwrap().join(&path);
|
||||
let abs_path = self
|
||||
.dot_git_path
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join(&path.as_std_path());
|
||||
Box::pin(async move { (path.clone(), self.fs.load(&abs_path).await.ok()) })
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
+14
-40
@@ -47,7 +47,7 @@ use collections::{BTreeMap, btree_map};
|
||||
use fake_git_repo::FakeGitRepositoryState;
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
use git::{
|
||||
repository::RepoPath,
|
||||
repository::{RepoPath, repo_path},
|
||||
status::{FileStatus, StatusCode, TrackedStatus, UnmergedStatus},
|
||||
};
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
@@ -1608,13 +1608,13 @@ impl FakeFs {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn set_index_for_repo(&self, dot_git: &Path, index_state: &[(RepoPath, String)]) {
|
||||
pub fn set_index_for_repo(&self, dot_git: &Path, index_state: &[(&str, String)]) {
|
||||
self.with_git_state(dot_git, true, |state| {
|
||||
state.index_contents.clear();
|
||||
state.index_contents.extend(
|
||||
index_state
|
||||
.iter()
|
||||
.map(|(path, content)| (path.clone(), content.clone())),
|
||||
.map(|(path, content)| (repo_path(path), content.clone())),
|
||||
);
|
||||
})
|
||||
.unwrap();
|
||||
@@ -1623,7 +1623,7 @@ impl FakeFs {
|
||||
pub fn set_head_for_repo(
|
||||
&self,
|
||||
dot_git: &Path,
|
||||
head_state: &[(RepoPath, String)],
|
||||
head_state: &[(&str, String)],
|
||||
sha: impl Into<String>,
|
||||
) {
|
||||
self.with_git_state(dot_git, true, |state| {
|
||||
@@ -1631,50 +1631,22 @@ impl FakeFs {
|
||||
state.head_contents.extend(
|
||||
head_state
|
||||
.iter()
|
||||
.map(|(path, content)| (path.clone(), content.clone())),
|
||||
.map(|(path, content)| (repo_path(path), content.clone())),
|
||||
);
|
||||
state.refs.insert("HEAD".into(), sha.into());
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn set_git_content_for_repo(
|
||||
&self,
|
||||
dot_git: &Path,
|
||||
head_state: &[(RepoPath, String, Option<String>)],
|
||||
) {
|
||||
pub fn set_head_and_index_for_repo(&self, dot_git: &Path, contents_by_path: &[(&str, String)]) {
|
||||
self.with_git_state(dot_git, true, |state| {
|
||||
state.head_contents.clear();
|
||||
state.head_contents.extend(
|
||||
head_state
|
||||
contents_by_path
|
||||
.iter()
|
||||
.map(|(path, head_content, _)| (path.clone(), head_content.clone())),
|
||||
.map(|(path, contents)| (repo_path(path), contents.clone())),
|
||||
);
|
||||
state.index_contents.clear();
|
||||
state.index_contents.extend(head_state.iter().map(
|
||||
|(path, head_content, index_content)| {
|
||||
(
|
||||
path.clone(),
|
||||
index_content.as_ref().unwrap_or(head_content).clone(),
|
||||
)
|
||||
},
|
||||
));
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn set_head_and_index_for_repo(
|
||||
&self,
|
||||
dot_git: &Path,
|
||||
contents_by_path: &[(RepoPath, String)],
|
||||
) {
|
||||
self.with_git_state(dot_git, true, |state| {
|
||||
state.head_contents.clear();
|
||||
state.index_contents.clear();
|
||||
state.head_contents.extend(contents_by_path.iter().cloned());
|
||||
state
|
||||
.index_contents
|
||||
.extend(contents_by_path.iter().cloned());
|
||||
state.index_contents = state.head_contents.clone();
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
@@ -1689,7 +1661,7 @@ impl FakeFs {
|
||||
|
||||
/// Put the given git repository into a state with the given status,
|
||||
/// by mutating the head, index, and unmerged state.
|
||||
pub fn set_status_for_repo(&self, dot_git: &Path, statuses: &[(&Path, FileStatus)]) {
|
||||
pub fn set_status_for_repo(&self, dot_git: &Path, statuses: &[(&str, FileStatus)]) {
|
||||
let workdir_path = dot_git.parent().unwrap();
|
||||
let workdir_contents = self.files_with_contents(workdir_path);
|
||||
self.with_git_state(dot_git, true, |state| {
|
||||
@@ -1697,10 +1669,12 @@ impl FakeFs {
|
||||
state.head_contents.clear();
|
||||
state.unmerged_paths.clear();
|
||||
for (path, content) in workdir_contents {
|
||||
let repo_path: RepoPath = path.strip_prefix(&workdir_path).unwrap().into();
|
||||
use util::{paths::PathStyle, rel_path::RelPath};
|
||||
|
||||
let repo_path: RepoPath = RelPath::from_std_path(path.strip_prefix(&workdir_path).unwrap(), PathStyle::local()).unwrap().into();
|
||||
let status = statuses
|
||||
.iter()
|
||||
.find_map(|(p, status)| (**p == *repo_path.0).then_some(status));
|
||||
.find_map(|(p, status)| (*p == repo_path.as_str()).then_some(status));
|
||||
let mut content = String::from_utf8_lossy(&content).to_string();
|
||||
|
||||
let mut index_content = None;
|
||||
|
||||
Reference in New Issue
Block a user