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

5 changes: 4 additions & 1 deletion crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::path::normalize_to_unix_path;

pub struct CodeSearchResponse {
pub hits: Vec<CodeSearchHit>,
}
Expand Down Expand Up @@ -50,6 +52,7 @@ pub enum CodeSearchError {
}

pub struct CodeSearchQuery {
// filepath in code search query always normalize to unix style.
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
pub filepath: Option<String>,
pub language: Option<String>,
pub content: String,
Expand All @@ -64,7 +67,7 @@ impl CodeSearchQuery {
source_id: String,
) -> Self {
Self {
filepath,
filepath: normalize_to_unix_path(filepath).unwrap_or(None),
language,
content,
source_id,
Expand Down
32 changes: 32 additions & 0 deletions crates/tabby-common/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cell::Cell, env, path::PathBuf, sync::Mutex};

use anyhow::Result;
use lazy_static::lazy_static;

lazy_static! {
Expand Down Expand Up @@ -53,4 +54,35 @@ pub fn events_dir() -> PathBuf {
tabby_root().join("events")
}

/// Normalize the path form different platform to unix style path
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
pub fn normalize_to_unix_path(filepath: Option<String>) -> Result<Option<String>> {
Ok(filepath.map(|path| path.replace('\\', "/")))
}

mod registry {}

#[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(Some(input.to_string())).unwrap(),
Some(expected.to_string()),
"Failed to normalize path: {}",
input
);
}
}
}
1 change: 1 addition & 0 deletions crates/tabby/src/services/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub struct Segments {
suffix: Option<String>,

/// The relative path of the file that is being edited.
/// The filepath will keep the same format as the original file path base on the platform.
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
/// - When [Segments::git_url] is set, this is the path of the file in the git repository.
/// - When [Segments::git_url] is empty, this is the path of the file in the workspace.
filepath: Option<String>,
Expand Down
Loading