Skip to content

Commit

Permalink
chore: Normalize file paths in code search query
Browse files Browse the repository at this point in the history
  • Loading branch information
Sma1lboy committed Nov 13, 2024
1 parent 6668afe commit b2e1a5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
4 changes: 2 additions & 2 deletions crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeSearchHit>,
Expand Down Expand Up @@ -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,
Expand Down
75 changes: 20 additions & 55 deletions crates/tabby-common/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,9 @@ pub fn events_dir() -> PathBuf {
tabby_root().join("events")
}

fn normalize_path_for_platform(
filepath: Option<String>,
is_windows: bool,
) -> Result<Option<String>> {
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<String>) -> Result<Option<String>> {
#[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<String>) -> Result<Option<String>> {
Ok(filepath.map(|path| path.replace('\\', "/")))
}

mod registry {}
Expand All @@ -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
);
}
}
}

0 comments on commit b2e1a5a

Please sign in to comment.