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 3d2682a commit b87cdc9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
10 changes: 2 additions & 8 deletions crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -68,15 +69,8 @@ impl CodeSearchQuery {
content: String,
source_id: String,
) -> Self {
debug!("CodeSearchQuery::new - Original filepath: {:?}", filepath);
let normalized = normalize_path(filepath).unwrap_or(None);
debug!(
"CodeSearchQuery::new - Normalized filepath: {:?}",
normalized
);

Self {
filepath: normalized,
filepath: normalize_path(filepath).unwrap_or(None),
language,
content,
source_id,
Expand Down
23 changes: 4 additions & 19 deletions crates/tabby-common/src/index/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,9 @@ pub fn body_query(tokens: &[String]) -> Box<dyn Query> {
}

fn filepath_query(filepath: &str) -> Box<TermQuery> {
debug!("filepath_query - Input filepath: {}", filepath);
let schema = IndexSchema::instance();
let mut term =
Term::from_field_json_path(schema.field_chunk_attributes, fields::CHUNK_FILEPATH, false);
// normalize the path base on the platform.
let filepath = normalize_path(Some(filepath.to_string()))
.ok()
.flatten()
.unwrap_or_else(|| filepath.to_string());
debug!("filepath_query - Normalized filepath: {}", filepath);

term.append_type_and_str(&filepath);
Box::new(TermQuery::new(term, IndexRecordOption::Basic))
}
Expand Down Expand Up @@ -93,17 +85,10 @@ pub fn code_search_query(

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

BooleanQuery::new(subqueries)
Expand Down
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.
/// - 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 b87cdc9

Please sign in to comment.