Skip to content

Commit

Permalink
to: Normalize file paths in code search query
Browse files Browse the repository at this point in the history
to: remove unuse import
  • Loading branch information
Sma1lboy committed Nov 14, 2024
1 parent b2e1a5a commit c7b8bd3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
32 changes: 30 additions & 2 deletions crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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 @@ -109,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: Option<String>) -> anyhow::Result<Option<String>> {
Ok(filepath.map(|path| path.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(Some(input.to_string())).unwrap(),
Some(expected.to_string()),
"Failed to normalize path: {}",
input
);
}
}
}
32 changes: 0 additions & 32 deletions crates/tabby-common/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{cell::Cell, env, path::PathBuf, sync::Mutex};

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

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

/// 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 {}

#[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: 0 additions & 1 deletion crates/tabby/src/services/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ 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.
/// - 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

0 comments on commit c7b8bd3

Please sign in to comment.