Skip to content

Commit

Permalink
fix(codeSearch): normalize path for code search (#3393)
Browse files Browse the repository at this point in the history
* chore: normalize file paths in code search query

* chore: adding debug

* chore: Normalize file paths in code search query

* chore: fix some chore change

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* to: adding tests to normalize path

* chore: Normalize file paths in code search query

* to: Normalize file paths in code search query

to: remove unuse import

* chore: adding comment slash

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Sma1lboy and autofix-ci[bot] authored Nov 14, 2024
1 parent ca61582 commit 414cac3
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum CodeSearchError {
}

pub struct CodeSearchQuery {
/// filepath in code search query always normalize to unix style.
pub filepath: Option<String>,
pub language: Option<String>,
pub content: String,
Expand All @@ -64,7 +65,7 @@ impl CodeSearchQuery {
source_id: String,
) -> Self {
Self {
filepath,
filepath: filepath.map(|path| normalize_to_unix_path(&path)),
language,
content,
source_id,
Expand Down Expand Up @@ -106,3 +107,33 @@ pub trait CodeSearch: Send + Sync {
params: CodeSearchParams,
) -> Result<CodeSearchResponse, CodeSearchError>;
}

/// Normalize the path form different platform to unix style path
pub fn normalize_to_unix_path(filepath: &str) -> String {
filepath.replace('\\', "/")
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_relative_path_normalization() {
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(input),
expected.to_string(),
"Failed to normalize path: {}",
input
);
}
}
}

0 comments on commit 414cac3

Please sign in to comment.