From b2e1a5af2d1f36b0f333bdac9b797af15f86e87a Mon Sep 17 00:00:00 2001 From: Jackson Chen <541898146chen@gmail.com> Date: Tue, 12 Nov 2024 22:17:26 -0600 Subject: [PATCH] chore: Normalize file paths in code search query --- crates/tabby-common/src/api/code.rs | 4 +- crates/tabby-common/src/path.rs | 75 ++++++++--------------------- 2 files changed, 22 insertions(+), 57 deletions(-) diff --git a/crates/tabby-common/src/api/code.rs b/crates/tabby-common/src/api/code.rs index 6879376cebfc..fe7b8c1e242e 100644 --- a/crates/tabby-common/src/api/code.rs +++ b/crates/tabby-common/src/api/code.rs @@ -3,7 +3,7 @@ use derive_builder::Builder; use serde::{Deserialize, Serialize}; use thiserror::Error; -use crate::path::normalize_path; +use crate::path::normalize_to_unix_path; pub struct CodeSearchResponse { pub hits: Vec, @@ -67,7 +67,7 @@ impl CodeSearchQuery { source_id: String, ) -> Self { Self { - filepath: normalize_path(filepath).unwrap_or(None), + filepath: normalize_to_unix_path(filepath).unwrap_or(None), language, content, source_id, diff --git a/crates/tabby-common/src/path.rs b/crates/tabby-common/src/path.rs index 74c1ddc3808d..7c81226fcd15 100644 --- a/crates/tabby-common/src/path.rs +++ b/crates/tabby-common/src/path.rs @@ -54,27 +54,9 @@ pub fn events_dir() -> PathBuf { tabby_root().join("events") } -fn normalize_path_for_platform( - filepath: Option, - is_windows: bool, -) -> Result> { - Ok(filepath.clone().map(|path| { - let path_buf = PathBuf::from(path); - if is_windows { - path_buf.to_string_lossy().replace('/', "\\") - } else { - path_buf.to_string_lossy().replace('\\', "/") - } - })) -} - -/// Normalize the path to unix style path -pub fn normalize_path(filepath: Option) -> Result> { - #[cfg(target_os = "windows")] - return normalize_path_for_platform(filepath, true); - - #[cfg(not(target_os = "windows"))] - return normalize_path_for_platform(filepath, false); +/// Normalize the path form different platform to unix style path +pub fn normalize_to_unix_path(filepath: Option) -> Result> { + Ok(filepath.map(|path| path.replace('\\', "/"))) } mod registry {} @@ -85,39 +67,22 @@ mod tests { #[test] fn test_relative_path_normalization() { - // the most common use, converting relative path to unix style - assert_eq!( - normalize_path_for_platform(Some("./src/main.rs".to_string()), false).unwrap(), - Some("./src/main.rs".to_string()) - ); - assert_eq!( - normalize_path_for_platform(Some(".\\src\\main.rs".to_string()), false).unwrap(), - Some("./src/main.rs".to_string()) - ); - assert_eq!( - normalize_path_for_platform(Some("../test/data.json".to_string()), false).unwrap(), - Some("../test/data.json".to_string()) - ); - assert_eq!( - normalize_path_for_platform(Some("..\\test\\data.json".to_string()), false).unwrap(), - Some("../test/data.json".to_string()) - ); - - assert_eq!( - normalize_path_for_platform(Some("./src/main.rs".to_string()), true).unwrap(), - Some(".\\src\\main.rs".to_string()) - ); - assert_eq!( - normalize_path_for_platform(Some(".\\src\\main.rs".to_string()), true).unwrap(), - Some(".\\src\\main.rs".to_string()) - ); - assert_eq!( - normalize_path_for_platform(Some("../test/data.json".to_string()), true).unwrap(), - Some("..\\test\\data.json".to_string()) - ); - assert_eq!( - normalize_path_for_platform(Some("..\\test\\data.json".to_string()), true).unwrap(), - Some("..\\test\\data.json".to_string()) - ); + let unix_test_cases = [ + ("./src/main.rs", "./src/main.rs"), + (".\\src\\main.rs", "./src/main.rs"), + ("../test/data.json", "../test/data.json"), + ("..\\test\\data.json", "../test/data.json"), + ("src/test/file.txt", "src/test/file.txt"), + ("src\\test\\file.txt", "src/test/file.txt"), + ]; + + for (input, expected) in unix_test_cases { + assert_eq!( + normalize_to_unix_path(Some(input.to_string())).unwrap(), + Some(expected.to_string()), + "Failed to normalize path: {}", + input + ); + } } }