Use the main thread less on search tool (#26732)

Release Notes:

- N/A
This commit is contained in:
Richard Feldman 2025-03-17 11:02:22 -04:00 committed by GitHub
parent 6cac0b33dc
commit a0ee84d3ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 24 deletions

1
Cargo.lock generated
View file

@ -727,6 +727,7 @@ dependencies = [
"ui", "ui",
"util", "util",
"workspace", "workspace",
"worktree",
] ]
[[package]] [[package]]

View file

@ -30,6 +30,7 @@ theme.workspace = true
ui.workspace = true ui.workspace = true
util.workspace = true util.workspace = true
workspace.workspace = true workspace.workspace = true
worktree.workspace = true
settings.workspace = true settings.workspace = true
[dev-dependencies] [dev-dependencies]

View file

@ -1,12 +1,13 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use assistant_tool::{ActionLog, Tool}; use assistant_tool::{ActionLog, Tool};
use gpui::{App, Entity, Task}; use gpui::{App, AppContext, Entity, Task};
use language_model::LanguageModelRequestMessage; use language_model::LanguageModelRequestMessage;
use project::Project; use project::Project;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{path::PathBuf, sync::Arc}; use std::{path::PathBuf, sync::Arc};
use util::paths::PathMatcher; use util::paths::PathMatcher;
use worktree::Snapshot;
#[derive(Debug, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct PathSearchToolInput { pub struct PathSearchToolInput {
@ -56,34 +57,38 @@ impl Tool for PathSearchTool {
Ok(matcher) => matcher, Ok(matcher) => matcher,
Err(err) => return Task::ready(Err(anyhow!("Invalid glob: {}", err))), Err(err) => return Task::ready(Err(anyhow!("Invalid glob: {}", err))),
}; };
let snapshots: Vec<Snapshot> = project
.read(cx)
.worktrees(cx)
.map(|worktree| worktree.read(cx).snapshot())
.collect();
let mut matches = Vec::new(); cx.background_spawn(async move {
let mut matches = Vec::new();
for worktree_handle in project.read(cx).worktrees(cx) { for worktree in snapshots {
let worktree = worktree_handle.read(cx); let root_name = worktree.root_name();
let root_name = worktree.root_name();
// Don't consider ignored entries. // Don't consider ignored entries.
for entry in worktree.entries(false, 0) { for entry in worktree.entries(false, 0) {
if path_matcher.is_match(&entry.path) { if path_matcher.is_match(&entry.path) {
matches.push( matches.push(
PathBuf::from(root_name) PathBuf::from(root_name)
.join(&entry.path) .join(&entry.path)
.to_string_lossy() .to_string_lossy()
.to_string(), .to_string(),
); );
}
} }
} }
}
if matches.is_empty() { if matches.is_empty() {
Task::ready(Ok(format!( Ok(format!("No paths in the project matched the glob {glob:?}"))
"No paths in the project matched the glob {glob:?}" } else {
))) // Sort to group entries in the same directory together.
} else { matches.sort();
// Sort to group entries in the same directory together. Ok(matches.join("\n"))
matches.sort(); }
Task::ready(Ok(matches.join("\n"))) })
}
} }
} }