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>
60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
use std::{borrow::Borrow, sync::Arc};
|
|
|
|
use gpui::SharedString;
|
|
use settings::WorktreeId;
|
|
use util::rel_path::RelPath;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
pub struct ManifestName(SharedString);
|
|
|
|
impl Borrow<SharedString> for ManifestName {
|
|
fn borrow(&self) -> &SharedString {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl Borrow<str> for ManifestName {
|
|
fn borrow(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl From<SharedString> for ManifestName {
|
|
fn from(value: SharedString) -> Self {
|
|
Self(value)
|
|
}
|
|
}
|
|
|
|
impl From<ManifestName> for SharedString {
|
|
fn from(value: ManifestName) -> Self {
|
|
value.0
|
|
}
|
|
}
|
|
|
|
impl AsRef<SharedString> for ManifestName {
|
|
fn as_ref(&self) -> &SharedString {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
/// Represents a manifest query; given a path to a file, [ManifestSearcher] is tasked with finding a path to the directory containing the manifest for that file.
|
|
///
|
|
/// Since parts of the path might have already been explored, there's an additional `depth` parameter that indicates to what ancestry level a given path should be explored.
|
|
/// For example, given a path like `foo/bar/baz`, a depth of 2 would explore `foo/bar/baz` and `foo/bar`, but not `foo`.
|
|
pub struct ManifestQuery {
|
|
/// Path to the file, relative to worktree root.
|
|
pub path: Arc<RelPath>,
|
|
pub depth: usize,
|
|
pub delegate: Arc<dyn ManifestDelegate>,
|
|
}
|
|
|
|
pub trait ManifestProvider {
|
|
fn name(&self) -> ManifestName;
|
|
fn search(&self, query: ManifestQuery) -> Option<Arc<RelPath>>;
|
|
}
|
|
|
|
pub trait ManifestDelegate: Send + Sync {
|
|
fn worktree_id(&self) -> WorktreeId;
|
|
fn exists(&self, path: &RelPath, is_dir: Option<bool>) -> bool;
|
|
}
|