Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix opening folders whose name ends in a position-like suffix (#59384)
Yan Khachko362035d52aaf
diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rsindex 63bd17101ec..cb190effe45 100644--- a/crates/util/src/paths.rs+++ b/crates/util/src/paths.rs@@ -1664,6 +1664,16 @@ mod tests {use super::*;use util_macros::perf;+ #[test]+ fn test_parse_str_treats_paren_suffix_as_position() {+ // This documents the behavior that causes the folder-drop bug: a name ending in+ // `(N)` is parsed as `name ` + row N. The fix lives in `derive_paths_with_position`,+ // which restores the original path when it exists on disk (file or directory).+ let parsed = PathWithPosition::parse_str("/root/Test (3)");+ assert_eq!(parsed.path, PathBuf::from("/root/Test "));+ assert_eq!(parsed.row, Some(3));+ }+#[test]fn test_join_path_uses_path_style_separator() {let posix_path = PathStyle::Posixdiff --git a/crates/zed/src/zed/open_listener.rs b/crates/zed/src/zed/open_listener.rsindex 2b33614f29c..eb38c1cac92 100644--- a/crates/zed/src/zed/open_listener.rs+++ b/crates/zed/src/zed/open_listener.rs@@ -1063,13 +1063,20 @@ pub async fn derive_paths_with_position(let original_path = Path::new(path_str.as_ref());let mut parsed = PathWithPosition::parse_str(path_str.as_ref());- // If the unparsed path string actually points to a file, use that file instead of parsing out the line/col number.- // Note: The colon syntax is also used to open NTFS alternate data streams (e.g., `file.txt:stream`), which would cause issues.- // However, the colon is not valid in NTFS file names, so we can just skip this logic.- if !cfg!(windows)+ // If the unparsed path string actually points to an existing file or directory, use it+ // instead of parsing out the line/col number. This matters for paths whose final+ // component looks like a position suffix, e.g. a folder named `Test (3)` would+ // otherwise be parsed as `Test ` at row 3.+ // Colon : is not valid in NTFS file names, so skip this logic if colon on windows.+ let has_colon = original_path+ .file_name()+ .and_then(|name| name.to_str())+ .is_none_or(|name| name.contains(':'));++ if (!has_colon || !cfg!(windows))&& parsed.row.is_some()&& parsed.path != original_path- && fs.is_file(original_path).await+ && (fs.is_file(original_path).await || fs.is_dir(original_path).await){parsed = PathWithPosition::from_path(original_path.to_path_buf());}@@ -1201,6 +1208,100 @@ mod tests {}}+ #[gpui::test]+ async fn test_derive_paths_with_position_directory_with_position_like_name(+ cx: &mut TestAppContext,+ ) {+ let app_state = init_test(cx);+ let fs = app_state.fs.as_fake();++ // A folder whose name ends in `(N)` or `(row,col)` would otherwise be parsed as a+ // path with a row/column suffix (e.g. the MSVC-style `file.c(22)`), truncating the name.+ fs.insert_tree(+ path!("/root"),+ json!({+ "TEST (1)": {},+ "Project (2,3)": {},+ "test 123": {},+ }),+ )+ .await;++ let inputs = vec![+ path!("/root/TEST (1)").to_string(),+ path!("/root/Project (2,3)").to_string(),+ path!("/root/test 123").to_string(),+ ];+ let result = derive_paths_with_position(fs.as_ref(), inputs).await;++ let paths: Vec<_> = result+ .iter()+ .map(|p| (p.path.to_string_lossy().to_string(), p.row, p.column))+ .collect();+ assert_eq!(+ paths,+ vec![+ (path!("/root/TEST (1)").to_string(), None, None),+ (path!("/root/Project (2,3)").to_string(), None, None),+ (path!("/root/test 123").to_string(), None, None),+ ]+ );+ }++ // Test file with colon (`:`) in the name on non-Windows platforms,+ // as it is valid for file names on Unix-like systems.+ #[cfg(not(target_os = "windows"))]+ #[gpui::test]+ async fn test_derive_paths_with_position_colon_in_name_reverts_on_unix(+ cx: &mut TestAppContext,+ ) {+ let app_state = init_test(cx);+ let fs = app_state.fs.as_fake();++ fs.insert_tree(path!("/root"), json!({ "test.txt:10": "" }))+ .await;++ let result =+ derive_paths_with_position(fs.as_ref(), vec![path!("/root/test.txt:10").to_string()])+ .await;++ let paths: Vec<_> = result+ .iter()+ .map(|p| (p.path.to_string_lossy().to_string(), p.row, p.column))+ .collect();+ assert_eq!(+ paths,+ vec![(path!("/root/test.txt:10").to_string(), None, None)]+ );+ }++ // On Windows `:` is used to delimit NTFS alternate data streams,+ // `notes.txt:10` should be parsed as `notes.txt` at row 10+ #[cfg(target_os = "windows")]+ #[gpui::test]+ async fn test_derive_paths_with_position_colon_in_name_parsed_as_position_on_windows(+ cx: &mut TestAppContext,+ ) {+ let app_state = init_test(cx);+ let fs = app_state.fs.as_fake();++ fs.insert_tree(path!("/root"), json!({ "test.txt": "" }))+ .await;++ let result =+ derive_paths_with_position(fs.as_ref(), vec![path!("/root/test.txt:10").to_string()])+ .await;++ let paths: Vec<_> = result+ .iter()+ .map(|p| (p.path.to_string_lossy().to_string(), p.row, p.column))+ .collect();+ assert_eq!(+ paths,+ vec![(path!("/root/test.txt").to_string(), Some(10), None)]+ );+ }+#[gpui::test]fn test_parse_ssh_url_preserves_open_behavior(cx: &mut TestAppContext) {let _app_state = init_test(cx);