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 12, 2024
1 parent c5a46ab commit d84bab1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
6 changes: 5 additions & 1 deletion crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::path::PathBuf;

use async_trait::async_trait;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::path::normalize_path;

pub struct CodeSearchResponse {
pub hits: Vec<CodeSearchHit>,
}
Expand Down Expand Up @@ -64,7 +68,7 @@ impl CodeSearchQuery {
source_id: String,
) -> Self {
Self {
filepath,
filepath: normalize_path(filepath).unwrap_or(None),
language,
content,
source_id,
Expand Down
21 changes: 15 additions & 6 deletions crates/tabby-common/src/index/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tantivy::{
pub use tokenizer::tokenize_code;

use super::{corpus, IndexSchema};
use crate::api::code::CodeSearchQuery;
use crate::{api::code::CodeSearchQuery, path::normalize_path};

pub mod fields {
pub const CHUNK_GIT_URL: &str = "chunk_git_url";
Expand Down Expand Up @@ -52,7 +52,14 @@ fn filepath_query(filepath: &str) -> Box<TermQuery> {
let schema = IndexSchema::instance();
let mut term =
Term::from_field_json_path(schema.field_chunk_attributes, fields::CHUNK_FILEPATH, false);
term.append_type_and_str(filepath);

// normalize the path base on the platform.
let filepath = normalize_path(Some(filepath.to_string()))
.ok()
.flatten()
.unwrap_or_else(|| filepath.to_string());

term.append_type_and_str(&filepath);
Box::new(TermQuery::new(term, IndexRecordOption::Basic))
}

Expand Down Expand Up @@ -84,10 +91,12 @@ pub fn code_search_query(

// When filepath presents, we exclude the file from the search.
if let Some(filepath) = &query.filepath {
subqueries.push((
Occur::MustNot,
Box::new(ConstScoreQuery::new(filepath_query(filepath), 0.0)),
))
if let Ok(Some(normalized_path)) = normalize_path(Some(filepath.clone())) {
subqueries.push((
Occur::MustNot,
Box::new(ConstScoreQuery::new(filepath_query(&normalized_path), 0.0)),
));
}
}

BooleanQuery::new(subqueries)
Expand Down
17 changes: 17 additions & 0 deletions crates/tabby-common/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use std::{cell::Cell, env, path::PathBuf, sync::Mutex};

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

pub fn normalize_path(filepath: Option<String>) -> Result<Option<String>> {
Ok(filepath.map(|path| {
let path_buf = PathBuf::from(path);

#[cfg(target_os = "windows")]
{
path_buf.to_string_lossy().replace('/', "\\")
}

#[cfg(not(target_os = "windows"))]
{
path_buf.to_string_lossy().replace('\\', "/")
}
}))
}

mod registry {}

0 comments on commit d84bab1

Please sign in to comment.