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
@@ -41,13 +41,12 @@ use std::{
|
||||
cell::Cell,
|
||||
cmp::{self, Ordering, Reverse},
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
ffi::OsStr,
|
||||
future::Future,
|
||||
iter::{self, Iterator, Peekable},
|
||||
mem,
|
||||
num::NonZeroU32,
|
||||
ops::{Deref, Range},
|
||||
path::{Path, PathBuf},
|
||||
path::PathBuf,
|
||||
rc,
|
||||
sync::{Arc, LazyLock},
|
||||
time::{Duration, Instant},
|
||||
@@ -65,7 +64,7 @@ pub use text::{
|
||||
use theme::{ActiveTheme as _, SyntaxTheme};
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
use util::RandomCharIter;
|
||||
use util::{RangeExt, debug_panic, maybe};
|
||||
use util::{RangeExt, debug_panic, maybe, paths::PathStyle, rel_path::RelPath};
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub use {tree_sitter_python, tree_sitter_rust, tree_sitter_typescript};
|
||||
@@ -349,15 +348,18 @@ pub trait File: Send + Sync + Any {
|
||||
fn disk_state(&self) -> DiskState;
|
||||
|
||||
/// Returns the path of this file relative to the worktree's root directory.
|
||||
fn path(&self) -> &Arc<Path>;
|
||||
fn path(&self) -> &Arc<RelPath>;
|
||||
|
||||
/// Returns the path of this file relative to the worktree's parent directory (this means it
|
||||
/// includes the name of the worktree's root folder).
|
||||
fn full_path(&self, cx: &App) -> PathBuf;
|
||||
|
||||
/// Returns the path style of this file.
|
||||
fn path_style(&self, cx: &App) -> PathStyle;
|
||||
|
||||
/// Returns the last component of this handle's absolute path. If this handle refers to the root
|
||||
/// of its worktree, then this method will return the name of the worktree itself.
|
||||
fn file_name<'a>(&'a self, cx: &'a App) -> &'a OsStr;
|
||||
fn file_name<'a>(&'a self, cx: &'a App) -> &'a str;
|
||||
|
||||
/// Returns the id of the worktree to which this file belongs.
|
||||
///
|
||||
@@ -4626,13 +4628,12 @@ impl BufferSnapshot {
|
||||
self.file.as_ref()
|
||||
}
|
||||
|
||||
/// Resolves the file path (relative to the worktree root) associated with the underlying file.
|
||||
pub fn resolve_file_path(&self, cx: &App, include_root: bool) -> Option<PathBuf> {
|
||||
if let Some(file) = self.file() {
|
||||
if file.path().file_name().is_none() || include_root {
|
||||
Some(file.full_path(cx))
|
||||
} else {
|
||||
Some(file.path().to_path_buf())
|
||||
Some(file.path().as_std_path().to_owned())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
@@ -5117,19 +5118,19 @@ impl IndentSize {
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub struct TestFile {
|
||||
pub path: Arc<Path>,
|
||||
pub path: Arc<RelPath>,
|
||||
pub root_name: String,
|
||||
pub local_root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
impl File for TestFile {
|
||||
fn path(&self) -> &Arc<Path> {
|
||||
fn path(&self) -> &Arc<RelPath> {
|
||||
&self.path
|
||||
}
|
||||
|
||||
fn full_path(&self, _: &gpui::App) -> PathBuf {
|
||||
PathBuf::from(&self.root_name).join(self.path.as_ref())
|
||||
PathBuf::from(self.root_name.clone()).join(self.path.as_std_path())
|
||||
}
|
||||
|
||||
fn as_local(&self) -> Option<&dyn LocalFile> {
|
||||
@@ -5144,7 +5145,7 @@ impl File for TestFile {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn file_name<'a>(&'a self, _: &'a gpui::App) -> &'a std::ffi::OsStr {
|
||||
fn file_name<'a>(&'a self, _: &'a gpui::App) -> &'a str {
|
||||
self.path().file_name().unwrap_or(self.root_name.as_ref())
|
||||
}
|
||||
|
||||
@@ -5159,6 +5160,10 @@ impl File for TestFile {
|
||||
fn is_private(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn path_style(&self, _cx: &App) -> PathStyle {
|
||||
PathStyle::local()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
@@ -5166,7 +5171,7 @@ impl LocalFile for TestFile {
|
||||
fn abs_path(&self, _cx: &App) -> PathBuf {
|
||||
PathBuf::from(self.local_root.as_ref().unwrap())
|
||||
.join(&self.root_name)
|
||||
.join(self.path.as_ref())
|
||||
.join(self.path.as_std_path())
|
||||
}
|
||||
|
||||
fn load(&self, _cx: &App) -> Task<Result<String>> {
|
||||
|
||||
Reference in New Issue
Block a user