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
+51
-17
@@ -1,4 +1,4 @@
|
||||
use anyhow::Result;
|
||||
use anyhow::{Result, anyhow};
|
||||
use collections::{HashMap, HashSet};
|
||||
use command_palette_hooks::CommandInterceptResult;
|
||||
use editor::{
|
||||
@@ -6,7 +6,7 @@ use editor::{
|
||||
actions::{SortLinesCaseInsensitive, SortLinesCaseSensitive},
|
||||
display_map::ToDisplayPoint,
|
||||
};
|
||||
use gpui::{Action, App, AppContext as _, Context, Global, Keystroke, Window, actions};
|
||||
use gpui::{Action, App, AppContext as _, Context, Global, Keystroke, Task, Window, actions};
|
||||
use itertools::Itertools;
|
||||
use language::Point;
|
||||
use multi_buffer::MultiBufferRow;
|
||||
@@ -22,12 +22,12 @@ use std::{
|
||||
path::Path,
|
||||
process::Stdio,
|
||||
str::Chars,
|
||||
sync::{Arc, OnceLock},
|
||||
sync::OnceLock,
|
||||
time::Instant,
|
||||
};
|
||||
use task::{HideStrategy, RevealStrategy, SpawnInTerminal, TaskId};
|
||||
use ui::ActiveTheme;
|
||||
use util::ResultExt;
|
||||
use util::{ResultExt, rel_path::RelPath};
|
||||
use workspace::{Item, SaveIntent, notifications::NotifyResultExt};
|
||||
use workspace::{SplitDirection, notifications::DetachAndPromptErr};
|
||||
use zed_actions::{OpenDocs, RevealTarget};
|
||||
@@ -305,31 +305,54 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
|
||||
let Some(worktree) = project.read(cx).visible_worktrees(cx).next() else {
|
||||
return;
|
||||
};
|
||||
let project_path = ProjectPath {
|
||||
worktree_id: worktree.read(cx).id(),
|
||||
path: Arc::from(Path::new(&action.filename)),
|
||||
let path_style = worktree.read(cx).path_style();
|
||||
let Ok(project_path) = RelPath::from_std_path(Path::new(&action.filename), path_style)
|
||||
.map(|path| ProjectPath {
|
||||
worktree_id: worktree.read(cx).id(),
|
||||
path,
|
||||
})
|
||||
else {
|
||||
// TODO implement save_as with absolute path
|
||||
Task::ready(Err::<(), _>(anyhow!(
|
||||
"Cannot save buffer with absolute path"
|
||||
)))
|
||||
.detach_and_prompt_err(
|
||||
"Failed to save",
|
||||
window,
|
||||
cx,
|
||||
|_, _, _| None,
|
||||
);
|
||||
return;
|
||||
};
|
||||
|
||||
if project.read(cx).entry_for_path(&project_path, cx).is_some() && action.save_intent != Some(SaveIntent::Overwrite) {
|
||||
if project.read(cx).entry_for_path(&project_path, cx).is_some()
|
||||
&& action.save_intent != Some(SaveIntent::Overwrite)
|
||||
{
|
||||
let answer = window.prompt(
|
||||
gpui::PromptLevel::Critical,
|
||||
&format!("{} already exists. Do you want to replace it?", project_path.path.to_string_lossy()),
|
||||
&format!(
|
||||
"{} already exists. Do you want to replace it?",
|
||||
project_path.path.display(path_style)
|
||||
),
|
||||
Some(
|
||||
"A file or folder with the same name already exists. Replacing it will overwrite its current contents.",
|
||||
"A file or folder with the same name already exists. \
|
||||
Replacing it will overwrite its current contents.",
|
||||
),
|
||||
&["Replace", "Cancel"],
|
||||
cx);
|
||||
cx,
|
||||
);
|
||||
cx.spawn_in(window, async move |editor, cx| {
|
||||
if answer.await.ok() != Some(0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let _ = editor.update_in(cx, |editor, window, cx|{
|
||||
let _ = editor.update_in(cx, |editor, window, cx| {
|
||||
editor
|
||||
.save_as(project, project_path, window, cx)
|
||||
.detach_and_prompt_err("Failed to :w", window, cx, |_, _, _| None);
|
||||
});
|
||||
}).detach();
|
||||
})
|
||||
.detach();
|
||||
} else {
|
||||
editor
|
||||
.save_as(project, project_path, window, cx)
|
||||
@@ -348,9 +371,15 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
|
||||
let Some(worktree) = project.read(cx).visible_worktrees(cx).next() else {
|
||||
return;
|
||||
};
|
||||
let path_style = worktree.read(cx).path_style();
|
||||
let Some(path) =
|
||||
RelPath::from_std_path(Path::new(&action.filename), path_style).log_err()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let project_path = ProjectPath {
|
||||
worktree_id: worktree.read(cx).id(),
|
||||
path: Arc::from(Path::new(&action.filename)),
|
||||
path,
|
||||
};
|
||||
|
||||
let direction = if action.vertical {
|
||||
@@ -442,9 +471,15 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
|
||||
let Some(worktree) = project.read(cx).visible_worktrees(cx).next() else {
|
||||
return;
|
||||
};
|
||||
let path_style = worktree.read(cx).path_style();
|
||||
let Some(path) =
|
||||
RelPath::from_std_path(Path::new(&action.filename), path_style).log_err()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let project_path = ProjectPath {
|
||||
worktree_id: worktree.read(cx).id(),
|
||||
path: Arc::from(Path::new(&action.filename)),
|
||||
path,
|
||||
};
|
||||
|
||||
let _ = workspace.update(cx, |workspace, cx| {
|
||||
@@ -1710,9 +1745,8 @@ impl Vim {
|
||||
if let Some((_, buffer, _)) = editor.active_excerpt(cx)
|
||||
&& let Some(file) = buffer.read(cx).file()
|
||||
&& let Some(local) = file.as_local()
|
||||
&& let Some(str) = local.path().to_str()
|
||||
{
|
||||
ret.push_str(str)
|
||||
ret.push_str(&local.path().display(local.path_style(cx)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user