Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codeSearch): normalize path for code search #3393

39 changes: 35 additions & 4 deletions crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ pub struct CodeSearchResponse {
pub hits: Vec<CodeSearchHit>,
}

#[derive(Default, Clone)]
#[derive(Default, Clone, PartialEq, Debug)]
pub struct CodeSearchHit {
pub scores: CodeSearchScores,
pub doc: CodeSearchDocument,
}

#[derive(Default, Clone)]
#[derive(Default, Clone, PartialEq, Debug)]
pub struct CodeSearchScores {
/// Reciprocal rank fusion score: https://www.elastic.co/guide/en/elasticsearch/reference/current/rrf.html
pub rrf: f32,
pub bm25: f32,
pub embedding: f32,
}

#[derive(Builder, Default, Clone)]
#[derive(Builder, Default, Clone, PartialEq, Debug)]
pub struct CodeSearchDocument {
/// Unique identifier for the file in the repository, stringified SourceFileKey.
pub file_id: String,
Expand Down 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
);
}
}
}
Loading