Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

util: Preserve path separators when parsing file positions (#66089)

buvidk1234347f171a3fd0
diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs
index f6cf1030f86..95426d2e125 100644
--- a/crates/agent_ui/src/conversation_view/thread_view.rs
+++ b/crates/agent_ui/src/conversation_view/thread_view.rs
@@ -15179,18 +15179,14 @@ mod tests {
vec![
Path::new(path!("/project"))
.join("crates/editor/src/inlays/inlay_hints.rs:394"),
- Path::new(path!("/project"))
- .join("crates/editor/src/inlays")
- .join("inlay_hints.rs"),
+ Path::new(path!("/project")).join("crates/editor/src/inlays/inlay_hints.rs"),
],
),
(
"crates/editor/src/element.rs:608".to_string(),
vec![
Path::new(path!("/project")).join("crates/editor/src/element.rs:608"),
- Path::new(path!("/project"))
- .join("crates/editor/src")
- .join("element.rs"),
+ Path::new(path!("/project")).join("crates/editor/src/element.rs"),
],
),
(missing_file_uri.clone(), Vec::new()),
@@ -15198,9 +15194,7 @@ mod tests {
"../outside/missing.md:2".to_string(),
vec![
Path::new(path!("/project")).join("../outside/missing.md:2"),
- Path::new(path!("/project"))
- .join("../outside")
- .join("missing.md"),
+ Path::new(path!("/project")).join("../outside/missing.md"),
],
),
(
diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs
index 03c595ce88e..510294da6f6 100644
--- a/crates/util/src/paths.rs
+++ b/crates/util/src/paths.rs
@@ -647,6 +647,14 @@ impl PathWithPosition {
// in the future seems unlikely.
static SUFFIX_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
+ // `Path::with_file_name` inserts the host's separator, which can
+ // corrupt Unix paths when the editor runs on Windows.
+ let replace_file_name = |input: &str, file_name: &str, replacement: &str| {
+ input.rsplit_once(file_name).map_or_else(
+ || PathBuf::from(input),
+ |(prefix, _)| PathBuf::from(format!("{prefix}{replacement}")),
+ )
+ };
match SUFFIX_RE
.captures(maybe_file_name_with_row_col)
.and_then(|captures| {
@@ -659,7 +667,11 @@ impl PathWithPosition {
let column = maybe_column.parse::<u32>().ok();
Self {
- path: path.with_file_name(&maybe_file_name_with_row_col[..file_name_end]),
+ path: replace_file_name(
+ trimmed,
+ maybe_file_name_with_row_col,
+ &maybe_file_name_with_row_col[..file_name_end],
+ ),
row,
column,
}
@@ -703,7 +715,7 @@ impl PathWithPosition {
Self {
path: if row.is_some() {
- path.with_file_name(path_string)
+ replace_file_name(s, file_name, &path_string)
} else {
path.to_path_buf()
},
@@ -2482,6 +2494,54 @@ mod tests {
}
}
+ #[test]
+ fn path_with_position_preserves_path_separators() {
+ for (input, expected_path, expected_column) in [
+ (
+ "/home/user/project/src/main.rs:49",
+ "/home/user/project/src/main.rs",
+ None,
+ ),
+ (
+ "/home/user/project/src/main.rs:49:7",
+ "/home/user/project/src/main.rs",
+ Some(7),
+ ),
+ (
+ "/home/user/project/src/main.rs:49:in",
+ "/home/user/project/src/main.rs",
+ None,
+ ),
+ ("src/main.rs(49,7)", "src/main.rs", Some(7)),
+ ("src/main.rs:49/", "src/main.rs", None),
+ ("src/main.rs:49/.", "src/main.rs", None),
+ ("parent//file.rs:49", "parent//file.rs", None),
+ ("parent/./file.rs:49", "parent/./file.rs", None),
+ ("parent/😀.rs:49", "parent/😀.rs", None),
+ ("C:/project/src/main.rs:49", "C:/project/src/main.rs", None),
+ (
+ r"C:\project\src\main.rs:49",
+ r"C:\project\src\main.rs",
+ None,
+ ),
+ (r"src\mixed/main.rs:49", r"src\mixed/main.rs", None),
+ (
+ r"\\server\share/main.rs:49",
+ r"\\server\share/main.rs",
+ None,
+ ),
+ ] {
+ let parsed = PathWithPosition::parse_str(input);
+ assert_eq!(
+ parsed.path.as_os_str(),
+ OsStr::new(expected_path),
+ "{input}"
+ );
+ assert_eq!(parsed.row, Some(49), "{input}");
+ assert_eq!(parsed.column, expected_column, "{input}");
+ }
+ }
+
#[test]
fn path_with_position_preserves_repeated_filename() {
for (input, expected_path) in [