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>
100 lines
3.5 KiB
Rust
100 lines
3.5 KiB
Rust
use std::path::Path;
|
|
|
|
use anyhow::Context as _;
|
|
use gpui::App;
|
|
use settings::{Settings, SettingsContent};
|
|
use util::{
|
|
ResultExt,
|
|
paths::{PathMatcher, PathStyle},
|
|
rel_path::RelPath,
|
|
};
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
pub struct WorktreeSettings {
|
|
pub project_name: Option<String>,
|
|
pub file_scan_inclusions: PathMatcher,
|
|
pub file_scan_exclusions: PathMatcher,
|
|
pub private_files: PathMatcher,
|
|
}
|
|
|
|
impl WorktreeSettings {
|
|
pub fn is_path_private(&self, path: &RelPath) -> bool {
|
|
path.ancestors()
|
|
.any(|ancestor| self.private_files.is_match(ancestor.as_std_path()))
|
|
}
|
|
|
|
pub fn is_path_excluded(&self, path: &RelPath) -> bool {
|
|
path.ancestors()
|
|
.any(|ancestor| self.file_scan_exclusions.is_match(ancestor.as_std_path()))
|
|
}
|
|
|
|
pub fn is_path_always_included(&self, path: &RelPath) -> bool {
|
|
path.ancestors()
|
|
.any(|ancestor| self.file_scan_inclusions.is_match(ancestor.as_std_path()))
|
|
}
|
|
}
|
|
|
|
impl Settings for WorktreeSettings {
|
|
fn from_settings(content: &settings::SettingsContent, _cx: &mut App) -> Self {
|
|
let worktree = content.project.worktree.clone();
|
|
let file_scan_exclusions = worktree.file_scan_exclusions.unwrap();
|
|
let file_scan_inclusions = worktree.file_scan_inclusions.unwrap();
|
|
let private_files = worktree.private_files.unwrap().0;
|
|
let parsed_file_scan_inclusions: Vec<String> = file_scan_inclusions
|
|
.iter()
|
|
.flat_map(|glob| {
|
|
Path::new(glob)
|
|
.ancestors()
|
|
.map(|a| a.to_string_lossy().into())
|
|
})
|
|
.filter(|p: &String| !p.is_empty())
|
|
.collect();
|
|
|
|
Self {
|
|
project_name: None,
|
|
file_scan_exclusions: path_matchers(file_scan_exclusions, "file_scan_exclusions")
|
|
.log_err()
|
|
.unwrap_or_default(),
|
|
file_scan_inclusions: path_matchers(
|
|
parsed_file_scan_inclusions,
|
|
"file_scan_inclusions",
|
|
)
|
|
.unwrap(),
|
|
private_files: path_matchers(private_files, "private_files")
|
|
.log_err()
|
|
.unwrap_or_default(),
|
|
}
|
|
}
|
|
|
|
fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut SettingsContent) {
|
|
if let Some(inclusions) = vscode
|
|
.read_value("files.watcherInclude")
|
|
.and_then(|v| v.as_array())
|
|
.and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
|
|
{
|
|
if let Some(old) = current.project.worktree.file_scan_inclusions.as_mut() {
|
|
old.extend(inclusions)
|
|
} else {
|
|
current.project.worktree.file_scan_inclusions = Some(inclusions)
|
|
}
|
|
}
|
|
if let Some(exclusions) = vscode
|
|
.read_value("files.watcherExclude")
|
|
.and_then(|v| v.as_array())
|
|
.and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
|
|
{
|
|
if let Some(old) = current.project.worktree.file_scan_exclusions.as_mut() {
|
|
old.extend(exclusions)
|
|
} else {
|
|
current.project.worktree.file_scan_exclusions = Some(exclusions)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn path_matchers(mut values: Vec<String>, context: &'static str) -> anyhow::Result<PathMatcher> {
|
|
values.sort();
|
|
PathMatcher::new(values, PathStyle::local())
|
|
.with_context(|| format!("Failed to parse globs from {}", context))
|
|
}
|