project_search: Add ability to search only for opened files (#16580)

Any suggestion?

Release Notes:
  - add ability for project search only for opend files.

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
CharlesChen0823 2024-08-29 11:41:29 +08:00 committed by GitHub
parent 403fdd6018
commit 400e503d9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 192 additions and 20 deletions

View file

@ -7238,8 +7238,11 @@ impl Project {
) -> Receiver<SearchResult> {
let (result_tx, result_rx) = smol::channel::unbounded();
let matching_buffers_rx =
self.search_for_candidate_buffers(&query, MAX_SEARCH_RESULT_FILES + 1, cx);
let matching_buffers_rx = if query.is_opened_only() {
self.sort_candidate_buffers(&query, cx)
} else {
self.search_for_candidate_buffers(&query, MAX_SEARCH_RESULT_FILES + 1, cx)
};
cx.spawn(|_, cx| async move {
let mut range_count = 0;
@ -7317,6 +7320,48 @@ impl Project {
}
}
fn sort_candidate_buffers(
&mut self,
search_query: &SearchQuery,
cx: &mut ModelContext<Project>,
) -> Receiver<Model<Buffer>> {
let worktree_store = self.worktree_store.read(cx);
let mut buffers = search_query
.buffers()
.into_iter()
.flatten()
.filter(|buffer| {
let b = buffer.read(cx);
if let Some(file) = b.file() {
if !search_query.file_matches(file.path()) {
return false;
}
if let Some(entry) = b
.entry_id(cx)
.and_then(|entry_id| worktree_store.entry_for_id(entry_id, cx))
{
if entry.is_ignored && !search_query.include_ignored() {
return false;
}
}
}
return true;
})
.collect::<Vec<_>>();
let (tx, rx) = smol::channel::unbounded();
buffers.sort_by(|a, b| match (a.read(cx).file(), b.read(cx).file()) {
(None, None) => a.read(cx).remote_id().cmp(&b.read(cx).remote_id()),
(None, Some(_)) => std::cmp::Ordering::Less,
(Some(_), None) => std::cmp::Ordering::Greater,
(Some(a), Some(b)) => compare_paths((a.path(), true), (b.path(), true)),
});
for buffer in buffers {
tx.send_blocking(buffer.clone()).unwrap()
}
rx
}
fn search_for_candidate_buffers_remote(
&mut self,
query: &SearchQuery,